How to detect collison and delete body?

Post Reply
i7.strelok
Posts: 21
Joined: Fri Jan 20, 2017 10:44 pm

How to detect collison and delete body?

Post by i7.strelok »

Hey,
I could do the collisions but they move until infinity that's why I close the game.
I need detected collision, then when they collide, I need to see them no more.
Then I should eliminate the ogre node, the object of my class, the rigid body and the state of movement, is that true?
How to detect collisions? My objects (bullet and alien) have attributes, one of those attributes is a pointer to the rigid body.

Please, I need help to delete collisions.
S1L3nCe
Posts: 50
Joined: Thu Mar 24, 2016 10:22 am
Location: France

Re: How to detect collison and delete body?

Post by S1L3nCe »

Hi,

Wiki with collision detection article (Collision Callbacks and Triggers) :
http://bulletphysics.org/mediawiki-1.5. ... l_Articles

Delete your Bullet bodies with removeRigidBody world's method. Idk for Ogre.
delete your bodies exemple:
http://www.bulletphysics.org/Bullet/php ... =9&t=11182
benelot
Posts: 350
Joined: Sat Jul 04, 2015 10:33 am
Location: Bern, Switzerland
Contact:

Re: How to detect collison and delete body?

Post by benelot »

Hi,

Your problem has to be solved in an inverse manner. When you create your rigidbodies, you have to set the userpointer to something like the object of your class or similar to access it on collision. You can detect your collisions by using a collision callback as described here:
http://www.bulletphysics.org/mediawiki- ... d_Triggers

Code: Select all

//Assume world->stepSimulation or world->performDiscreteCollisionDetection has been called

    int numManifolds = world->getDispatcher()->getNumManifolds();
    for (int i = 0; i < numManifolds; i++)
    {
        btPersistentManifold* contactManifold =  world->getDispatcher()->getManifoldByIndexInternal(i);
        btCollisionObject* obA = static_cast<btCollisionObject*>(contactManifold->getBody0());
        btCollisionObject* obB = static_cast<btCollisionObject*>(contactManifold->getBody1());

       //... here you can check for obA´s and obB´s user pointer again to see if the collision is alien and bullet and in that case initiate deletion.
    }
Cheers!
i7.strelok
Posts: 21
Joined: Fri Jan 20, 2017 10:44 pm

Re: How to detect collison and delete body?

Post by i7.strelok »

Hi thanks.
I'm very very very worried.
I try with this code:

Code: Select all

void MyFrameListener::DetectCollisionDrain() {
    int i = 0;
 for (i = _world->getNumCollisionObjects() - 1; i >= 0; --i) {
        btCollisionObject* obj = _world->getCollisionObjectArray()[i];
        btRigidBody* body = btRigidBody::upcast(obj);
        if (body && body->getMotionState()) {
            delete body->getMotionState();
        _world->removeCollisionObject(obj);
        delete obj;
But all objects are motionless.
In my scene I have 2 types of bodies. (E and B)

Code: Select all

  btRigidBody::btRigidBodyConstructionInfo ARigidBodyCI(mass,AMotionState,_ACollisionShape,AInertia);
     _ARigidBody = new btRigidBody(ARigidBodyCI);
     alien->setPhysicalRigidBody(_ARigidBody); //add rigid body to my object alien
   _alienManager->addEnemyList(alien); //Add a alien to list alien
     _world->addRigidBody(_ARigidBody); //Add ridig body to world
]
i7.strelok
Posts: 21
Joined: Fri Jan 20, 2017 10:44 pm

Re: How to detect collison and delete body?

Post by i7.strelok »

error: static_cast from type ‘const btCollisionObject*’ to type ‘btCollisionObject*’ casts away qualifiers

warning: unused variable ‘obB’ [-Wunused-variable]
----
(Alt-Enter shows hints)


In this lines:

Code: Select all

btCollisionObject* obA = static_cast<btCollisionObject*>(contactManifold->getBody0());
        btCollisionObject* obB = static_cast<btCollisionObject*>(contactManifold->getBody1());
:roll: :roll: :roll:
benelot
Posts: 350
Joined: Sat Jul 04, 2015 10:33 am
Location: Bern, Switzerland
Contact:

Re: How to detect collison and delete body?

Post by benelot »

Making it a ´const´ btCollisionObject* should fix the problem above. What problems are you having now?
i7.strelok
Posts: 21
Joined: Fri Jan 20, 2017 10:44 pm

Re: How to detect collison and delete body?

Post by i7.strelok »

?? I do not understand. What do I have to change?
S1L3nCe
Posts: 50
Joined: Thu Mar 24, 2016 10:22 am
Location: France

Re: How to detect collison and delete body?

Post by S1L3nCe »

http://bulletphysics.org/Bullet/phpBB3/ ... php?t=8591

Code: Select all

const btCollisionObject* obA = contactManifold->getBody0();
const btCollisionObject* obB = contactManifold->getBody1();
Try that.
benelot
Posts: 350
Joined: Sat Jul 04, 2015 10:33 am
Location: Bern, Switzerland
Contact:

Re: How to detect collison and delete body?

Post by benelot »

I just updated the doc to be consistent with this.
i7.strelok
Posts: 21
Joined: Fri Jan 20, 2017 10:44 pm

Re: How to detect collison and delete body?

Post by i7.strelok »

Now yes, it compiles me and runs well.
I will try to express myself as best as possible so that you can help me.

I'm making a game for a project, it's similar to spaceinvaders, I have to shoot bullets at enemies, enemies move (with impulse) and bullets I apply a force. The problem is that after firing a bullet and spend a few seconds my game is closed, I guess the problem is that the bullet tends towards infinity then it closes.
I need 2 things.

1) If a bullet goes too far, I want it to be destroyed.
2) If there is a collision between a bullet and an enemy, I want them both to be destroyed.

I am new and I am striving to learn but I need the support of experts like you to start on this.

I appreciate all your answers.
S1L3nCe
Posts: 50
Joined: Thu Mar 24, 2016 10:22 am
Location: France

Re: How to detect collison and delete body?

Post by S1L3nCe »

1) Just check bullet position, and if it's out of your screen, delete it.

2) You can just delete it in your collision detection.

delete with removing it from the Bullet world and hide sprites.
benelot
Posts: 350
Joined: Sat Jul 04, 2015 10:33 am
Location: Bern, Switzerland
Contact:

Re: How to detect collison and delete body?

Post by benelot »

So now that the collision callback is working, can you check if two objects like your bullet and your enemy collided? Just add your game object to your rigidbody as its userpointer and then, if it collides with something else and shows up in the collision callback, you can do something like:

Code: Select all

if(((YourGameObject*)obA->getUserPointer())->isOfType() == TYPE:BULLET && ((YourGameObject*)obB->getUserPointer())->isOfType() == TYPE:BULLET){
((YourGameObject*)obA->getUserPointer())->delete(); // this does something like deleting your graphics object, removing from world and deleting your physics object and then deleting your game object
((YourGameObject*)obB->getUserPointer())->delete();
}
The bullets flying out of your screen will slow down your game but should not crash it. You should debug into it to see what is going on. You can either limit your game space and add a boundary, against which your bullets collide and destroy or you can simply add any other kind of left screen or passed x,y,z position test to kill your bullets.

Edit: second!
Post Reply