# auto-aim gun like the one used in Jets n Guns

This topic is 2671 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

hi all
i'm writing code for a small 2d shooter game similar to Jets n Guns, i'm trying to implement a auto-aim gun for my spaceship but i just can't get it working.
my gun have a position, initial rotation & a direction vector for shooting bullets along it & i have a target which obviously has a position. i want my gun to rotate
automatically around its center until it faces the moving target & then fire.
thanks.

##### Share on other sites
Constant speed using longitude and lattitude:
By representing both the target direction and current direction in polar coordinates, you can take the linear distance in polar space. If the distance is lesser than timestep * speed, place it at the goal, otherwise move it closer. This method can be alloying if the gun have to move a large angle but will not have a crazy acceleration in the start.

Exponentially decreasing:
Without polar coordinates, lerp between the current direction and the target direction and normalize. This method require fixed timesteps because of the normalization but makes it look like the engines are limited by acceleration instead of friction.

Applied forces:
Treat the direction as normalized positions and try to accelerate the gun direction to the target direction. To not comming in too fast, calculate where the gun would be directed after some time. If it is shaking at the goal, just lock to the target direction if distance to target + speed is within a treshold.

If the enemy is moving, use linear interpolation of where the enemy will be after the time it takes for the projectile to reach the enemy at it's current location. For more accuracy, iterate the process 3 times by using the result location as the current location.

1. 1
2. 2
3. 3
Rutin
25
4. 4
5. 5
khawk
14

• 11
• 11
• 23
• 10
• 9
• ### Forum Statistics

• Total Topics
633650
• Total Posts
3013118
×