Collision Question??

Hello
I want to create a force vector When objects collide to the same reaction force, please explain.
Thank you. :smiley: :question:

you first. i don’t see any questions nor explanation what you’re trying to do

I can only guess he wants to simulate the result of the collision of two objects by transferring the impact momentum from one object to another. If both objects are in movement that’s even better…
Think pool (billiards)…

And the question probably is: how could that be done?
I would also be interested in seeing a really simple example if someone who is already proficient with P3D would care to share :slight_smile:

^
^
^
Yes…

Thank :smiley:

Would the panda physics engine or ODE be the thing to use for that? Or do you want to know how to code that from scratch?

import direct.directbase.DirectStart
from pandac.PandaModules import *
from direct.interval.IntervalGlobal import *
from pandac.PandaModules import MouseWatcher
from direct.directtools.DirectGeometry import LineNodePath
from pandac.PandaModules import OdeFixedJoint
#MouseData.getInWindow

# Setup our physics world 
world = OdeWorld() 
world.setGravity(0, 0, -2) 
# The surface table is needed for autoCollide 
world.initSurfaceTable(1) 
world.setSurfaceEntry(0, 0, 100, 1.0, 9.1, 0.9, 0.00001, 0.0, 0.002) 
#########
# Create a space and add a contactgroup to it to add the contact joints 
space = OdeSimpleSpace() 
space.setAutoCollideWorld(world) 
contactgroup = OdeJointGroup() 
space.setAutoCollideJointGroup(contactgroup) 
#load a model. reparent it to the render so we can move it.
smiley = loader.loadModel('smiley')
smiley.reparentTo(render)
smiley.setPos(0,0,0)

#load a main model. reparent it to the render so we can move it.
box = loader.loadModel('mybox')
box.reparentTo(render)
box.setPos(0,5,0)
box.setHpr(0,90,0)

#Create Object & set Mass
ballBody = OdeBody(world) 
M = OdeMass() 
M.setSphere(1, 1) 
ballBody.setMass(M)
ballBody.setPosition(smiley.getPos(render)) 
ballBody.setQuaternion(smiley.getQuat(render))
ballBody.addForce(0,0,0)

boxBody = OdeBody(world) 
M = OdeMass() 
M.setSphere(1, 1) 
boxBody.setMass(M)
boxBody.setPosition(box.getPos(render)) 
boxBody.setQuaternion(box.getQuat(render))
boxBody.addForce(0,0,0)


base.trackball.node().setPos(0,50,0)

# Create a ballGeom 
ballGeom = OdeSphereGeom(space, 1) 
ballGeom.setCollideBits(BitMask32(0x00000001)) 
ballGeom.setCategoryBits(BitMask32(0x00000001)) 
ballGeom.setBody(ballBody) 
# Now create some lights to apply to everything in the scene.
 
# Create Ambient Light
ambientLight = AmbientLight( 'ambientLight' )
ambientLight.setColor( Vec4( 0.2, 0.2, 0.2, 0.1 ) )
ambientLightNP = render.attachNewNode( ambientLight.upcastToPandaNode() )
render.setLight(ambientLightNP)
 
# Directional light 01
directionalLight = DirectionalLight( "directionalLight" )
directionalLight.setColor( Vec4( 1, 1, 1, 0.5) )
directionalLightNP = render.attachNewNode( directionalLight.upcastToPandaNode())
# This light is facing forwards, away from the camera.
directionalLightNP.setHpr(10, -30, 0)
render.setLight(directionalLightNP)
axisx = LineNodePath(render,'axisy',4,Vec4(.5,.5,.5,.5))

axisx.reset()
axisx.drawLines([[(0,0,0),(2,0,0)]])
axisx.setColor(200,0,0)
axisx.create()


axisy = LineNodePath(render,'axisy',4,Vec4(.5,.5,.5,.5))
axisy.reset()
axisy.drawLines([[(0,0,0),(0,-2,0)]])
axisy.setColor(0,200,0)
axisy.create()

#axisz.reset()
axisz = LineNodePath(render,'axisz',4,Vec4(.5,.5,.5,.5))
axisz.reset()
axisz.drawLines([[(0,0,0),(0,0,2)]])
axisz.setColor(0,0,200)
axisz.create()
def myFunction():
   if base.mouseWatcherNode.hasMouse():
      mpos=base.mouseWatcherNode.getMouse()
      yaxis=base
      return mpos*15
