Physics moves object briefly on slope before Event Sheet correction

Not favoritedFavorited Favorited 0 favourites
  • 3 posts
From the Asset Store
Very simple code without excess options (15 events for server and 11 events for client)
  • [youtube video="[youtube video="

    "]"]**Title:** How can I prevent Physics from moving an object before Event Sheet correction?

    Hi,

    I’m trying to find a practical solution to a Physics issue in Construct 3.

    I have a physics-based object landing on a slope, for example around 45 degrees. Once grounded, my Event Sheet logic prevents it from sliding.

    The problem is that, at the exact moment of contact, the Physics behavior seems to move the object very briefly along the slope before my Event Sheet logic can stop it.

    There is no player input involved when this happens.

    It looks roughly like this:

    Physics simulation

    → contact with slope

    → very small movement along the slope

    → Event Sheet runs

    → velocity/state is corrected

    The movement is extremely brief, but visible.

    An important detail is that the issue seems to disappear when using **Unlimited framerate**, which makes me think the unwanted movement happens during a physics step before my correction code runs.

    What I’m mainly looking for is a way to **prevent that first unwanted movement entirely**.

    Is there any solution for this, for example:

    - running code before the Physics behavior updates;

    - using JavaScript and `pretick`;

    - manually controlling or stepping the physics simulation;

    - creating a custom addon/behavior;

    - changing the physics timestep or update order;

    - intercepting the contact before the solver applies movement;

    - or any other workaround that would let Event Sheet/JavaScript logic modify the object before the Physics step?

    Ideally, I want to keep using the Physics behavior, but have enough control to make a character remain perfectly stationary on a slope without one visible physics step occurring first.

    If this cannot be done directly with Event Sheets, I’m also open to a JavaScript or addon-based solution.

    What would be the recommended way to achieve this in Construct 3?

    Thanks.

  • Can't you just disable physics on the object at some point before it hits the slope?

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Construct physics doesn't provide the ability to intercept a collision to do your own logic. The underlying library probably does but it’s not exposed.

    You could go the route of using a js library to do the physics with that. You’d just need to add code to sync the physics sim with construct objects and vise versa. Main annoyance would be to get the library to use construct object collisions. Especially for some objects like tilemap. Anyways then you’d use a postCollide callback or something in the library to intercept the collision response and do your own thing.

    There may be something simpler you can do. If you kept tract of the objects previous position and velocity you could set the object to that after a collision with a slope and manually advance the object till its in contact with the slope. Could be fiddly if you end up having to fight the physics behavior.

    Logic for that would be to save the position and velocity to some variables at the end of every tick. Then under a player “collides with slope” condition you’d know that the physics hit and slid down the slope a bit in the last update so you’d set the position and velocity to the saved values. Next to close the gap you could manually advance the position till in contact with a loop similar to:

    repeat 100 times
    — player: move 0.5 pixels at angle angle(0,0,self.physics.velocityX, self.physics.velocityY)
    — player: overlaps slope
    — — stop loop
    — — player: set velocity to 0,0
    — — //any other logic you want

    You can adjust the step size to be more accurate. That assumes linear motion in the last step so to incorporate the acceleration from gravity you’d set the position instead of moving at angle which would look similar to this:

    Set x to self.x+self.vx*step
    Set y to self.y+self.vy*step+0.5*gravity*step*step

    Units are strange in the physics behavior so you may have to modify the gravity value a bit by a factor of 50. Either /50 or *50 but I forget. For step you’d use a value smaller than dt to get a more accurate collision.

    Some may argue that you could use a raycast to calculate where the object may hit the slope. However, it would miss collisions near the edges and would only work well for round players.

    Rolling your own physics with events may be viable too depending how complex you want it. Biggest chunk would be collision detection and resolution. But if you want to have rotating physics objects then it may just be easier to use the physics behavior instead or calculating contact points.

    Revisiting the idea of doing your own collision detection. The idea I posted above is basically moving things in sub-steps to get a semi accurate collision point. If you wanted to get the collision normal then you’d need to use some raycasts in that care to find that (especially if your slopes are just rotated walls) or maybe you could cheese it by utilizing the angle of the wall.

    There are more elaborate collision detection that both detect collision and give you the closest directing to move objects apart (but that will result in sliding). Some kind of shape casting algorithm can get you the position exactly where the objects touch without sliding, but that’s in straight lines and isn’t trivial for all shapes. But I’m rambling a bit.

Jump to:
Active Users
There are 0 visitors browsing this topic (0 users and 0 guests)