Advanced Collision Code Platformer

I

Intergalakti

Guest
Hi,

I'm programming with GameMaker for about five years now. But all the time, when I was making a platformer, I had a problem: I don't know how to make a really good collision code. I currently am using the following code (simplified):

Code:
if (!place_free(x+hspeed,y))
{
    if (hspeed<=0){move_contact_solid(180,abs(hspeed));}
    if (hspeed>0){move_contact_solid(0,abs(hspeed));}
    hspeed=0;
}
 
if (!place_free(x,y+vspeed))
{
    if (vspeed<=0){move_contact_solid(90,abs(vspeed));}
    if (vspeed>0){move_contact_solid(270,abs(vspeed));djump=1}
    vspeed=0;
}
   
if place_free(x+hspeed,y+vspeed) == false
    hspeed=0
The problem with this code is that if I want to check for collision with the player in another object, I cant because the player stops just before he hits the wall. I tried to do make the objects that needed such a collision event non-solid and make themselves solid when the player hits them, but that works only once and isn't elegant at all. So I just wanted to ask the advanced programmers here how they do it. Thanks for your help!

Intergalakti
 

Roa

Member
@Intergalakti
Don't use solids.

ever

period.

If you really have 5 years, you should have enough experience to know this is where the problem lies. Forget they exist and never touch them again.

don't use place_free() either, because that checks all instances. What you want to do is check for instances using place_meeting(). This checks for a specific type of object in a collision with your current mask in relation to the cords. You should have a parent object to handle the collisions with, and every type of collision object and platform should inherit from that object. Start by trying these features out.
 
Last edited:

jazzzar

Member
@Intergalakti
Don't use solids.

ever

period.

If you really have 5 years, you should have enough experience to know this is where the problem lies. Forget they exist and never touch them again.

don't use place_free() either, because that checks all instances. What you want to do is check for instances using place_meeting(). This checks for a specific type of object in a collision with your current mask in relation to the cords. You should have a parent object to handle the collisions with, and every type of collision object and platform should inherit from that object. Start by trying these features out.
as he said, NEVER USE SOLIDS, the rest is true and i'' give you a really simple code about vertical collision :
Code:
if place_meeting(x,y+vsp,oWall)
{
      while !place_meeting(x,y+sign(vsp),oWall)
      {
           y+=sign(vsp);
      }
vsp=0;
}
y+=vsp;
//vsp is the vertical speed, that's how i go about it
//i put vsp to 0 in create event and increase it this way
if vsp < 12
   vsp+=0.1;//whatever here is the power of gravity
else vsp=12;
 
I

Intergalakti

Guest
Hi,

Thanks for your help! I altered by code so it doesn't use the solid, place_free and move_against_solid any more. Well, I don't exactly understand why that is so "evil", but I'm going to research a bit about that. Anyway, the result is exactly the same: If I make a collision event in an object the player walks over, nothing happens because the player just does not touch it. So what can I do about that?
 
I

Intergalakti

Guest
Ok, now I know why I shouldn't use solid and I found a solution for my problem, though it isn't very elegant:

Code:
if ( place_meeting(x+1,y,spieler)or place_meeting(x-1,y,spieler) or place_meeting(x,y+1,spieler) or place_meeting(x,y-1,spieler))
    spieler.ohspeed = 5
Thats the step event of the other object. But are there any more elegant ways?

@Rafsun Thanks!
 
Top