Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: arj0n on Sat 24/07/2010 09:18:00

Title: [SOLVED]Prevent object to move when another object is next to it .
Post by: arj0n on Sat 24/07/2010 09:18:00
Another question:

Say object1 moves only left or right and object2 can be in it's path.

How can check to prevent 2 objects colliding with each other?
Tried 'IsCollidingWithObject' but that doesn't prevent.
Title: Re: Prevent object to move when another object is next to it .
Post by: Joe on Sat 24/07/2010 23:33:13
Try something like this:

object1.Move(...);
if(object1.x>=object2.x && object1.y==object2.y)
 object1.x=object2.x;

//But if you want to make it better you should use with object1.width... aswell
if(object1.x+object1.width>=object2.x)
 if(object1.y<object2.y-object2.height && object1.y-object1.height>object2.y)
   object1.x=object2.x-object1.width;


Obviously... this is not tested
Title: Re: Prevent object to move when another object is next to it .
Post by: Dualnames on Sun 25/07/2010 07:54:33
I think this actually may work.


int detection=5;//How close in pixels should the objects be
Object*back; //if object1 is behind object 2
Object*forth; //if object2 is in front of object 1

function repeatedly_execute() {
back=Object.GetAtScreenXY(object[1].X-detection,object[1].Y);//I've used object[1] instead of object1, in case you want this globally, if you do put a player.Room check.
forth=Object.GetAtScreenXY(object[1].X+detection,object[1].Y);

if ((back!=null) || (forth!=null)) {
//prevent movement of objects here
}

}


EDIT: There's a chance you'll need something different than X property to get the actual width of the objects, like Game.SpriteWidth, so keep that in mind.
Title: Re: Prevent object to move when another object is next to it .
Post by: arj0n on Sun 25/07/2010 15:31:26
Thanx guys