0

I have a 2D top-down jetboat that is moving (has a x, y velocity). I apply the velocity by multiplying against the delta time dt:

position.x += velocity.x * dt
position.y += velocity.y * dt

Before that however I calculate the velocity with:

velocity.x += acceleration * cos(angle) * dt
velocity.y += acceleration * sin(angle) * dt

angle = (heading_current * M_PI) / 180;

Angle is in radians, and it should be changing the velocity x, y based on the angle at which the force is being applied.

Two things seem to be going on, one is that the angle is at 0° and the boat travels toward 90°, and two, when I change the angle to anything else, the boat just continues on it 90° path. What am I doing wrong?

I hope that makes sense.

Pikalek
  • 12,372
  • 5
  • 43
  • 51
fresh
  • 1
  • 3
  • "But it's not" — what symptoms are you observing instead? – DMGregory Mar 15 '24 at 10:12
  • Have updated my question with what is not working – fresh Mar 15 '24 at 10:24
  • 1
    What heading do you call 0° here? +y or -y? Do you notice any deflection at all when changing the angle, even if it's not as sharp a turn as you expected? – DMGregory Mar 15 '24 at 11:09
  • Hmm, there is a gradual change, we are talking minutes to see anything. 0deg should be north right? But my boat is going 90deg, east – fresh Mar 15 '24 at 11:20
  • No, using the mathematical convention of x = cos(angle) as you're using, zero degrees points toward x+ (usually East in typical graphing layouts). If you want 0 to point up, swap cos and sin. You may have to negate one or the other depending on your coordinate, system, whether y+ points up or down, and whether you want angles to increase clockwise or counterclockwise. – DMGregory Mar 15 '24 at 11:24
  • I note that your code doesn't show any way to bleed off accumulated velocity in the old direction before adding velocity in the new direction. That means if you've been accelerating one way for some amount of time, you'll have to spend an equal time accelerating in the new direction before your heading bends even halfway. This is like a probe out in deep space, where a tiny nudge will send it drifting in a straight line forever. If you want to simulate a water craft, you need to add water resistance. – DMGregory Mar 15 '24 at 11:29
  • That seems to have fixed that issue, thanks. when I increase the speed after changing the heading, i can see the change in velocity, it's still slow however, i'm guessing apart from the direction of travel there's was no issue, its just was hard to see a change in velocity. Is there a simple way to "over compensate" the heading for the lack of time it takes to change the velocity? – fresh Mar 15 '24 at 11:31
  • Thank you for your help! – fresh Mar 15 '24 at 11:57
  • If that solved your problem, feel free to post your solution as an Answer below. If not, want to edit your question to express your revised understanding of the situation/updated code and describe what help you need to take it to the next step? – DMGregory Mar 15 '24 at 16:46

0 Answers0