• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

GML Physics question

D

Dr Nitronio

Guest
So I'm trying to create something simple using the physics engine.

I want any physics object (like a ball, square, triangle, players, etc) to stay on the platform and move with it when the platform itself moves vertically, horizontally, or diagonally. Anytime I move the platform itself, the object will stay on top somewhat, but won't move with the platform correctly. Here's what it looks like.

Moving sideways, down, and diagonally doesn't work well. The platform will be able to tilt too so we have to make sure it works with that too.

Here is what I have to control the platform in the step event if this helps. Am I going at this wrong or am I missing code?

hspd += stick_h_hold*acceleration;
hspd = clamp(hspd, -move_speed, move_speed)
if stick_h_hold = 0 {hspd = lerp(hspd, 0, .25);}

vspd += stick_v_hold*acceleration;
vspd = clamp(vspd, -move_speed, move_speed)
if stick_v_hold = 0 {vspd = lerp(vspd, 0, .25);}

phy_position_x += hspd;
phy_position_y += vspd;

if button_a = 1 {phy_rotation -= 5;}
if button_b = 1 {phy_rotation += 5;}



 
Last edited by a moderator:
P

Pyxus

Guest
So I'm trying to create something simple using the physics engine.

I want any physics object (like a ball, square, triangle, players, etc) to stay on the platform and move with it when the platform itself moves vertically, horizontally, or diagonally. Anytime I move the platform itself, the object will stay on top somewhat, but won't move with the platform correctly. Here's what it looks like.

Moving sideways, down, and diagonally doesn't work well. The platform will be able to tilt too so we have to make sure it works with that too.

Here is what I have to control the platform in the step event if this helps. Am I going at this wrong or am I missing code?

hspd += stick_h_hold*acceleration;
hspd = clamp(hspd, -move_speed, move_speed)
if stick_h_hold = 0 {hspd = lerp(hspd, 0, .25);}

vspd += stick_v_hold*acceleration;
vspd = clamp(vspd, -move_speed, move_speed)
if stick_v_hold = 0 {vspd = lerp(vspd, 0, .25);}

phy_position_x += hspd;
phy_position_y += vspd;

if button_a = 1 {phy_rotation -= 5;}
if button_b = 1 {phy_rotation += 5;}



Im not at my normal computer so I can't hop on test out a solution but I can provide an explanation for why this is occurring and possibly steer you in the right direction. Side note, im not a physicist nor do I possess an in depth understanding on the Box2d physics Engine so anyone feel free to correct me if im wrong.

In physics a force is required to accelerate an object, when you suddenly drop the platform downwards the object on top of the platform wont by default move with the platform because they are accelerating and different rates and thus moving at different velocities. Same idea applies when moving the platform to the left or the right, at rest everything is fine because there is no acceleration but the moment you move the platform the platform is accelerating while the objects on top continue to stay at rest.

When you think about it that's how objects actually behave in the real world aswell, anyway. To have the objects on top move in accordance with the platform using the physics engine you in theory have to apply a force so that they accelerate at the same rate. For horizontal movement you could play around with the friction value however for vertical movement the only thing I can think of is having the platform gradually build up speed like an elevator would.
 
Last edited by a moderator:

Jezla

Member
You are going about it incorrectly. By manually adjusting the phy_position_x and _y, you are breaking the simulation. When using the physics system, you need to move objects by applying forces or impulses. In addition, rotate your objects by applying torque or an angular impulse.

As @Pyxus said to have the top object move in conjunction with the bottom one, you'll have to account for inertia by either applying a similar force or impulse, or increasing the friction between the two so that the top object does not 'slide' on the bottom one.

If you haven't already done so, I strongly recommend reading ALL of the physics documentation (even the stuff you don't think you'll use right now) and following this series of tech blogs.
 
Last edited:
Top