Objective c 计算两个SKSPRITENODE之间的截距速度
我创建了一个“ship”节点,以沿圆形路径移动,如下所示:Objective c 计算两个SKSPRITENODE之间的截距速度,objective-c,sprite-kit,game-physics,Objective C,Sprite Kit,Game Physics,我创建了一个“ship”节点,以沿圆形路径移动,如下所示: self.orbit = [OrbitManager getCirclePathWithCenter:centerRealPt radius:radius startingAngle:angelFromCenter isClockwise:self.isClockwise]; SKAction* orbitAction = [SKAction followPath:self.orbit asOffset:NO orientToPath
self.orbit = [OrbitManager getCirclePathWithCenter:centerRealPt radius:radius startingAngle:angelFromCenter isClockwise:self.isClockwise];
SKAction* orbitAction = [SKAction followPath:self.orbit asOffset:NO orientToPath:YES speed:300];
[self.ship.node runAction:orbitAction];
bullet.node.physicsBody.velocity = [ActionHelper getVelocityFrom:bullet.node toNodeB:self.target speed:bullet.speed];
我有一门大炮,它通过对子弹施加速度来发射子弹,如下所示:
self.orbit = [OrbitManager getCirclePathWithCenter:centerRealPt radius:radius startingAngle:angelFromCenter isClockwise:self.isClockwise];
SKAction* orbitAction = [SKAction followPath:self.orbit asOffset:NO orientToPath:YES speed:300];
[self.ship.node runAction:orbitAction];
bullet.node.physicsBody.velocity = [ActionHelper getVelocityFrom:bullet.node toNodeB:self.target speed:bullet.speed];
当船沿着小路行驶时。但子弹每次都会打不中。如何计算加农炮在给定速度下应瞄准的位置?这是我的目标C(实际上是一个C函数)解决方案,用于向运动目标发射射弹
你可以看看派生词
这会给你一个命中点和拍摄角度,
你可以简单地把它转换成速度,因为你知道角度和抛射速度,它会是这样的:
`CGVector Velocity = CGVectorMake(speed * cos(theta), speed * sin(theta));`
BOOL calculateAngleToShoot(CGVector targetVelocity, CGPoint targetPosition, CGPoint selfPos,CGFloat projectileSpeed,CGFloat *theta, CGPoint * hitPoint)
{
CGFloat dx = targetPosition.x - selfPos.x;
CGFloat dy = targetPosition.y - selfPos.y;
CGVector v = targetVelocity;
CGFloat a = v.dx * v.dx + v.dy * v.dy - projectileSpeed * projectileSpeed;
CGFloat b = 2 * (v.dx * dx + v.dy * dy);
CGFloat c = v.dx * v.dx + v.dy * v.dy;
CGFloat q = b * b - 4 * a * c;
if (q < 0)
{
//Dead End;
return NO;
}
CGFloat t = ((a < 0 ? -1 : 1) * sqrt(q) - b) / (2 * a);
// Aim for where the target will be after time t
dx += t * v.dx;
dy += t * v.dy;
*theta = atan2(dy, dx);
*hitPoint = CGPointMake(targetPosition.x + v.dx * t, targetPosition.y + v.dy * t);
return YES;
}
CGVector速度=CGVectorMake(速度*cos(θ),速度*sin(θ))`
BOOL CalculateAngleToShot(CGVector targetVelocity、CGPoint targetPosition、CGPoint selfPos、CGFloat ProjectleSpeed、CGFloat*theta、CGPoint*hitPoint)
{
CGFloat dx=targetPosition.x-selfPos.x;
CGFloat dy=targetPosition.y-selfPos.y;
Cgv=目标速度;
CGFloat a=v.dx*v.dx+v.dy*v.dy-投射速度*投射速度;
cgb=2*(v.dx*dx+v.dy*dy);
CGFloat c=v.dx*v.dx+v.dy*v.dy;
CGFloat q=b*b-4*a*c;
if(q<0)
{
//死胡同;
返回否;
}
cgrt=((a<0-1:1)*sqrt(q)-b)/(2*a);
//瞄准时间t后目标的位置
dx+=t*v.dx;
dy+=t*v.dy;
*θ=atan2(dy,dx);
*生命点=CGPointMake(targetPosition.x+v.dx*t,targetPosition.y+v.dy*t);
返回YES;
}
经过一些调查,我知道了如何得到答案 首先,我需要得到目标和中心之间的距离(d) 子弹从中心到目标的时间。 因为船是沿着圆移动的,所以半径也等于距离(d) 查找在此时间段内移动的角度
CGFloat angle = angularSpeed * timeToArriveTarget;
CGFloat x = self.target.position.x;
CGFloat y = self.target.position.y;
CGFloat a = bullet.node.position.x;
CGFloat b = bullet.node.position.y;
最后使用这个公式:
详情请点击此链接
getVelocity函数由以下公式给出
+(CGVector)getVelocityFrom:(CGPoint)ptA toPtB:(CGPoint)ptB speed:(CGFloat)speed{
CGPoint targetPosition = ptB;
CGPoint currentPosition = ptA;
double angle = [MathHelper getRotateAngleFrom:currentPosition toTargetPosition:targetPosition];
double velocityX = speed * cos(angle);
double velocityY = speed * sin(angle);
CGVector newVelocty = CGVectorMake(velocityX, velocityY);
return newVelocty;
}听起来你在计算轨迹,你需要计算出两个物体在什么时间点碰撞,这需要一些数学知识