Heat-Seeking Missile Game Physics

We’re working on an iPhone game using the excellent cocos2d library. The game has a heat-seeking missile that attempts to collide with a game object. The initial implementation was one that just tracks the object and adjusts its velocity vector to intercept it. Unfortunately, this approach allowed the missile to “turn on a dime” which looked completely unrealistic. We’ve tried several attempts to correct it but they all had various issues.


The best workable solution we could identify involved some simple trigonometry. The missile has an angle (in radians) and a speed. Each game tick, the missile position (stored as floats) is updated with the following code:

 

 

 

 

 

 

missilePosX += cosf(missileAngle) * missileSpeed * timeSinceLastTick; 
missilePosY += sinf(missileAngle) * missileSpeed * timeSinceLastTick; 

 

 


To determine the missile’s angle at any given game tick, you need the target position and the missile position:

 

 

 

 

float angleToTarget = atan2f(targetPosition.y - missilePosition.y, targetPosition.x - missilePosition.x); 
if (missileAngle > angleToTarget) {   
   missileAngle -= 0.05; 
} else {  
   missileAngle += 0.05; 
} 


Finally, the rotation of the sprite (in degrees) is adjusted so that it matches the missile’s heading:

 

 

 

sprite.rotation = (-missileAngle*(180/3.142)); 

 


The end result is a missile that tracks the object but moves in a much more realistic manner than one that always adjusts directly to an intercept vector. While this quick implementation required additional changes to improve its realism, this proved to be a reasonable the starting point. 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值