View Single Post
  #1  
Old 12-21-2005, 03:03 AM
Eunice Eunice is offline
Member
 
Join Date: Oct 2005
Posts: 21
using sensor data in Physics1.py

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)?

Code:
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.

Last edited by Eunice; 12-21-2005 at 03:41 AM.
Reply With Quote