Crystal Space
9054 Posts in 2047 Topics by 74731 Members
Latest Member: Nicholroan441
 Pages: [1]
 Author Topic: Collision Detection - What's going wrong here?  (Read 4992 times)
bernardofd
Jr. Member

Posts: 74

 « on: June 02, 2005, 02:56:53 pm »

Hello all;

I having some troubles about doing collision detection. My code was based in the Maze Tutorial in Crystal Space, but, I don't know why, it's just do not collide! The source code, mazetut.cpp, works fine, but uses a different approach in movement with collisions. I, for now, have a more simple view. If the player collides in something, he just can't move. Thatś why the amount that the camera moves is atomic (or minimal).

Well, I'll show here my collision detection code:
Code:

Player::Player(ERD *erd) // Player Constructor
{
Player::erd = erd;
}

Player::~Player() //Player Destructor
{
}

bool Player::InitCollider() // Instatiate player collider mesh
{
// Create the Bounding Box for the Player
csPolygonMeshBox* box = new csPolygonMeshBox(csBox3(csVector3(1, -0.5, -0.5), csVector3(-1, 5, 0.5)));

//Create Player's Collider
player_collider = erd->GetCollisionDetectionSystem()->CreateCollider(box);
player_radius = 2; //In fact, the player's max radius is 1.0, but it is just for safety.

//Derreferences object 'box'
box->DecRef();

if (!player_collider)
return false;
else
return true;
}

bool Player::CanMove(const csVector3 &direction, float speed) // Detects collision in movement
{
//Get the camera and the transformation matrix
iCamera* cam = erd->GetView()->GetCamera();
csReversibleTransform cam_trans = cam->GetTransform();

//Get all nearby meshes that can possibly collide
csRef<iMeshWrapperIterator> it = erd->GetEngine()->GetNearbyMeshes(
cam->GetSector(), direction, player_radius + speed, true);

if (it->HasNext()) //We have nearby meshes... Collision is possible
{
iCollideSystem *cd_sys = erd->GetCollisionDetectionSystem();

cam_trans.SetOrigin(direction);
it->Reset(); //Just for sure ;-)

while (it->HasNext())
{
iMeshWrapper* mesh = it->Next();
csColliderWrapper* collide_wrap = csColliderWrapper::GetColliderWrapper (mesh->QueryObject ());
if (collide_wrap)
{
csReversibleTransform mesh_trans = mesh->GetMovable()->GetFullTransform();
cd_sys->ResetCollisionPairs(); //New collisions - Avoid continuous grow of the collision table
if (cd_sys->Collide(collide_wrap->GetCollider(), &mesh_trans, player_collider, &cam_trans))
{
//We have a collision! So the character can not move to the point
return false;
}
}
}
}

//No collisions at all... Movement is possible.
return true;
}

And here is how the camera moves:
Code:

if (erd->kbd->GetKeyState('d'))
if (player->CanMove(CS_VEC_RIGHT * 4 * speed, 4 * speed))
c->Move(CS_VEC_RIGHT * 4 * speed);
if (erd->kbd->GetKeyState('a'))
if (player->CanMove(CS_VEC_LEFT * 4 * speed, 4 * speed))
c->Move(CS_VEC_LEFT * 4 * speed);
if (erd->kbd->GetKeyState('w'))
if (player->CanMove(CS_VEC_FORWARD * 4 * speed, 4 * speed))
c->Move(CS_VEC_FORWARD * 4 * speed);
if (erd->kbd->GetKeyState('s'))
if (player->CanMove(CS_VEC_BACKWARD * 4 * speed, 4 * speed))
c->Move(CS_VEC_BACKWARD * 4 * speed);

I appreciate any help.

Regards;
 Logged

__________________________
Bernardo Figuerêdo Domingues
Computer Science - UFMG - Brazil
bernardofd
Jr. Member

Posts: 74

 « Reply #1 on: June 02, 2005, 03:11:29 pm »

And I forgot! The problem is the line:
Code:

if (cd_sys->Collide(collide_wrap->GetCollider(), &mesh_trans, player_collider, &cam_trans))

