Add force and clamp speed

How do I…

I want to add force and clamp the (x, y) force in the 2D physics engine.

What is the expected result

Add force and limit the (x, y) force coordinates to a maximum movement speed.

What is the actual result

My player fades off screen once I press any of the arrows.

Related screenshots

Project files (optional)

ZIP:

https://github.com/hydroperfox/rigidfourdemo/archive/refs/heads/master.zip

I’m assuming you want to do this in events, not javascript. Have you tried clamping the X and Y linear velocities?

If you want the object to slow down, increase friction or linear dampening in the object’s behaviour settings.

1 Like

Your player has the physics2 behavior but the JavaScript is applying basic force.
You need to get the behavior and use its force method. It’s something like this.

Edit: I think I had that backwards. Fixed. I think.

applyForce(forceX: number, forceY: number, positionX: number, positionY: number): void

const player = objects[0];
/** @type {gdjs.Physics2RuntimeBehavior} */
const physicsBehavior = player.getBehavior("Physics2");
physicsBehavior.applyForce(10,10, player.getX(),player.getY())

As I stated in my other post. I’m a bit weak when it comes to JavaScript. Even weaker when using JavaScript with behaviors. You’ll need to make sure all of your code used the correct method.

https://docs.gdevelop.io/GDJS%20Runtime%20Documentation/classes/gdjs.Physics2RuntimeBehavior.html

1 Like

@Keith_1357 Using these methods you found now the force works. My car is just not moving as I want, but I’ll see what I can do to make it work…

1 Like

I have made the force work like I want now, combining a bit of linear damping as recommended by @MrMen

Now, just needing the 360 degrees drift to rotate the car a bit more nicely, but it’s pratically in place now.