#player movement, collision, and physics

16 messages · Page 1 of 1 (latest)

silver walrus
#

hey all! i've ended up with my game mechanics in a knot and hoping someone can help me untangle them. ;v;;

i've been working on a simple game where different objects respond to the player differently when the player interacts with them. attached is the guide i made for my design doc, but i'll explain here too:

  • one type of jellyfish slows the player down if they touch it
  • one type of jellyfish is pushed around by the player if they move into it
  • one type of jellyfish speeds the player up if they approach it/graze it in a certain radius

at the moment, player movement is directional, and speed is based on motion_add/acceleration code (screenshotted below). this works fine on its own, but i learned from tutorials that i may have to use physics - specifically for the objects pushed around by the player. i used this tutorial to attempt this, but it seems like the two things i've coded don't work together. when i turn on physics in the room, the player doesn't move, and obviously running into the jellies with no physics causes a crash.

is there some way i can adjust my directional player movement to use physics instead of a fixed speed? alternatively, if i should redo the player movement code completely, how do i move the player without them being affected by gravity? (all the platformer tutorials i can find rely on it, but my guy is swimming, so......)

also, i haven't figured the other two jellyfish types out yet either, if anyone is willing to tackle those with me as well. thank you!

In this tutorial learn how to add bouncing objects in GameMaker, use physics within the game engine, make objects bounce, use parent objects, and create re-usable functions

This tutorial is suitable for beginner game devs, you can follow along with both GameMaker languages: GML & GML visual, and all the assets you need are downloadable.

Compl...

▶ Play video
vagrant fern
#

Turning on physics prevents most of the standard non-physics movement methods from working.

You will have to substitute out those movement methods for methods that utilize the physics engine. If you're doing movement based upon a vector, which is seems like you are, then reading up on physics_apply_local_force might be a good starting point.

It also changes how collision detection can be done.

civic rapidsBOT
#

Another way to use force in the physics world is to apply it locally to an instance. What this means is that the strength and direction of the force are calculated based on the origin (or the position if it has no sprite) of the instance, without taking into consideration the direction or rotation it may have in the game room or physics world. It should be noted that with this function, forces are not applied to the center of mass of the object, but rather at a point relative to the instance and they will not be instantly applied as they are dependent on any other forces that are working on the object (like gravity). This illustration demonstrates how a local force works:

Arguments
xlocal: The x coordinate relative to the origin where the force will be applied
ylocal: The y coordinate relative to the origin where the force will be applied
xforce: the x component of the force vector
yforce: the y component of the force vector

vagrant fern
#

I haven't done a lot of work with physics (just one small and half-finished game) but the nice thing about it is there is no need to recalculate the direction of the apply_local_force. It just updates automagically as the physics rotation of the object is updated.

silver walrus
#

i'm experimenting with it now! there's a lot to test but that gives me a direction to try i hadn't before, so ty!

vagrant fern
#

For sure! For context, I have a physics-enabled zombie shooter. This was the method I used to move my zombies forward towards their chase target when in their chase state. Then all I did to steer them was make them adjust their physics rotation over time to face them towards the target.

#

Hopefully it works for what you are trying to do. But if not, here are other tools you can use:

silver walrus
#

im looking into local impulses now though and it seems like it would be viable for player movement? so i'm gonna try that too

vagrant fern
#

I use physics apply local impulse to move my player object, so yes, it is.

I do it this way, but for me the rotation of my object is totally independent of the movement direction:

#

And frankly, I fumbled my way into it since most of the physics oriented tutorials were basically 'let's make angry birds', and very few where they were replicating non-physics movement in a physics enabled game.

silver walrus
#

messing with local impulse just to see what does what, and the good news is the collision works how it's supposed to!! i just have to refine player movement

vagrant fern
#

Lovely. Yeah. Get ready for a lot of tweaking of values, both in your code and in the physics settings pane to get it working the way you want. It's helpful to know that many settings are relative to the size of your physics collision area. So if there are two objects with equal density, one with a smaller sprite and collision area, and one with a larger one, then the smaller-sprite object is denser.

silver walrus
vagrant fern
#

The good part, though, is that many more complex behaviors once you've got that dialed in will just be automatic.