Jump to content


Photo

Collision Detection Codes Needed


  • Please log in to reply
1 reply to this topic

#1 hydroxy

hydroxy

    GMC Member

  • GMC Member
  • 223 posts
  • Version:GM:Studio

Posted 11 April 2011 - 10:57 PM

I need perfect collision detection for my game. My character is currently a 16x16 pixel box who moves around a 16x16 box world. For my game I am currently using a set of codes I made myself shown below:

//solid object collisions

if vspeed > 0 and ((collision_rectangle(bbox_left,bbox_bottom,bbox_right,bbox_bottom,obj_solid,1,1)) or (collision_rectangle(bbox_left,bbox_bottom,bbox_right,bbox_bottom+vspeed,obj_solid,1,1)))
    {move_contact_solid(270, 8) move_outside_solid(90,4) gravity =0  vspeed = 0}

if vspeed < 0 and ((collision_rectangle(bbox_left,bbox_top,bbox_right,bbox_top,obj_solid,1,1)) or (collision_rectangle(bbox_left,bbox_top+vspeed,bbox_right,bbox_top,obj_solid,1,1)))
    {move_contact_solid(90, 8) move_outside_solid(270,4) vspeed = 0}

if hspeed > 0 and ((collision_rectangle(bbox_right,bbox_top,bbox_right,bbox_bottom,obj_solid,1,1)) or (collision_rectangle(bbox_right,bbox_top,bbox_right+hspeed,bbox_bottom,obj_solid,1,1)))
    {move_contact_solid(0, 8) move_outside_solid(180,4) hspeed = 0}

if hspeed < 0 and ((collision_rectangle(bbox_left,bbox_top,bbox_left,bbox_bottom,obj_solid,1,1)) or (collision_rectangle(bbox_left+hspeed,bbox_top,bbox_left,bbox_bottom,obj_solid,1,1)))
    {move_contact_solid(180, 8) move_outside_solid(0,4) hspeed = 0}


These codes sometimes cause errors like the player getting stuck 1 or 2 pixels into a wall (obj_solid), I need him to not ever do this. What good alternatives could I use to my own codes?
  • 0

#2 creativebunch

creativebunch

    The Bunchiest

  • GMC Member
  • 886 posts
  • Version:GM:Studio

Posted 11 April 2011 - 11:18 PM

I have this code from an old example, can't remember who made it though:
//This is a nice code that handels collisions from all sides
//Basically it stops the ball from moving through the blocks, while allowing it to slide across them
//Using the  place_free function and +-1 to the x or y checks the collision by moving the mask of the object. Not the origin.

if (vspeed < 0 && not place_free(x,y+vspeed-1)) //Up collisions: Is is moving up and is there something above it?
{y=yprevious move_contact(90); vspeed = 0;} //Move up as much as possible and stop moving up completely

if (vspeed > 0 && not place_free(x,y+vspeed+1)) //Down collisions: Is is moving down and is there something below it?
{y=yprevious move_contact(270); vspeed = 0;} //Move down as much as possible and stop moving up completely

if (hspeed < 0 && not place_free(x+hspeed-1,y)) //Left collisions: Is is moving left and is there something to the left?
{x=xprevious move_contact(180); hspeed = 0;} //Move to the left as much as possible and stop moving left completely

if (hspeed > 0 && not place_free(x+hspeed+1,y)) //Right collisions: Is is moving right and is there something to the right?
{x=xprevious move_contact(0); hspeed = 0;} //Move to the right as much as possible and stop moving right completely

  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users