Forums

Having Collision Issues? Look here first.

110 posts, 42 voices

 

Jul 8, 2010 2:29am

16 Ton

QuoteAbout 30 - 40% of the questions that get asked here have to do with the same type of problem: Collisions (usually for platform games). Here is a list of the most common problems encountered with collisions, and how to fix them.

• Character sticks to things easily, gets stuck and can't move.
There are two possible reasons for this.
1. You have your character set to solid. A general rule of thumb is to only set objects that never move to solid.
2. Make sure your character is not inside a wall (Use place_free(x,y) to check this).

• Character sticks when changing direction, or repeatedly sticks/unsticks.
This is most likely caused by precise collision checking being enabled. Open up your character's sprite and disable precise collision checking. Also, if your character uses multiple sprites, make sure that they all have the same bounding box (see the GM help file on how to set the bounding box).

• Character stops one or two pixels away from walls.
This is probably caused by one of two things.
1. You have your bounding box on your character sprite too big.
2. Your walls are set to solid.
Bounding boxes are easy to fix. Read the GM help file.
When you set walls to solid, they automatically move your character to xprevious, yprevious when there is a collision, and then execute your collision code. Since your character doesn't move in perfect 1px increments, you have to tell it to move up against the wall. This can be done by inserting this code
Code

move_contact_solid(direction,speed);
into either your collision with wall event, or your keyboard events, depending on how your movement system is set up.

• Character sticks to walls when I jump towards one.
The most common reason for this is that you are not distinguishing between a collision with the side of a wall block, and the top of a wall block. The easiest way to do this is to check whether there is a block under you.
Code
//collision event of character with block

if place_free(x,y+4) {
//code for collision with side of wall
//Here, for instance, you would not set vspeed to 0, but hspeed to 0
} else {
//code for collision with top of wall
//here, for instance, you could set vspeed to 0.
}
If that doesn't work, you should also try to check for a wall in front of you before you move, so in your move events, use the place_free(x,y) function to check if there's a wall at the place you're going to move to (add to the x coordinate to check to the right, subtract to check to the left).

• Character won't move when pressing key that moves towards a wall.
This is probably because you aren't checking whether the place you're going to move to is empty first. For a system where you move using x and y, instead of vspeed and hspeed, this is easy to fix. Simply use place_free(x,y) (like in the code above) to check whether there is a wall next to you before you move.

• Character 'skips' right before hitting the floor.
Most likely, you need to add a move_contact_solid(270,12) in your collision code.

• Bullet (or other object) misses collision with enemy/object.
This is most likely because your bullet's speed it too great. If the bullet is moving to fast, it can skip over objects. A simple fix is to use collision_line to check the distance between your last and current location.
Code
//End Step event of bullet object

if collision_line(x,y,xprevious,yprevious,insert collision object here,false,true) {
x=xprevious;
y=yprevious;
move_contact_all(direction,speed);
event_perform(ev_collision,insert collision object here);
}

• How do I do jump-through platforms?
Jump-through platforms are a bit tricky. I suggest this tutorial here.

• How do I make my character slide along walls (top-down)?
This is actually pretty easy. When you collide with a wall, you figure out how you are colliding with it, and then move outside of it accordingly. I would put the code below in a script that you call on collision.
Code

if collision_rectangle(bbox_left-speed, bbox_top,bbox_right+speed,bbox_bottom,obj_wall,false,false) {
if bbox_left<other.bbox_right+speed then move_contact_solid(180,speed);
else if bbox_right>other.bbox_left-speed then move_contact_solid(0,speed);
} else {
if bbox_top<other.bbox_bottom+speed then move_contact_solid(90,speed);
else if bbox_bottom>other.bbox_top-speed then move_contact_solid(270,speed);
}


More to come...
Please leave suggestions as to any other problems I should address, and I will try to get to them. Or, if you can't find your problem above, leave a description of your problem here, and I will try and address it.

 

Jul 8, 2010 2:36am

16 Ton

I'm having problems with collitions in a game I'm making. In this game, depth=-y, but this meens that in order to collide, objects need to have the exact same y. Is there any way to make the objects colllide with anything that has a depth +- 5 of it's own, so an object with a depth of 0 would collide with any obect with a depth between -5 and 5?

 

Jul 8, 2010 5:40pm

elfdud7

sure.
the code is as follows:

