WorldViz User Forum using sensor data in Physics1.py
 Register FAQ Search Today's Posts Mark Forums Read

#1
12-21-2005, 03:03 AM
 Eunice 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)

#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

#The list of rigid body objects
bodies = []

#Add a ground plane to the physcis simulation

PORT_MOTIONSTAR = '193.61.166.56'

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

#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.color(viz.YELLOW)

#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.
#2
12-21-2005, 10:09 AM
 farshizzo WorldViz Team Member Join Date: Mar 2003 Posts: 2,849
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.
Code:
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.
#3
12-23-2005, 02:50 AM
 Eunice Member Join Date: Oct 2005 Posts: 21
Thanks a lot. I just tried this code. Now the ball bounces once in each timer cycle when physics applies.

Any other solutions?
#4
12-23-2005, 10:24 AM
 farshizzo WorldViz Team Member Join Date: Mar 2003 Posts: 2,849
Try having your timer expire every frame, instead of every half second.
Code:
viz.starttimer(0,0,viz.FOREVER)
#5
01-03-2006, 05:18 AM
 Eunice Member Join Date: Oct 2005 Posts: 21
it looks much better. Thanks a lot

 Thread Tools Display Modes Rate This Thread Linear Mode Rate This Thread: 5 : Excellent 4 : Good 3 : Average 2 : Bad 1 : Terrible

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is Off Forum Rules
 Forum Jump User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home Announcements Vizard Precision Position Tracker (PPT) Plug-in development

All times are GMT -7. The time now is 07:36 AM.