• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Crashing when colliding

Sometimes the car enters the object and gets stuck. How to make the car not enter solid objects in all collisions? See Attach file!

GML:
// collision 

   if place_meeting(x, y, Casa) || place_meeting(x, y, Casaa) || place_meeting(x, y, Casab) || place_meeting(x, y, Forest) || place_meeting(x, y, Object12)
    {
    speed = 0;
    }
// direction

if keyboard_check(ord("A")) && !place_meeting(x, y, Casa) && !place_meeting(x, y, Casaa) && !place_meeting(x, y, Casab) && !place_meeting(x, y, Forest) && !place_meeting(x, y, Object12)
    {
    image_angle = image_angle + 5;
    }
  
    if keyboard_check(ord("D")) && !place_meeting(x, y, Casa) && !place_meeting(x, y, Casaa) && !place_meeting(x, y, Casab) && !place_meeting(x, y, Forest) && !place_meeting(x, y, Object12)
    {
    image_angle = image_angle - 5;
    }
  
    direction = image_angle - 270;

    }
 

Attachments

ThraxxMedia

Member
Well... I don't really see why you wouldn't "get stuck" with this code. You're clearly telling your car to stop in its tracks whenever it hits an obstacle, and nothing else seems to happen. And since you're not moving anymore, you obviously also cannot move out of the area where the collision happened in the first place - so I assume the collision is just happening over and over again.

What you want to do is: detect a collision, and if it happens, move the player (i.e. your car) back to a position where it's not colliding anymore.
 
Maybe the mask is a little off with the image_angle. I think the problem is with the mask and the image_angle. Sometimes someone experienced sees a solution. Another try

GML:
 if place_meeting(x, y, Casa) || place_meeting(x, y, Casaa) || place_meeting(x, y, Casab) || place_meeting(x, y, Forest) || place_meeting(x, y, Object12)
    {
    if hspeed >= 0
    {
    hspeed = -hspeed;
    }
    if vspeed >= 0
    {
    vspeed = -vspeed;
    }
    }
 
Last edited:

chamaeleon

Member
The main issue with the code appears to be the fact that you are performing collision tests that test whether something has already happened or is true, whereas in most cases you want to program collision logic in such a way that you check to see *if* there would be a collision *if* you were to perform some movement or rotation. This is why most collision code posted contains additions to x and y for the movement part, in order to test a potentially new position and not go there if it would result in a collision. For rotation, you could try performing the image_angle update, test collision, and if you collide undo the image_angle update (or make a smaller update).
 
Top