in collision
if abs(other.depth-depth)<5
//do action!


^if you like it, tell me!^

 

Jul 8, 2010 6:38pm

creatorofchaos

@ 16 ton

my character isn't set to solid, every player sprite has the exact same bounding box, I use place_free in my code, and precise collision isn't checked, maybe it's a problem with my players movement code>it checks if place_free x+3, for right and place_free x-3 for left,then moves them 3 pixels over, and when they jump it sets vspeed the vspeed and gravity.

 

Jul 9, 2010 7:51am

16 Ton

Quotesure.
the code is as follows:

in collision
if abs(other.depth-depth)<5
//do action!


Thanks a lot. I'll try this. By the way, what does abs meen?

 

Jul 10, 2010 4:57am

elfdud7

sure thing 16 ton! abs stands for absolute value. so basically if the value is negative it converts it to positive.


^if you like it, tell me!^

 

Aug 6, 2010 6:02pm

Lokarin

Minor collision problem.

Since my walls are not solid (required since characters can jump up through them) I can't use move_contact_solid.

move_contact_all does give satisfying results... EXCEPT when player two (or any other object) happens to be occupying the same square. This makes wall jumping in two player mode virtually impossible.

Is there a way to fake a move_contact_specificobject command?

EDIT: Looks like more than my wall clipping will require move_contact_specificobject. Isn't it just, like, a For...Next statement or something?

EDIT2: Solved it, yay move_contact_object!

 

Aug 13, 2010 9:06am

JARH3AD

if collision_rectangle(bbox_left-speed, bbox_top,bbox_right+speed,bbox_bottom,obj_wall,false,false) { if bbox_left<other.bbox_right+speed then move_contact_solid(180,speed); else if bbox_right>other.bbox_left-speed then move_contact_solid(0,speed);} else { if bbox_top<other.bbox_bottom+speed then move_contact_solid(90,speed); else if bbox_bottom>other.bbox_top-speed then move_contact_solid(270,speed);}

only works on 2 sides of the wall and I can't figure out why, and and when the object slides along the wall it keeps moving back and forth, please help me

 

Aug 16, 2010 3:17pm

Hevion

hey people my character picks and throws stuff. i have figured out the carrying and throwing but when i throw the object at the wall i want it to bounce. sometimes it does sometimes it just sticks there. i dont understand y. collision with floor is perfect but sides not so much. walls are solids but the object im throwing is not. im using place_meeting and i tried place free but both do the same exact thing.
also coins fly when enemies die and sometimes they bounce off walls other times they stick. i cant make it work. even the character when hit he flies back and is supposed to change direction when hitting a wall and bounce off it. sometimes works sometimes sticks and ruins everything. collision with floor is always perfect.
what can i do? would extreme physics help?

 

Sep 9, 2010 7:51am

Frolacosta

I just found something you can add. If you put the collision code in the object that isnt moving, the object will stick to it. Not sure if its in there already though.

Everything slowly falls apart around me...

 

Oct 4, 2010 5:20pm

MegasGamer

For some reason my character (which is a simple person-shaped blue thing for engine testing) keeps falling through the floor even when the floor is set to solid.

 

Oct 4, 2010 6:19pm

A saurus1

Does he have any collision events with the floor? If not, then game maker is not going to check for a collision, so he'll just go right through.



Yes, it's here!

 

Oct 4, 2010 7:01pm

MegasGamer

QuoteDoes he have any collision events with the floor? If not, then game maker is not going to check for a collision, so he'll just go right through.

Well, not as a D&D action, I'm trying to set it up in GML.

Sooner or later I would like to design a collisions engine for the platformer genre that makes it so the only solid objects (to making collision checking more efficient) are terrain objects around the player's (excluding enemies) 8 directions (for side-scrollers).

 

Oct 5, 2010 9:29am

TheUglySoldier

In my top-down shooter, certain areas are not visible to the player until he enters them through a door, appearing with a black "fog of war" over the areas. These areas become covered again once the character leaves them.

I figured the best way to do this is to have objects which cover the areas and are sprited as big black shapes. When the player colides with the shape (entering the area), it becomes invisible, revealing all the objects in the area. When the player is no longer colliding with it, the area becomes black again. The same method I hope to use for doors - when the player collides with them a sound plays and they become invisible, giving the apperance of them being opened.

