2
\$\begingroup\$

I'm making top-down race game in GM:S with Box2D, the car is controled in unusual way: player sets direction and engine's speed, and the car uses them: turns to the desired direction and reaches speed. But the question is about the "residual" speed after the turn. Here is what I mean: enter image description here

I make rotation by applying torque and move with applying force. I don't have tyres, it's just one plain object.
So how can I get rid of this momentum? I'm working in GM:S, but I'd like to hear any solution, I can adapt it easily.

Movement code:

var ad = angle_difference(phy_rotation, desiredDir);
physics_apply_torque((angleTorque) * -sign(ad)); // rotate the car

dx = lengthdir_x(spd, -phy_rotation);
dy = lengthdir_y(spd, -phy_rotation);

physics_apply_force(phy_position_x, phy_position_y, dx, dy);  // move the car
\$\endgroup\$
2
  • \$\begingroup\$ It would be helpful to see the code you are using. I suspect you want to use a force perpendicular to the car, rather than a torque force. The torque force won't interact with the existing movement. \$\endgroup\$
    – CLo
    Commented Jul 21, 2017 at 20:12
  • \$\begingroup\$ @Chris I added the movement code, but I don't think that it's because of the torque. Even if it is, how can I get rid of the momentum? \$\endgroup\$ Commented Jul 22, 2017 at 9:50

1 Answer 1

1
\$\begingroup\$

Combination of Forces

Instead of using a Torque force to steer the car, apply force perpendicular to the direction it is traveling. This is very roughly how the physics actually works.

When you apply the force perpendicular to the forward direction, the forward force and the perpendicular force will combine over each physics update to change the direction of the car. In this way, the forward force is "negated" by being added to the perpendicular force, creating a new direction for the car.

Torque forces and linear forces do not combine in this way. So using torque to turn is actually not affecting the forward force at all.

You may also need to manually apply a rotation to the car to keep it pointed the same direction as it is currently being moved.

GML Notes

I haven't worked much with physics in Game Maker, however, I'd look at the local physics functions.

https://docs.yoyogames.com/source/dadiospice/002_reference/physics/forces/physics_apply_local_force.html

With this it'd probably be easiest to setup three "forces" and apply them as needed for acceleration, left, and right turn.

Forward (assuming from your picture that 0,1 is forward):
physics_apply_local_force(0, 0, 1, 0)

Left Turn:
physics_apply_local_force(0, 0, 0, 1)

Right Turn:
physics_apply_local_force(0, 0, 0, -1)

You should swap out the "1" or "-1" for magnitude of the force.

Game Maker example for handling the rotation:
https://docs.yoyogames.com/source/dadiospice/002_reference/physics/physics%20variables/phy_rotation.html

\$\endgroup\$
1
  • \$\begingroup\$ Thanks, I will try it out and mark the answer if it will change something! \$\endgroup\$ Commented Jul 23, 2017 at 12:15

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .