# 2d space game

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

## Recommended Posts

I started programming the AI for a 2d spaceship game i've been working on for a while. Now ships in my game can only turn 22.5 degrees at a time. If I want the ship to say go to the top left corner of the map, I figure out what the angle between the ship and corner is, then turn as close as I can to that angle and start moving. Eventually the ship will get to a point where the angle between it and the corner is a multiple of 22.5, meaning that the ship can start accelerating directly to its objective. Ok so here's where I hit a snag. If I have the ship accelerate towards the objective when it detects that the angle is right, it will overshoot it because it takes time to change it's velocity direction when its already moving. So basically I need to know when I should start accelerating my ship so that its on the correct course by the time the angle is right. I hope I explained all this clearly :).

##### Share on other sites
If you are applying thrust in the new direction, the angle you are travelling in is constantly changing, until it is the same as the angle you are facing. This means even though you can only face a direction that is a multiple of 22.5 degrees, you can travel at any angle inbetween. With this in mind, why limit your direction to a multiple of 22.5 degrees? why not just have the ai travel at the exact angle required to reach the destination?

##### Share on other sites
Not very clear
Is you problem with 'how to align ship onto psuedo-grid so that compass direction movement can work'?
or is your problem with 'overshooting and acceleration/decceleration times in general'?

uhh...
desired velocity - current velocity = delta velocity
accelerate over time to produce the needed delta velocity

to deccelerate so that you stop moving at the desired location without overshooting...
you can either do some calculus and try to calculate the right moment to retro-burn
or... personally im lazy and used a PID (PD) controller to do it, worked well enough...

Is the reason rotation is limited to 22.whatnot degrees because you only have that many sprites drawn?
Id reccomend allowing a separate graphic and game model direction setting... makes control a lot easier...

##### Share on other sites
Your problem involves a nontrivial amount of trigonometry and integral calculus to solve. Although it's not what you want to hear, I feel I should echo the concensus and say that you should seriously consider lifting the 22.5° constraint.
If this isn't an option, then here are a couple of pointers:

Assuming that gravity is always directed downwards, you'll find that the problem is easier to solve if your ship aligns itself below the target point before reaching it (as opposed to aligning itself horizontally with the target point, and then battling with gravity to stay on target).

Do you need your ship to hit the point exactly? If you only need to get close, you just may get away with a simple homeostasis setup, where the ship will always direct itself towards the point and accelerate, only fighting the gravitational effects passively. This would be easy to implement, but results would be unpredictable and you may find your ship going into orbit for a while before it reaches its destination. Rudimentary (linearised) measures to counteract gravity may help out considerably, here.

I shan't write you any code or equations though, since you'll probably live to regret hurling yourself into the deep end. Once again: Things will be far easier if your ship's orientation is continuous.

Regards

1. 1
2. 2
Rutin
21
3. 3
JoeJ
18
4. 4
5. 5

• 14
• 39
• 23
• 13
• 13
• ### Forum Statistics

• Total Topics
631717
• Total Posts
3001877
×