So I've figured the best way is via a step event - every step the object checks if it's colliding with the player, and if so becomes invisible. If not, it becomes visible. However, I'm having trouble actually coding this - all codes seem to want to specify certain points, although I want any collision at all - be it the edge of the black area or the centre of it. I'm having similiar problems with other objects, where the collision event only seems to occur if the player touches a specific point of the object. Any ideas on how I go about remedying these?

 

Oct 5, 2010 11:24am

A saurus1

the collision_*() functions only allow you to specify certain shapes to check for collisions, which is normally fine (just check your player's bounding box). The place_*() collision functions check whether your player meets a free or empty space, or meets another object when placed at the position specified. Normally, you want to use some value relative to the player's x and y.



Yes, it's here!

 

Oct 7, 2010 12:04am

Untaken

Major problem that's been bugging me for a while. I'm trying to get an 8 direction isometric area to work by using diamonds around an area. I've tried various functions (place_free, place_meeting, move_contact_solid, collision_line) and the collisions either don't work or stick in the diamond and can't get back out. Precise checking's off and the problem happens regardless.

The code for movement is one large block but each section asks if the player can move without colliding with anything: and what button's pressed:

if keyboard_check(vk_down) and place_meeting(x,y+vspeed,objOWBoundary)=false
{//Starts block
vspeed=2
sprite_index=OWMove5

 

Oct 8, 2010 10:40pm

Ultigonio

Honestly, I'm not really sure where to even start with the wall collision issue I'm having.

So, I want my character to move sort of like in a top-down RPG map screen or whatever, but with gradual speed-up and slow-down (max speed is 10, there is also a downwards gravity of .2). That works fine. Now, I need him to slide along the walls properly. I saw the code given for that, but that alone won't work. I'd just like to know what to do from the very start. If someone could explain it or point me in the right direction with it, that'd be very helpful.

 

Oct 15, 2010 2:08pm

TheUglySoldier

Got a colision/speed problem going on.

For motorbike driving levels of the game, there is a road the player must obviously stick to. This is boardered by an invisible object (simply called "boarder") which stops the player from leaving the road, via turning it's vspeed to 0 upon colision. Problem is, it's also stopping their hspeed, and I have no idea why!

 

Oct 15, 2010 4:42pm

A saurus1

Neither do we, if we can't see your code.



Yes, it's here!

 

Oct 18, 2010 11:34am

TheUglySoldier

Heheh, my bad! For the "boarder" object in the step event:

if collision_rectangle(0,54,101,52,player_bike,false,false)
player_bike.vspeed = 0
else
exit

For the record, upon starting, the player has a hspeed of about 20, holding "D" will increase it to 30, holding "A" will bring it down to 10. "W" sets the vspeed to -10, whilst "S" will set it to 10. The boarder is solid.

Update: Okay...now no stopping is occuring at all...not sure what I've done there...

 

Oct 24, 2010 4:41am

tony10997

I put a gravity on my character, but everytime i enter the game, he falls through the ground, how do i fix this?

 

Nov 10, 2010 9:02pm

dr_epica

Well this helped me a bit after I used the move_contact_solid function. But I want my character to only do that when it is touching the top of a platform, not the side or bottom. When I try to fix it, I get back into the whole mess of getting stuck in platforms.

Tony10997: Set your platforms to solid. Then in the event for collision with platform, put this:

if place_free(x,y+1)
{
move_contact_solid(270,-1)
}

they thought Bill Gates was a nerd too

 

Nov 28, 2010 7:48am

NukeOTron

Boy, this one might be a challenge...
The following is for a game engine with a character who moves with momentum. However, the character has a hard time walking up slopes...
...Oh, and if it helps, the slopes are solid blocks, drawn to look diagonal. Why does the character get stuck once in a while when walking up the slopes? It's obviously a collision detection problem...

The Ninja game is working!

 

Nov 28, 2010 7:55am

Brawl

Yes it is. But I don't know what the problem is because you didn't tell us the codes that you are currently using. I can't just see your codes and know what the problem it. Only a select few here can do that. We call them hackers.



"We vanquished the dinosaurs with the careless trajectory of a child's lost meteorite."

 

Feb 21, 2011 9:44pm

Anonymous Anarchy

I know this is kind of a bump, but you really need to add one about the problem with the circle character. And the square floor. As in the circle gets kind of stuck on the corner of the floor because of it's shape.