How to make spaceship move to direction without instantly turning and facing direction?

Started by
3 comments, last by GoldSpark 1 year, 10 months ago

I have 2D sprite of a spaceship and I need him to move towards the waypoint without instantly changing his direction but to actually slowly turn around depending on how large it is. Can someone give me the pointers where to learn about that or teach me here how would that look like as a formula?

Ayy lmao

Advertisement

If you want to go from point A to point B (or facing A to facing B) in a series of steps, instead of instantly jumping between A and B, that is not hard. The solution involves using interpolation to get a series of in between steps. This process is the same whether you are talking about changing positions, or changing rotations.

There are many different types of interpolation one could use for this process. But the basic idea is that you have a start point and an end point, and a time you want to take to go from start to end. Then you have a number of tween states (probably determined by frame rate, in a game). You then divide the move up into mini-steps, and go step by step till you reach the destination time and end point.

Linear interpolation is the most simple, so I'll start with that as an example. . . Lets say you want your spaceship to turn 60 degrees over 1 second. And to keep the math simple, let us assume that your game is running at 60 frames per second.

So, instead of going from 0 to 60 instantly (in one step), you'd turn the ship 1 degree per frame, and with 60 frame over the course of 1 second, you'd reach your 60 degree goal at the end of 1 second. Of course, things don't usually line up that neatly, but the idea remains the same.

As I mentioned above, there are other ways to generate those tween points besides linear interpolation. . . I recently watched a pretty good youtube video ( here ) on the subject, which you might want to check out.

A really simple concept you can use is to create left and right vectors 90 degrees from the ships current velocity. These can be created as the ships velocity perpendicular vectors left = -y,x right = y,-x (or right and left swapped if its wrong).

float magnitude = magnitude(ShipsNewVelocity ) ; // learn how to compute length of a vector.
ShipsNewVelocity = ShipsOldVelocity + left*steerSpeed;
ShipsNewVelocity = normalize(ShipsNewVelocity) ; // convert velocity to a vector that is of length 1.
ShipsNewVeloeicty *= magnitude; // The new direction you want to go but maintain the same speed as your prior direction.

NBA2K, Madden, Maneater, Killing Floor, Sims http://www.pawlowskipinball.com/pinballeternal

Thank you all a ton! <3

Ayy lmao

This topic is closed to new replies.

Advertisement