Collision response problem [solved]

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
Malebolge
Posts: 19
Joined: Sun Aug 31, 2008 8:24 pm

Collision response problem [solved]

Post 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
Last edited by Malebolge on Mon Oct 13, 2008 3:20 pm, edited 1 time in total.
rogerborg
Admin
Posts: 3590
Joined: Mon Oct 09, 2006 9:36 am
Location: Scotland - gonnae no slag aff mah Engleesh
Contact:

Post 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.
Please upload candidate patches to the tracker.
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
Acki
Posts: 3496
Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:

Post by Acki »

what does player->getLastPosition() do ???
while(!asleep) sheep++;
IrrExtensions:Image
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
Malebolge
Posts: 19
Joined: Sun Aug 31, 2008 8:24 pm

Post 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.
rogerborg
Admin
Posts: 3590
Joined: Mon Oct 09, 2006 9:36 am
Location: Scotland - gonnae no slag aff mah Engleesh
Contact:

Post 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.
Please upload candidate patches to the tracker.
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
Acki
Posts: 3496
Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:

Post 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 !!! :)
while(!asleep) sheep++;
IrrExtensions:Image
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
Post Reply