[SOLVED] How to return WHICH rigid body was hit?

Post Reply
jimjamjack
Posts: 39
Joined: Tue Jan 31, 2017 8:07 pm

[SOLVED] How to return WHICH rigid body was hit?

Post by jimjamjack »

I'm currently making a simple game with OpenGL and Bullet, and I have targets and walls in the scene. The targets are each given a collision shape of it's own mesh and the walls have a simple box shape.

I want my ray casting method to return something like "Hit target" or "Hit wall", as right now it just says "Hit" or "Miss" depending on whether the ray has hit a target. How can I differentiate between my rigid bodies in the scene, to know which I am currently hitting?

Right now, I'm just doing this (without the walls having collision shapes):

Code: Select all

if (RayCallback.hasHit()) {
			printf("Hit!");
		}
		else {
			printf("Miss!");
		}
Apologies if this easily done, I'm still just getting into Bullet. Cheers though.
ktfh
Posts: 44
Joined: Thu Apr 14, 2016 3:44 pm

Re: How to return WHICH rigid body was hit?

Post by ktfh »

There are several different callbacks for finding objects that overlap your ray and you can extend them to create your own special query. Generally RayCallback.m_collisionObject would contain a pointer to the object you hit. btCollisionObject::getInternalType or btRigidbody::upcast could be used to determine the objects type. Additionally btCollisionObject::getUserPointer and btCollisionObject::setUserPointer are useful for associating more information with a collision object.

Try skimming the docs might give you more insight into how to design your game.
http://bulletphysics.org/Bullet/BulletF ... lback.html
http://bulletphysics.org/Bullet/BulletF ... 31c72b0a6a
jimjamjack
Posts: 39
Joined: Tue Jan 31, 2017 8:07 pm

Re: How to return WHICH rigid body was hit?

Post by jimjamjack »

Seems like what I need, great. I should hopefully be able to store enough information in the pointer then.

Quick question: I'm creating a new box collision shape, then creating 4 new position vectors (one for each wall) and placing a box at each position. However, I seem to have an extra one that just sits at the default position (I can see it with the debug drawer). It's there even when I just add a rigidbody without any of my positions too. Do you know why this might be?

Thanks.
jimjamjack
Posts: 39
Joined: Tue Jan 31, 2017 8:07 pm

Re: How to return WHICH rigid body was hit?

Post by jimjamjack »

Nevermind, after having all 4 collision boxes set, it appears to be gone. I assume it was because I was trying to create extras since I had only 2 sets of positions yet I was expecting 4 total.
Post Reply