How to turn off gravity only for some Objects ?

Official Python bindings with a focus on reinforcement learning and robotics.
Yogha001
Posts: 3
Joined: Tue Jan 31, 2023 4:16 pm

How to turn off gravity only for some Objects ?

Post by Yogha001 »

I am currently working on a VR simulation in VRED, utilizing PyBullet as the physics simulation engine to support real-time simulation with objects affected by gravity. However, there are instances where it may be necessary to turn off gravity for specific objects in the simulation.

My current solution to achieve this is by setting the mass of these objects to 0, but this does not result in a realistic scenario of objects floating in space. I am seeking a solution to have some objects float in space with no gravity, while others are affected by gravity in the simulation.

The most realistic solution is to run the Physics client on multiple Server.
My current code looks like this.

Code: Select all

newScene()

from pybullet_utils import bullet_client as bc
import pybullet
import math
import time
import pybullet_data

class TestAction(vrAEBase):
   def __init__(self):
       vrAEBase.__init__(self)
       self.physicsClient1 = bc.BulletClient(connection_mode=pybullet.DIRECT)
       self.physicsClient2 = bc.BulletClient(connection_mode=pybullet.DIRECT)
       
       self.physicsClient1.setAdditionalSearchPath(pybullet_data.getDataPath())
       self.physicsClient2.setAdditionalSearchPath(pybullet_data.getDataPath())
       
       self.physicsClient1.setRealTimeSimulation(1)
       self.physicsClient2.setRealTimeSimulation(1)
       
       # Set gravity to zero in the first instance
       self.physicsClient1.setGravity(0,0,0) # 0 gravity in the GUI
       
       # Set gravity for the sphere object in the second instance
       self.physicsClient2.setGravity(0,0,-9.8) # -9.8 in Z-axis (typical earth gravity)
       
       #loadURDF
       self.planeId = self.physicsClient1.loadURDF("plane.urdf", [0, 0, 0], [0, 0, 0, 1])
       
       self.boxId1 = self.physicsClient1.loadURDF("BOX.urdf",[-1, -1, 10], [0, 0, 0, 1])

       self.boxId2 = self.physicsClient2.loadURDF("BOX.urdf",[1, 1, 10], [0, 0, 0, 1])
       
       self.createNodes()
       self.addLoop()


   def __del__(self):
       self.physicsClient1.disconnect()
       self.physicsClient2.disconnect()
       print("end")
       
   def createNodes(self):
       
       if not getRootNode().getChild("Box").isValid():
           self.boxNode = createBox(1000, 1000, 1000, 1, 1, 1, 0.0, 1.0, 0.0, 0.0)
           self.boxNode.setName("Box")
           getRootNode().addChild(self.boxNode)
       if not getRootNode().getChild("Box2").isValid():
           self.boxNode2 = createBox(1000, 1000, 1000, 1, 1, 1, 0.0, 0.0, 1.0, 0.0)
           self.boxNode2.setName("Box2")
           getRootNode().addChild(self.boxNode2)


   def loop(self):

       pos, rot = self.physicsClient1.getBasePositionAndOrientation(self.boxId1

 
   def loop(self):

       pos, rot = self.physicsClient1.getBasePositionAndOrientation(self.boxId1)
       eulerRot = self.physicsClient1.getEulerFromQuaternion(rot)
       self.boxNode.setWorldTranslation(pos[0]*1000, pos[1]*1000, pos[2]*1000)
       self.boxNode.setRotation(math.degrees(eulerRot[0]), math.degrees(eulerRot[1]), math.degrees(eulerRot[2]))
   
   
       pos2, rot2 = self.physicsClient2.getBasePositionAndOrientation(self.boxId2)
       eulerRot2 = self.physicsClient2.getEulerFromQuaternion(rot2)
       self.boxNode2.setWorldTranslation(pos2[0]*1000, pos2[1]*1000, pos2[2]*1000)
       self.boxNode2.setRotation(math.degrees(eulerRot[0]), math.degrees(eulerRot[1]), math.degrees(eulerRot[2]))

 ta = TestAction()
 ta.setActive(true)
When i run this script i don't get any error, so i guess it seems to work. But the Simulation is not working.

I tried another approche to have a better prespective this time without VRED.

So first i run the Example script "runServer.py"

Code: Select all

#add parent dir to find package. Only needed for source code build, pip install doesn't need it.
import os
import inspect
currentdir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
parentdir = os.path.dirname(os.path.dirname(currentdir))
os.sys.path.insert(0, parentdir)

import pybullet_data
import pybullet as p
import time

p.connect(p.GUI_SERVER)
p.setAdditionalSearchPath(pybullet_data.getDataPath())



while (1):
  #this is a no-op command, to allow GUI updates on Mac OSX (main thread)
  p.setPhysicsEngineParameter()
  p.setRealTimeSimulation(1)
then I run my script it's so similar to the "multipleScenes.py"

Code: Select all

from pybullet_utils import bullet_client as bc
import pybullet
import pybullet_data

physicsClient1 = bc.BulletClient(connection_mode=pybullet.SHARED_MEMORY)
physicsClient2 = bc.BulletClient(connection_mode=pybullet.SHARED_MEMORY)

planeID = physicsClient1.loadURDF(pybullet_data.getDataPath() + "/plane.urdf", [0, 0, 0])
sphereId1 = physicsClient1.loadURDF(pybullet_data.getDataPath() + "/r2d2.urdf", [1,1,2])

sphereId2 = physicsClient2.loadURDF(pybullet_data.getDataPath() + "/r2d2.urdf", [-1,-1,2])

physicsClient1.setGravity(0,0,-9.8) # 0 gravity in the GUI

physicsClient2.setGravity(0,0,0) # -9.8 in Z-axis (typical earth gravity)

physicsClient1.setRealTimeSimulation(1)
physicsClient2.setRealTimeSimulation(1)

physicsClient1.disconnect()
physicsClient2.disconnect()
The Problem here seems that the same Gravity is applied on both spheres.