[SOLVED] raycast and collision filtering

Mike_Hoff

25-02-2015 01:39:40

Hello everyone,
I have troubles getting a collision filtering on a raycast to work, and was hoping that someone might point out what I am missing.
The code works perfectly if I don't use any masks, but the raycast collides occasionally with the player (depending on the animation state), so I started to use masks.
I can still walk my character in the environment und collide with other characters as well as with the level itself, but as soon as I used the filtering masks the raycast code didn't return any hits anymore (even if I set the raycast mask on -1).
(I have also tried setting a manual raycast which should return something and that didn't get any results either)

Short Version:
As soon as I use masks, the raycast doesn't return any results anymore.


Raycast code:

...
Ogre::Ray ray = Ogre::Ray(sword_pos, sword_direction);
OgreBulletCollisions::CollisionClosestRayResultCallback callBackObject(ray, m_world, m_sword_length);
m_world->launchRay(callBackObject, player_CollidesWith);

if(callBackObject.doesCollide())
{
printf("Hit: %s\n",callBackObject.getCollidedObject()->getName().c_str());
}
...



Applying a collisionGroup and collisionMask on the character

colBody = new OgreBulletDynamics::RigidBody("Player", world, COL_PLAYER, player_CollidesWith);


Applying a collision Group and collision Mask on the level

// Create the rigid body
OgreBulletDynamics::RigidBody *body = new OgreBulletDynamics::RigidBody("level_"+ pParent->getName(),
m_pWorld,
COL_LEVEL, level_CollidesWith);


Masks:

// collisionGroup
#define BIT(x) (1<<(x))
enum collisiontypes {
COL_NOTHING = 0, //<Collide with nothing
COL_PLAYER = BIT(1), //<Collide with player
COL_LEVEL = BIT(2), //<Collide with walls
COL_ENEMY = BIT(3), //<Collide with enemies
COL_OBJ = BIT(4) //<Collide with objects (health potions)
};


// collisionMask

static short player_CollidesWith = COL_LEVEL | COL_ENEMY | COL_OBJ; // player collides with level and enemies and objects
static short level_CollidesWith = COL_PLAYER | COL_ENEMY | COL_OBJ; // level collides with every Object moving on it
static short enemy_CollidesWith = COL_PLAYER | COL_LEVEL | COL_OBJ | COL_ENEMY; // Enemies collide also with themselves




I appreciate any help on this matter!
Thank you very much in advance!

Greetings
Mike

Mike_Hoff

02-03-2015 12:53:49

I solved it by setting the bits via numbers rather than using a bit shifts:


// #define BIT(x) (1<<(x))

enum collisiontypes {
COL_NOTHING = 0, //<Collide with nothing
COL_PLAYER = 1, //BIT(1), //<Collide with player
COL_LEVEL = 2, //BIT(2), //<Collide with walls
COL_ENEMY = 4, //BIT(3), //<Collide with enemies
COL_OBJ = 8, //BIT(4), //<Collide with objects (health potions)
COL_SWORD = 16, //BIT(5), //Collide with Player Sword
};