space.setCollisionEvent("ode-collision") 
#base.accept("ode-collision", onCollision) 
def simulationTask(task): 
  space.autoCollide()
  world.quickStep(globalClock.getDt())
  x,y,z=ballBody.getPosition()
  mx,my = myFunction()
  #ballBody.setPosition(mx,0,my)
  smiley.setPosQuat(render, ballBody.getPosition(), Quat(ballBody.getQuaternion()))
  
  axisx.setPos(ballBody.getPosition())
  axisy.setPos(ballBody.getPosition())
  axisz.setPos(ballBody.getPosition())
  ox,oy,oz=ballBody.getPosition()
  speedx=ox-x
  speedy=oy-y
  speedz=oz-z
  axisx.setScale(speedx*3)
  axisy.setScale(speedy*3)
  axisz.setScale(speedz*3)
  return task.cont
taskMgr.doMethodLater(0.5, simulationTask, "Force Vector") 
run()


I can’t read momentum vector when objects collide. :question: :question:

Update Code

import direct.directbase.DirectStart
from pandac.PandaModules import *
from direct.interval.IntervalGlobal import *
from pandac.PandaModules import MouseWatcher
from direct.directtools.DirectGeometry import LineNodePath
from pandac.PandaModules import OdeFixedJoint
from pandac.PandaModules import BitMask32, CardMaker, Vec4, Quat
from pandac.PandaModules import OdeBody, OdeMass, OdeBoxGeom, OdePlaneGeom
from pandac.PandaModules import OdeWorld, OdeSimpleSpace, OdeJointGroup
#MouseData.getInWindow
# Setup our physics world 
world = OdeWorld() 
world.setGravity(0, 0, 0) 
# The surface table is needed for autoCollide 
world.initSurfaceTable(1) 
world.setSurfaceEntry(0, 0, 150, 0.5, 9.1, 0.9, 0.00001, 0.0, 0.002) 
#########
# Create a space and add a contactgroup to it to add the contact joints 
space = OdeSimpleSpace()
space.setAutoCollideWorld(world)
contactgroup = OdeJointGroup()
space.setAutoCollideJointGroup(contactgroup)
#load a model. reparent it to the render so we can move it.
smiley = loader.loadModel('smiley')
smiley.reparentTo(render)
smiley.setPos(0,0,10)

#load a main model. reparent it to the render so we can move it.
box = loader.loadModel('mybox')
box.reparentTo(render)
box.setPos(0,5,10)
box.setHpr(0,90,0)

#Create Object & set Mass
ballBody = OdeBody(world) 
M = OdeMass() 
M.setSphere(1, 1) 
ballBody.setMass(M)
ballBody.setPosition(smiley.getPos(render)) 
ballBody.setQuaternion(smiley.getQuat(render))
ballBody.addForce(0,0,0)

box1Body = OdeBody(world) 
K = OdeMass() 
K.setBox(50000, 20,10,1) 
box1Body.setMass(K)
box1Body.setPosition((0,0,-4)) 
box1Body.setQuaternion(box.getQuat(render))
box1Body.addForce(0,0,0)

box2Body = OdeBody(world) 
K = OdeMass() 
K.setBox(50000, 1,100,500) 
box2Body.setMass(K)
box2Body.setPosition((9.5,0,4)) 
box2Body.setQuaternion(box.getQuat(render))
box2Body.addForce(0,0,0)

box3Body = OdeBody(world) 
K = OdeMass() 
K.setBox(50000, 1,100,500) 
box3Body.setMass(K)
box3Body.setPosition((-9.5,0,4)) 
box3Body.setQuaternion(box.getQuat(render))
box3Body.addForce(0,0,0)

box4Body = OdeBody(world) 
K = OdeMass() 
K.setBox(50000, 20,10,1)
box4Body.setMass(K)
box4Body.setPosition((0,0,24)) 
box4Body.setQuaternion(box.getQuat(render))
box4Body.addForce(0,0,0)

base.trackball.node().setPos(0,50,-10)
# Create a ballGeom 
ballGeom = OdeSphereGeom(space,1)
ballGeom.setCollideBits(BitMask32(0x00000002))
ballGeom.setCategoryBits(BitMask32(0x00000001))
ballGeom.setBody(ballBody)

#make box colli
box1Geom = OdeBoxGeom(space,20.0,10.0,1.0)
box1Geom.setCollideBits(BitMask32(0x00000001))
box1Geom.setCategoryBits(BitMask32(0x00000002))
box1Geom.setBody(box1Body)