The Collide() finction always returns false, even if the player is inside the polygon! I don know what I can do...
 Logged

__________________________
Bernardo Figuerêdo Domingues
Computer Science - UFMG - Brazil
bernardofd
Jr. Member

Posts: 74

 « Reply #2 on: June 06, 2005, 02:53:52 pm »

Hello...

Although there are some views of the topic, nobody answered it. But I will update my progress so far anyway.

Well, the function Collide() returned true one time, when I changed the csPolygonMeshBox() parameters, as you'll see below:

Code:

csPolygonMeshBox* box = new csPolygonMeshBox(csBox3(csVector3(-1, -1, -1), csVector3(1, 1, 1)));

Testing my app with the partsys map, the camera got stuck in the ground. (The start position is a little over the ground. So, the polygon is cutting the player's box in a half, and then, only then, I got a collision.

But when I load another map that I do not start inside a polygon, I have no collision at all.

I thinked a little, and I want some clearness in this stuff. When I move the camera, the collider that I've created moves too?

Regards
 Logged

__________________________
Bernardo Figuerêdo Domingues
Computer Science - UFMG - Brazil
deckerego
Full Member

Posts: 149

 « Reply #3 on: June 06, 2005, 08:15:58 pm »

So much of the collision detection stuff I do is now in CEL... but if I remember correctly you have to move the bounding box... moving the camera doesn't do anything for collision detection.
 Logged
bernardofd
Jr. Member

Posts: 74

 « Reply #4 on: June 07, 2005, 02:00:00 pm »

Ok. That was what I was suspecting. But, now in a practical way, how should I do it? I have searched a little through the CS's API Reference, but I haven't found nothing yet.

And can you give me an quick description-how-to-use-little-tour of CEL?

Regards
 Logged

__________________________
Bernardo Figuerêdo Domingues
Computer Science - UFMG - Brazil
bernardofd
Jr. Member

Posts: 74

 « Reply #5 on: June 18, 2005, 12:43:52 am »

Well, I researched a little, and the only function that I found that specifically moves an mesh is implemented in class iMovable.

My question now is, how can I reach an iMovable object from an iCollider (or an csPolygonMeshBox) I didn't see any connections between these two...

 Logged

__________________________
Bernardo Figuerêdo Domingues
Computer Science - UFMG - Brazil
jorrit
Hero Member

Posts: 1706

 « Reply #6 on: June 29, 2005, 11:42:15 am »

Quote from: bernardofd
Well, I researched a little, and the only function that I found that specifically moves an mesh is implemented in class iMovable.

My question now is, how can I reach an iMovable object from an iCollider (or an csPolygonMeshBox) I didn't see any connections between these two...

You can't. Why would you need to do that?

Greetings,
 Logged
bernardofd
Jr. Member

Posts: 74

 « Reply #7 on: July 04, 2005, 10:23:06 pm »

Well... Because I want to move the player's Bounding Box... Is there any way?
 Logged

__________________________
Bernardo Figuerêdo Domingues
Computer Science - UFMG - Brazil
jorrit
Hero Member

Posts: 1706

 « Reply #8 on: July 08, 2005, 02:07:51 pm »

Well... Because I want to move the player's Bounding Box... Is there any way?

You can go from iMeshWrapper to iMovable and iCollider. Not the other way around. So that's what you need to do.

Greetings,
 Logged
bernardofd
Jr. Member

Posts: 74

 « Reply #9 on: July 11, 2005, 07:41:30 pm »

Ok. So, next step is:

How do I create the player Bounding Box using something turnable in an iMeshWrapper?

Regards...
 Logged

__________________________
Bernardo Figuerêdo Domingues
Computer Science - UFMG - Brazil
bernardofd
Jr. Member

Posts: 74

 « Reply #10 on: July 15, 2005, 04:32:01 pm »

OR

Hot to turn the csPolygonMeshBox into something that could be linked by an iMeshWrapper?

And I would appreciate some code samples, please.

Regards;
 Logged

__________________________
Bernardo Figuerêdo Domingues
Computer Science - UFMG - Brazil
 Pages: [1]