GameMaker: Studio

GameMaker: Studio

View Stats:
Shannon Oct 5, 2012 @ 5:52pm
Physics - Collision Detection for Jumping
Hello! I've been playing around with the physics capabilities and I now have a moving, jumping character (block) in a physics room complete with gravity, floors, and an 'red' block that is also physics-enabled and doesn't move on its own (it's just there to be pushed around for testing purposes). While I was tweaking the jumping animation I ran into a funny glitch and would appreciate any insight you can offer for solving it.

The glitch seems to occur when I check to see whether there is an object directly under my character so that it can jump. I've tried this two ways. This is the first:

if (physics_test_overlap(x, y+1, 0, all)) {
phy_linear_velocity_y = -100;

When I use this method, the character is able to zoom all over the room whether it is near any other object or not - in other words, the check doesn't seem to be working at all. For the second one, I made use of the "collision check" GUI object, which creates the following check:

if relative position (0,1) gives a collision with All objects

(The same line is used within this check to set the linear velocity.)
Using this second method, the object behaves appropriately and will only jump when it is on the ground; it cannot fly around the room. I can also jump on top of the red block object. However, if I move the red block in any way (such as pushing it around the room), I can no longer jump from on top of the red block, but I can jump from the location where the red block was before.

In conclusion, the first method is a physics check but is not working correctly and always returns "true" even if nothing is below the character object. The second method seems to be out of sync with physics and will only take into account the location of objects from the time that they were created; if those objects are moved by the physics engine, it will not update that location correctly and further collisions with that object will not be detected (or will be detected only at the object's prior location).

Thank you for reading this and any assistance would be appreciated!
Last edited by Shannon; Oct 5, 2012 @ 5:53pm
< >
Showing 1-8 of 8 comments
Mattchu Oct 5, 2012 @ 6:02pm 
Just today, I updated and reuploaded my physics example because I had to fix an issue that kind of goes in line with this.

This seems to be the full-proof way to do it, where +33 is one pixel more than the length/height of my player object (in my example, the origin is set to 0,0.

if (physics_test_overlap(x, y, 0, obj_block) && position_meeting(x, y + 33, obj_block)) { onGround = true; y = yprevious; } else if (physics_test_overlap(x, y, 0, obj_block) && position_meeting(x, y, obj_block)) { y = yprevious; } else if (physics_test_overlap(x, y, 0, obj_block) && (position_meeting(x + 32, y, obj_block) || position_meeting(x, y, obj_block))) { x = xprevious; }

Oh hey, code tags. Didn't know about that.

Anyways, what you want is specifically the first part...

if (physics_test_overlap(x, y, 0, obj_block) && position_meeting(x, y + 33, obj_block)) { onGround = true; y = yprevious; }

It should be pretty self-explanatory, but pretty much it's not enough to test for an overlap because the overlap tests every point on the block. I don't know the actual effect of modifying x and/or y when specifying a position to test, I just know that it tests the whole block when unmodified.

So, in addition to testing an overlap, you need to test whether or not your object will meet with the object that it's landing on/bumping into at the location specified by the overlap. This way you can give specific instructions based on where they meet.

EDIT: If that doesn't work, I might have to do some coding of my own with a movable block. Not really sure what could be going wrong.
Last edited by Mattchu; Oct 5, 2012 @ 6:06pm
Mattchu Oct 5, 2012 @ 6:22pm 
Okay, I tried it out and the moving block works...sort of. For some reason, when the block is rotated, the image somehow disconnects from the space where the block's collision testing is. And I can't figure out how to fix it. I tried setting image_angle to phy_rotation every step, but nothing. No idea. I had that issue when the player rotated, too.
Shannon Oct 5, 2012 @ 9:21pm 
Hey, thanks for the response! Your code got the physics_test_overlap function working but it's giving the same problem with the moveable block for some reason. I'm getting this error even when the blocks' rotations are fixed; if the red block is pushed to a different location at all, I cannot jump from on top of it. I will continue working with this and see if I can figure out what's up. Thanks again for the assistance though; at least now I can use a physics test for jumping.

EDIT: Just noticed, I seem to be able to jump off of the spot where my 'character' block begins as well, so it seems that neither object is being tracked correctly for one or both of the statements in the check.
Last edited by Shannon; Oct 5, 2012 @ 9:28pm
Mattchu Oct 5, 2012 @ 9:30pm 
Mine's working fine when the rotation is fixed. Well, sort of. Due to the nature of the code, onGround only is set to true of the left end of the player is over the block, as opposed to if it just lands on the block.

EDIT: Try changing your physics world update rate?
Last edited by Mattchu; Oct 5, 2012 @ 9:30pm
Shannon Oct 5, 2012 @ 9:46pm 
That I haven't tried. I'm guessing it's physics_world_update_speed and should be set in the creation code of the room? Also, any tips on what to set it to to get results? I tried setting it to twice the room speed as in the physics_world_update example but got no results.
Mattchu Oct 5, 2012 @ 9:55pm 
I don't know then. :/

I have iterations and update speed both set to my room speed.
Shannon Oct 5, 2012 @ 9:59pm 
Thanks anyway! I'll work with it more tomorrow and update if I find anything. I appreciate the help regardless.
Shannon Oct 6, 2012 @ 9:08am 
Made a few more discoveries. I think position_meeting, place_meeting, etc. don't always update with the physics engine, hence the weird error I've been getting; however, I think I have found my initial problem with physics_test_overlap.
if (physics_test_overlap(x, y+1, phy_rotation, all)) { phy_linear_velocity_y = -100; }

This was the code that made the block zoom all over the map. I finally realized that the 'all' keyword includes 'self', meaning the object is checking to see whether it will overlap with itself. I can make this stop by changing y+1 to y+33 to make the check occur outside of the sprite's box; however, even though the sprite's origin is set to the top left of the sprite (0,0) this change will cause the character block to jump even when it is 33 pixels away from the ground. I want this character block to be able to utilize any physics object present in the room so 'all' seemed like a good, clean keyword to use, but is there any way to subtract 'self' from it? I could always check for each object individually but I was hoping someone knew of a more elegant solution.

Also, Mattchu, curious to know whether replacing '0' with 'phy_rotation' would help your rotation issues at all? It might not make any difference, though; I can't test it myself yet since I'm still having the block issue.

Thanks for reading and sorry for asking so many questions!
< >
Showing 1-8 of 8 comments
Per page: 15 30 50

Date Posted: Oct 5, 2012 @ 5:52pm
Posts: 8