How to remove bodyA when its in contact with bodyB?

Official Python bindings with a focus on reinforcement learning and robotics.
Post Reply
AisyJeff
Posts: 4
Joined: Sat Apr 02, 2022 1:13 pm

How to remove bodyA when its in contact with bodyB?

Post by AisyJeff »

this is the sample code. Then I uses "p.getContactPoint()" between bodyA and bodyB. Then, I would like to remove bodyA after they hit each other using p.removeBody(). This is the code:

Code: Select all

import pybullet as p
import pybullet_data
import time

physicsClient = p.connect(p.GUI)
p.setAdditionalSearchPath(pybullet_data.getDataPath())
p.loadURDF('plane.urdf', [0,0,0])

cube1=p.loadURDF("cube.urdf", [0,0,1])
lego=p.loadURDF("cube.urdf", [0,0,4])
p.setGravity(0,0,-10)

def contactPoints(self):

    contacts = p.getContactPoints(bodyA=self.cube1, bodyB=self.lego)
    for contact in contacts:
        link_index = contact[2]
        if link_index >= 0:
                p.removeBody(self.lego)


for i in range(10000):
    p.stepSimulation()

    time.sleep(1./240.)
    
   

Hopefully anyone can help me with this. It seems simple but I can't get it done :cry:
AisyJeff
Posts: 4
Joined: Sat Apr 02, 2022 1:13 pm

Re: How to remove bodyA when its in contact with bodyB?

Post by AisyJeff »

Don't think its needed to use "self". This method is resolved by writing this in code:

Code: Select all

import pybullet as p
import pybullet_data
import time

physicsClient = p.connect(p.GUI)
p.setAdditionalSearchPath(pybullet_data.getDataPath())
p.loadURDF('plane.urdf', [0,0,0])

cube1=p.loadURDF("cube.urdf", [0,0,1])
lego=p.loadURDF("cube.urdf", [0,0,4])
p.setGravity(0,0,-10)


def contactPoints():
    contacts = p.getContactPoints(bodyA=cube1, bodyB=lego)
    for contact in contacts:
        link_index = contact[2]
        if link_index >= 0:
                p.removeBody(lego)


for i in range(10000):
    p.stepSimulation()
    contactPoints()
    time.sleep(1./240.)
User avatar
RKaji
Posts: 2
Joined: Wed Aug 04, 2021 8:48 am

Re: How to remove bodyA when its in contact with bodyB?

Post by RKaji »

I'm genuinely impressed with the comprehensive effort put into organizing the MSU-1 game list on Zeldix. It's a true game-changer for enthusiasts like me who appreciate the convenience of having everything in alphabetical order. The meticulous detailing in the sidenotes showcases a commitment to user-friendly navigation. Kudos to the team for making our gaming experience seamless and enjoyable! If you're as passionate about organization as I am, you might find this link helpful for any writing needs: (https://essaypro.com/write-my-speech). Cheers to the Zeldix community for fostering a well-ordered and inclusive gaming hub!
Post Reply