check collision masks when colliding

Post Reply
Salocin808
Posts: 7
Joined: Thu Jun 15, 2017 9:32 am

check collision masks when colliding

Post by Salocin808 » Fri Jun 16, 2017 8:53 am

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

Salocin808
Posts: 7
Joined: Thu Jun 15, 2017 9:32 am

Re: check collision masks when colliding

Post by Salocin808 » Fri Jun 16, 2017 9:11 am

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..

S1L3nCe
Posts: 38
Joined: Thu Mar 24, 2016 10:22 am
Location: France

Re: check collision masks when colliding

Post by S1L3nCe » Fri Jun 16, 2017 12:26 pm

Hi,

What do you mean by "too early" ?

Salocin808
Posts: 7
Joined: Thu Jun 15, 2017 9:32 am

Re: check collision masks when colliding

Post by Salocin808 » Sat Jun 17, 2017 4:10 pm

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
Attachments
collision.png
early collision
collision.png (654.84 KiB) Viewed 779 times

S1L3nCe
Posts: 38
Joined: Thu Mar 24, 2016 10:22 am
Location: France

Re: check collision masks when colliding

Post by S1L3nCe » Mon Jun 19, 2017 7:31 am

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

Post Reply