1

Here's a quick description of what some of the methods you'll see do:

  • this.bounds: Returns the ship's bounds (a rectangle)
  • this.bounds.getCenter(): Returns a Vector2d representing the center of the ship's bounds.
  • getVelocity(): A Vector2d which represents the ship's velocity (added to position every frame)
  • new Vector2d(angle): A new Vector2d, normalized, when given an angle (in radians)
  • Vector2d#interpolate(Vector2d target, double amount): Not linear interpolation! If you want to see the interpolate code, here it is (in class Vector2d):

    public Vector2d interpolate(Vector2d target, double amount) {
       if (getDistanceSquared(target) < amount * amount)
           return target;
       return interpolate(getAngle(target), amount);
    }
    public Vector2d interpolate(double direction, double amount) {
       return this.add(new Vector2d(direction).multiply(amount));
    }
    

When the player is not pressing keys, the ship should just decelerate. Here's what I do for that:

public void drift() {
    setVelocity(getVelocity().interpolate(Vector2d.ZERO, this.deceleration));
}

However, now I've realized I want it to drift while going toward a target. I've tried this:

public void drift(Vector2d target) {
    double angle = this.bounds.getCenter().getAngle(target);
    setVelocity(getVelocity().interpolate(new Vector2d(angle), this.deceleration));
}

This of course won't ever actually reach zero speed (since I'm interpolating toward an angle vector, which has a magnitude of 1). Also, it only really "drifts" in the direction of the target when it gets very slow.

Any ideas on how I can accomplish this? I just can't figure it out. This is a big question so thanks a lot.

This is using a big library I made so if you have any questions as to what some stuff does please ask, I think I've covered most of it though.

meowgoesthedog
  • 14,670
  • 4
  • 27
  • 40
MCMastery
  • 3,099
  • 2
  • 20
  • 43
  • 2
    I don't understand what you're trying to do. If you just want to decelerate, then just reduce the length of the velocity vector. If you assume [Rayleigh drag](https://en.wikipedia.org/wiki/Drag_(physics)#Drag_at_high_velocity), the drag force grows quadratically with velocity. In the case of [Stokes drag](https://en.wikipedia.org/wiki/Drag_(physics)#Very_low_Reynolds_numbers:_Stokes.27_drag), it grows linearly. Assuming a constant ship mass and a constant update time frame, you would do something like: `dragForce = constant * v.length()(^2); v = v * (1 - dragForce / v.length())`. – Nico Schertler Sep 01 '17 at 06:00
  • 2
    Btw, please don't link to external content if it is essential to your question. As far as I can see, your interpolation function will just return the zero vector if you input the zero vector (because its length is less than `amount * amount`). – Nico Schertler Sep 01 '17 at 06:01
  • @NicoSchertler sorry, i had to post on pastebin because it was not formatting on here, no matter what i tried. The interpolation does work (i use it many times). Right now the ship just decelerates when no key is pressed, until it stops completely, while going in the last direction it was moving. I want the ship to do this same thinh, but in a certain direction. Before, i could just interpolate the velocity toward zero. But i dont know what to do if i want it to slow down while being able to change direction. If its still hard to understand maybe ill post some images, if that might help. – MCMastery Sep 01 '17 at 06:18
  • Do you always require the ship to stop *on* the target? This would not be possible with a simple drag force, but might be if your ship has some kind of "brake" system – meowgoesthedog Sep 01 '17 at 08:18

1 Answers1

1

Your interpolate function does strange things. Why not use simple physical models? For example:

Body has position (px, py) and velocity (vx, vy), also unit direction vector (dx, dy) is convenient

After (small) time interval dt velocity changes depending on acceleration

vx = vx + ax * dt
vy = vy + ay * dt

Any outer force (motor) causes acceleration and changes velocity

ax = forcex / mass  //mass might be 1
ay = forcey / mass

Dry friction force magnitude does not depend on velocity, it's direction is reverse to velocity

ax = c * mass * (-dx)
ay = c * mass * (-dy)

Liquid friction force magnitude depends on velocity (there are many different equations for different situations), it's direction is reverse to velocity

ax = k * Vmagnitude * (-dx)
ay = k * Vmagnitude * (-dy)

So for every time interval add all forces, find resulting acceleration, find resulting velocity, change position accordingly

MBo
  • 77,366
  • 5
  • 53
  • 86