GML Need help with physics

Scienitive

Member
Hello I recently learned about GameMaker's physics and added that into my game but I have a problem and I have no idea how to solve it.

My game is a platformer game. In my game there are some movable (dynamic) objects. Player can push these blocks. When they push more than one block at a time the friction and density makes it harder (naturally). But I don't want that.
What I want is: No matter how many blocks player push, player has to apply predetermined value of force.

How can I do it? Thanks for replies and sorry about bad english.
 

chamaeleon

Member
Physics engines tend to try to be realistic. Since what you want is not realistic or seems you have a decision to make. Use physics and live with the implications, or implement your own movement of instances without physics so they behave the way you want.
 

Jezla

Member
This thread describes a similar problem to what you are describing. I posted a solution there that should work for you as well. The example project I posted is for GM:S 1.4, but it should work in GMS 2.
 

NightFrost

Member
This is doable without physics too, though the code can be bit of a task to write. You need to write a movement function that is called in the context of the moving instance, and it receives the distance to move and the direction to move. The function would need to do the following:
  1. Check collisions along the given direction to given distance (hence, "current movement distance").
  2. If any collisions were detected, for each collider:
    1. Calculate using current movement distance and distance between pusher's and collider's edges how far it needs to move into given direction to no longer be in collision.
    2. Call this function in the context of the collider and tell it to move that much in given direction.
    3. If the collider returns it was not able to move full requested distance, shorten current movement distance by the difference of requested move and actual move.
  3. Now that every collider has been told to move, the remaining value in current movement distance tells you how much space there is to move, so you may move by that amount.
  4. Return the difference between how far you were originally asked to move, before collision detection, and the distance actually moved, that is, the value in current movement distance.
In other words, if there's a player character on a platform with three boxes in a row: when the player walks into the first box, it tells the first box to move, which tells the second box to move, which tells the third box to move, and as the third box finds no collisions it moves, then the second box moves, then the first box moves, then the player moves as the very last thing.
 
Last edited:
Top