box2Geom = OdeBoxGeom(space,1.0,100.0,500.0)
box2Geom.setCollideBits(BitMask32(0x00000001))
box2Geom.setCategoryBits(BitMask32(0x00000002))
box2Geom.setBody(box2Body)

box3Geom = OdeBoxGeom(space,1.0,100.0,500.0)
box3Geom.setCollideBits(BitMask32(0x00000001))
box3Geom.setCategoryBits(BitMask32(0x00000002))
box3Geom.setBody(box3Body)

box4Geom = OdeBoxGeom(space,20.0,10.0,1.0)
box4Geom.setCollideBits(BitMask32(0x00000001))
box4Geom.setCategoryBits(BitMask32(0x00000002))
box4Geom.setBody(box4Body)
# Add a plane to collide with
cm = CardMaker("ground")
cm.setFrame(-20, 20, -20, 20)
ground = render.attachNewNode(cm.generate())
ground.setPos(0, 0, 0); ground.lookAt(0, 0, -1)
groundGeom = OdePlaneGeom(space,(0, 0, 1, 0))
groundGeom.setCollideBits(BitMask32(0x00000001))
groundGeom.setCategoryBits(BitMask32(0x00000002))

# Create Ambient Light
ambientLight = AmbientLight( 'ambientLight' )
ambientLight.setColor( Vec4( 0.2, 0.2, 0.2, 0.1 ) )
ambientLightNP = render.attachNewNode( ambientLight.upcastToPandaNode())
render.setLight(ambientLightNP)
 
# Directional light 01
directionalLight = DirectionalLight( "directionalLight" )
directionalLight.setColor( Vec4( 1, 1, 1, 0.5) )
directionalLightNP = render.attachNewNode( directionalLight.upcastToPandaNode())
# This light is facing forwards, away from the camera.
directionalLightNP.setHpr(10, -30, 0)
render.setLight(directionalLightNP)

axisx = LineNodePath(render,'axisy',4,Vec4(.5,.5,.5,.5))
axisx.reset()
axisx.drawLines([[(0,0,0),(2,0,0)]])
axisx.setColor(200,0,0)
axisx.create()


axisy = LineNodePath(render,'axisy',4,Vec4(.5,.5,.5,.5))
axisy.reset()
axisy.drawLines([[(0,0,0),(0,-2,0)]])
axisy.setColor(0,200,0)
axisy.create()

axisz = LineNodePath(render,'axisz',4,Vec4(.5,.5,.5,.5))
axisz.reset()
axisz.drawLines([[(0,0,0),(0,0,2)]])
axisz.setColor(0,0,200)
axisz.create()

sumv = LineNodePath(render,'axisz',4,Vec4(.5,.5,.5,.5))
sumv.reset()

def myFunction():
   mpos = False
   if base.mouseWatcherNode.hasMouse():
      mpos=base.mouseWatcherNode.getMouse()
      return mpos*18
kk=0
#space.setCollisionEvent("ode-collision") 
def simulationTask(task):
  global kk,ox,oy,oz
  sumv.reset()
  space.autoCollide()
  world.quickStep(globalClock.getDt())
  x,y,z=ballBody.getPosition()
  temp = myFunction()
  if type(temp).__name__ =='Point2':
    mx,my = temp
    ballBody.addForce(mx*20,0,my*20)
  smiley.setPosQuat(render, ballBody.getPosition(), Quat(ballBody.getQuaternion()))
  axisx.setPos(ballBody.getPosition())
  axisy.setPos(ballBody.getPosition())
  axisz.setPos(ballBody.getPosition())
  speedx,speedy,speedz=0,0,0
  
  if (kk==1):
    speedx=ox-x
    speedy=oy-y
    speedz=oz-z
  kk=1
  ox,oy,oz=x,y,z 
  axisx.setScale(speedx*-3)
  axisy.setScale(speedy*-3)
  axisz.setScale(speedz*-3)
  sumv.drawLines([[(ballBody.getPosition()),(((speedx*-6)+x),((speedy*-6)+y),((speedz*-6)+z))]])
  sumv.setColor(0,0,200)
  sumv.create()
  print space.autoCollide()
  #print "speed X = ",speedx,"\nspeed Y = ",speedy,"\nspeed Z = ",speedz
  #speedx,speedy,speedz=0,0,0
  contactgroup.empty()
  return task.cont
taskMgr.doMethodLater(0.5, simulationTask, "Force Vector") 
run()


[/img]