PDA

View Full Version : using sensor data in Physics1.py


Eunice
12-21-2005, 03:03 AM
I'm trying to change the code of physics1.py and use sensor data to control a ball to bump into other objects. Please see the following code. The yellow ball linked with sensor behaves erratically, though the printed sensor data seems okay. I wonder whether that because I try to link a dynamic object to a sensor. Is it possible to "turn off" the dynamics on this ball (e.g. gravity) while keep its collision response (I mean, when it collides with other objects, it will push them out of the way with seemingly infinite force)?


viz.enable(viz.PHYSICS)

#Add the ground
ground = viz.add('tut_ground.wrl')
#This will create an infinite collision plane that points upward with height 0
ground.collideplane(0,1,0,0)

#Declare some constants
NUM_BALLS = 40
BALL_ALPHA = 0.8 #0.5
BALL_RADIUS = 0.1

#The list of rigid body objects
bodies = []

#Add a ground plane to the physcis simulation
viz.addplane(0,1,0,0)

PORT_MOTIONSTAR = '193.61.166.56'
sensor1 = viz.add('motionstar.dls')
sensor2 = viz.add('motionstar.dls')

#Add a bunch of balls to the physics simulation
for x in range(0,NUM_BALLS):
#Add the geometric representation of the ball
ball = viz.add('ball.wrl')
#The balls collision will be represented by a sphere with BALL_RADIUS
ball.collidesphere(BALL_RADIUS)
#Scale the our ball to match the size of the sphere
ball.scale(BALL_RADIUS/0.4,BALL_RADIUS/0.4,BALL_RADIUS/0.4)
#Set the alpha value of the ball
ball.alpha(BALL_ALPHA)
#Add the ball to the physical objects list
bodies.append(ball)

#Add a box model
box = viz.add('box10cm.wrl')
#Use the boxes bounding box for collisions
box.collidebox()
#Set the alpha value of the box, transparency
box.alpha(BALL_ALPHA)
#Add the box to the list of physical objects
bodies.append(box)

myBall1 = viz.add('ball10cm.wrl')
myBall1.color(viz.YELLOW)
myBall1.collidesphere(BALL_RADIUS)

myBall1.link(sensor1)

#viz.lookat(0,0,0)

#This function will reset the physics simulation
def Reset():
for x in range(0,NUM_BALLS+1):
#Reset the physics of the object (This will zero its velocities and forces)
bodies[x].reset()
#Translate the object to the proper height
bodies[x].translate(vizmat.GetRandom(-2,2),0,vizmat.GetRandom(-2,2))
#Reset the rotation of the object
bodies[x].rotate(0,1,0,0)

Reset()

def mykey(key):
#Reset the physics simulation
if key == 'r':
Reset()

def timer(num):
print sensor1.get()

#Create a callback for keyboard events
viz.callback(viz.KEYBOARD_EVENT,mykey)

#Set the background color
viz.clearcolor(0.5,0.5,1)
viz.starttimer(0,0.5,viz.FOREVER)
viz.callback(viz.TIMER_EVENT, timer)


Do I have to the dynamics manually like the duckcourt example?
Any ideas? Thank you.

farshizzo
12-21-2005, 10:09 AM
Hi,

This would require the use of a joint to control the balls position while performing dynamics on it. Currently, the physics plugin doesn't have an interface for creating joints.

Instead of linking the sensor to the ball, use the following code in your timer to place it manually.data = sensor1.get()
myBall1.translate(data[:3])
myBall1.rotate(data[3:6])
myBall1.reset() #Reset physics on ball
myBall1.enable(viz.PHYSICS)It won't be perfect, but it's the best you can do without performing the physics manually.

Eunice
12-23-2005, 02:50 AM
Thanks a lot. I just tried this code. Now the ball bounces once in each timer cycle when physics applies.

Any other solutions?

farshizzo
12-23-2005, 10:24 AM
Try having your timer expire every frame, instead of every half second.viz.starttimer(0,0,viz.FOREVER)

Eunice
01-03-2006, 05:18 AM
it looks much better. Thanks a lot