Page 1 of 1

Collision response problem [solved]

Posted: Mon Oct 13, 2008 2:10 pm
by Malebolge
Hello again.

I am back with other newbie question. This time is collision response.
In my game, i control the character (from a top down perspective) with the arrow keys, around a city. The character is always looking to the mouse.
I needed that the character collide with the buldings, so i think using Bounding Boxes.
On the main loop, i keep the position of the character on a variable (lastPosition), then i read the input, so i can move the character accord.
The next thing is check the collision. If the character is in collision, its return to the last position.

The problem is that the character gets stuck in the bulding and can be move only if its is rotated (using the mouse).

obviusly, i am missing something.

This is the code of the coliision:

Code: Select all

bool 
Collision::collisionAABB(irr::scene::IAnimatedMeshSceneNode *n1, irr::scene::IMeshSceneNode *n2)
{
	core::aabbox3d<f32> box1 = n1->getTransformedBoundingBox(); 
	core::aabbox3d<f32> box2 = n2->getTransformedBoundingBox(); 


	if (box1.intersectsWithBox(box2))
		return true; 
	return false; 

} 

this is when i check the collision:

Code: Select all

for(unsigned int i =0;i<listProps.size();i++){
		scene::IMeshSceneNode* msn = (scene::IMeshSceneNode*)listProps[i]; 
		if(collisionAABB(player->getNode(),msn)){
			player->getNode()->setPosition(player->getLastPosition());
		}
	}
listProps is and array of the statics elements of the scene.

I thought the idea was right, but it still gets stuck in the bulding.

thanks

Posted: Mon Oct 13, 2008 2:38 pm
by rogerborg
I'd suspect that it's an issue with absolute positions. The code that you didn't post, is it by any chance something like this...

Code: Select all

player->LastPosition = player->getNode()->getAbsolutePosition();
player->getNode()->setPosition(someNewPosition);

//...
// Then do the collision check
If so, then the bounding box you'll get for player->getNode() inside collisionAABB() will be for the player's old position, since setPosition() only updates the relative position, and the absolute position isn't recalculated until a render is done, or you call... (here comes the possible solution)...

Code: Select all

// After the presumed call to player->getNode()->setPosition(someNewPosition);
player->getNode()->updateAbsolutePosition();
See if that sorts you out. If not, then it'd be handy to see more of your code so that we can identify the issue with specificity.

Posted: Mon Oct 13, 2008 2:46 pm
by Acki
what does player->getLastPosition() do ???

Posted: Mon Oct 13, 2008 2:52 pm
by Malebolge
Acki wrote:what does player->getLastPosition() do ???
It´s return the position of the node in the last frame. Sorry, i should explain it better before.

Code: Select all

player->getNode()->updateAbsolutePosition(); 
it fix the problem...i think i must be more careful next time.

Sorry for not post in a appropriate way. i am not used to post in forums.

thanks again to both.

Posted: Mon Oct 13, 2008 3:11 pm
by rogerborg
It's OK, that was fine. It's not an obvious problem, which is why it catches so many people out (...which is why I was able to make an informed guess at it). I'm glad to have been able to help.

Posted: Mon Oct 13, 2008 3:11 pm
by Acki
Malebolge wrote:
Acki wrote:what does player->getLastPosition() do ???
Sorry for not post in a appropriate way. i am not used to post in forums.
no need to be sorry !!!
it was a pleasure to read your post, realy well done !!! :)
you just forgot to show the getLastPosition function, which seemed to cause the problem... ;)
but rogerborg did the right guess !!! :lol:

looking forward to help you next time !!! :)