Page 1 of 1

Collision trouble

Posted: Mon Sep 27, 2010 1:00 pm
by N64vSNES
So I'm trying to rewrite my collision system for my tiles and here is what I've done

I've added a new function that takes a pointer to a vector like this

Code: Select all


bool IsCollision(ENG_Rectangle *a, ENG_Rectangle *b, Vector2f *normal) {

 /* Since the vector is a pointer then it could have been
	intialized to any value so we need to make sure we initalize
	the vectors X and Y to zero */
	normal->x = normal->y = 0.0f;

	// The distance between the two objects
	Vector2f Distance;
	// The absDistance between the objects
	Vector2f absDistance;

	// Calculate the distance between A and B
	Distance.x = ( b->x - a->x );
	Distance.y = ( b->y - a->y );

	// Combine both rectangles and half the returned value
	float XAdd = ( b->w + a->w ) / 2.0f;
	float YAdd = ( b->h + a->h ) / 2.0f;

	// Check if the Distance vector is below 0.0f
	( Distance.x < 0.0f ) ? absDistance.x = Distance.x * -1 : absDistance.x = Distance.x;
	( Distance.y < 0.0f ) ? absDistance.y = Distance.y * -1 : absDistance.y = Distance.y;

	 /*If the absDistance X is less than X add and the absDistance is less thank YAdd
	 then it dosen't take a genius to figure out they arn't colliding so return false*/
	if( ! ( ( absDistance.x < XAdd ) && ( absDistance.y < YAdd ) ) ) {	
	return false;
	}

	/*Get the magnitute by the overlap of the two rectangles*/
	float XMagnitute = XAdd - absDistance.x;
	float YMagnitute = YAdd - absDistance.y;

	/*Determin what axis we need to act on based on the overlap*/
	if( XMagnitute < YMagnitute ) {
      normal->x = ( Distance.x < 0) ? XMagnitute : -XMagnitute;
        }
       else {
      normal->y = ( Distance.y < 0) ? YMagnitute : -YMagnitute;
       }

	// If we reached this point then we now know the was a collision
	return true;
}

BTW: Thanks Falco for you're post in a similar thread.

Now I'm sure I'm missing somthing obviouse here but lets say I'm walking to the right and I'm walking into some tiles this prevents the player from entering the tiles but if I press right and down then I slide down still not entering the tiles however if I walk right and up then I don't enter the tiles but I don't slide up them either.

But if I walk down into the tiles and left or right then the player slides across the tiles in either direction no problem.

But if I change the end of the function from this:

Code: Select all

/*Determin what axis we need to act on based on the overlap*/
	if( XMagnitute < YMagnitute ) {
      normal->x = ( Distance.x < 0) ? XMagnitute : -XMagnitute;
        }
       else {
      normal->y = ( Distance.y < 0) ? YMagnitute : -YMagnitute;
       }
to this:

Code: Select all

/*Determin what axis we need to act on based on the overlap*/
	if( XMagnitute > YMagnitute ) {
      normal->y = ( Distance.y < 0) ? YMagnitute : -YMagnitute;
        }
       else {
     normal->x = ( Distance.x < 0) ? XMagnitute : -XMagnitute;
       }
Then this problem occurs when heading down into the tiles and not when heading right and up or left and up.

I hope I explained that ok.

Any questions or better yet solutions?

Re: Collision trouble

Posted: Tue Sep 28, 2010 4:12 am
by N64vSNES
Nobody?

Re: Collision trouble

Posted: Tue Sep 28, 2010 8:42 am
by dandymcgee
I spent hours upon hours trying to make 2D AABB collision work in my own game, which is enough for me to never want to look at it again. Unless you really need it for physics of some sort, I'd recommend you just use standard bounding box collision with rectangles rather than the Separating Axis Theorem.

P.S. Try IRC channel (#elysian_shadows @ irc.freenode.net), if you get lucky someone there will be willing to help.

Re: Collision trouble

Posted: Tue Sep 28, 2010 11:27 am
by N64vSNES
dandymcgee wrote:I spent hours upon hours trying to make 2D AABB collision work in my own game, which is enough for me to never want to look at it again. Unless you really need it for physics of some sort, I'd recommend you just use standard bounding box collision with rectangles rather than the Separating Axis Theorem.

P.S. Try IRC channel (#elysian_shadows @ irc.freenode.net), if you get lucky someone there will be willing to help.

Thanks for the post.

But luckily I fixed it earlier, to be honest I don't remember the problem I think it was somthing to do with the converting the values from negetive to positive.

Thanks anyway =]

Re: Collision trouble

Posted: Tue Sep 28, 2010 1:24 pm
by dandymcgee
N64vSNES wrote: Thanks for the post.

But luckily I fixed it earlier, to be honest I don't remember the problem I think it was somthing to do with the converting the values from negetive to positive.

Thanks anyway =]
Awesome, something to think about in the future though. ;)