Page 1 of 1

check collision masks when colliding

Posted: Fri Jun 16, 2017 8:53 am
by Salocin808
Hi!

I have a spaceship an cubes. I am adding those to the dynamicsWorld like so:

Code: Select all

#define BIT(x) (1<<(x))
enum collisiontypes {
	COL_NOTHING = 0, //<Collide with nothing
	COL_GROUND = BIT(0), //<Collide with ground
	COL_WALL = BIT(1), //<Collide with walls
	COL_CUBE = BIT(2), //<Collide with powerups
	COL_SHIP = BIT(3) //<Collide with ship
};

int shipCollidesWith = COL_WALL | COL_CUBE;
int wallCollidesWith = COL_SHIP;
int cubeCollidesWith = COL_SHIP;
int groundCollidesWith = COL_NOTHING;

Code: Select all

colShapeCube = new btBoxShape(btVector3(edge_length, edge_length, edge_length));
Physics::addCollisionShape(colShapeCube, px, py, pz, mass, COL_CUBE, cubeCollidesWith);

Code: Select all


btRigidBody* Physics::addCollisionShape(btCollisionShape* colShape, float px, float py, float pz, float mass, int group, int mask)
{
	btTransform startTransform;
	startTransform.setIdentity();

	btScalar tMass(mass);

	//rigidbody is dynamic if and only if mass is non zero, otherwise static
	bool isDynamic = (tMass != 0.f);

	btVector3 localInertia(0, 0, 0);
	if (isDynamic)
		colShape->calculateLocalInertia(tMass, localInertia);

	startTransform.setOrigin(btVector3(px, py, pz));

	//using motionstate is recommended, it provides interpolation capabilities, and only synchronizes 'active' objects
	btDefaultMotionState* myMotionState = new btDefaultMotionState(startTransform);
	btRigidBody::btRigidBodyConstructionInfo rbInfo(tMass, myMotionState, colShape, localInertia);
	btRigidBody* body = new btRigidBody(rbInfo);
	dynamicsWorld->addRigidBody(body,group,mask);
	//collision
	return body;
}
Now i want to check for collisions between the ship and the cube. Currently I am iterating over manifolds (wich does not seem to work perfectly yet because sometimes my ship collides even though there is nothing to collide with). How can I read the group/mask to see if it collided with a cube? Or is there generally a better way to do this?

Thanks in advance!

Cheers

Re: check collision masks when colliding

Posted: Fri Jun 16, 2017 9:11 am
by Salocin808
Ok, never mind... it already works. I am quite new to this. If I change the shipCollidesWith variable, no collisions are detected.

But still there is still the problem that the collision is detected way too early and I have no idea why..

Re: check collision masks when colliding

Posted: Fri Jun 16, 2017 12:26 pm
by S1L3nCe
Hi,

What do you mean by "too early" ?

Re: check collision masks when colliding

Posted: Sat Jun 17, 2017 4:10 pm
by Salocin808
S1L3nCe wrote:Hi,

What do you mean by "too early" ?
The problem can be seen in the picture. This is already a collision and I don't know why.. The collision is happening with the cactus. Cubes are working fine -> collision with spaceship only detected when actually collided.
Any ideas?

Cheers

Re: check collision masks when colliding

Posted: Mon Jun 19, 2017 7:31 am
by S1L3nCe
I remember having this issue when I tried to scaling my world. All was good except this kind of problem with collision.
Otherwise it could be your draw who is smaller than real size.

As cube vs Spaceship works well, a good way to know if problem come from Cactus shape could be to replace it with a boxShape and try if your problem persists