I have a “space ship” that I want to go to a “target”. How do I calculate where to add velocity.
I have a direction in radians, and I have the direction to the target in radians, and I have current velocity in radians. But I fail to see how I can get it to aim at the right spot. I have tried to do something like “targetDirection - (velocityDirection - targetDirection)”, but it only works when I aim at a small portion of the screen.

``````    Vector2 position;//Ship position
Vector2 velocity;//Ship velocity
double direction;//Direction of target in radians
double dirVel;//Direction of velocity in radians
Vector2 gointNextTick1Adjusted;//Shows where the ship is next tick.
``````

How do I calculate where to point the ship to adjust for the direction my “stored” velocity is taking me?
I don’t care if the velocity at the target is “to big” as I just kill all velocity when I reach it. But I would like to not have to “stop” the ship every time I want to move the ship. Target is a move target. In the end I want to have the ship move at full speed until it is halfway there and then flip, but as long as I can’t get it to aim properly I cant get further.

This is driving me nuts.

You can either use my Angle class, which greatly simplifies working with angles, and has a ToVector2 method which converts it to a unit vector.

Or you can simply not use angles at all for your targeting. Why would you? As far as I know, you can simply do (targetPosition - position).Normalize() and get yourself a unit vector from your position to the target. No angles involved. But again, if you need angles specifically for some reason, just use the Angle class.

That seems like a nice class. But I don’t think it solves my problem. Let me try to explane it with a example.

In the picture the “star” is my move target, but the green vector is my current velocity. The Black line is a vector to the target, but if I add it to my “velocity” I would not move directly towards the target but towards the side of it. I would need to know how much I have to change my “aim” to the other side so that I would start to move towards the target and not to the side. How do I calculate my adjusted aimpoint (Red line)?

acos (?) of the Dotproduct of the unit vectors will give you the angle between those vectors in radians.

but there is a bit more to it - I recommend watching some youtubne videos about the math as its quite a good learn, the videos from Freya are really good in actually learning about the math without really being a mathematician Vectors & Dot Product • Math for Game Devs [Part 1] - YouTube

If I guess correctly, this looks similar to my own game where I have ships moving in space in some direction and speed and then the ships need to turn to reach another destination like a star. I couldn’t find the exact angle to turn exactly because speed changes every frame on top of that I have gravity and stellar wind in some levels. Does your ship turns in 1 frame to the correct position? I guess not.

So I use atan2 to know what should be the target angle, but that will change every frame depending on the next position , new speed, new angle, so what I do is calculate the new angle, and I rotate my ships left or right depending on the target angle, so every frame it is calculated so it rotates to target little by little without me knowing what should be the exact target angle. I also have ship mass and many other things that makes the rotation very difficult to estimate, so by doing 1 frame at a time and a little bit of rotation gives me what I need. You can see a video examples of my ships running around in my youtube channel @ Junkcraft Armada - Pre alpha testing Chase/Dogfight/Inside ship crew Test - YouTube

1 Like

Keep adding your acceleration vector to the velocity and cap its length at your max movement speed. Eventually, you will get to the target vector. If you don’t need the acceleration… just set the vector to your velocity. No complicated calculations or questionable videos required.

There is no max movement speed, it’s in space.

But I have a “working” solution, using Martenfur’s class. It have a conversion from angle to radians, and a compare solution. It works when I’m moving the right way, so I use my old very flawed solution to get it right enough and then I use the correct solution. I still don’t understand why it doesn’t work when I’m more than 180 degrees wrong, but it looks ok ish for what I have, a turn based(Paradox style) strategy game in space.

I even got the ships to turn and slow down when the speed is at the max for the distance it needs to travel.

It took me a few days to get it working, but it have stooped me for almost a year.