WorldViz User Forum  

Go Back   WorldViz User Forum > Vizard

Reply
 
Thread Tools Rate Thread Display Modes
  #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
  #2  
Old 12-21-2005, 10:09 AM
farshizzo farshizzo is offline
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.
Reply With Quote
  #3  
Old 12-23-2005, 02:50 AM
Eunice Eunice is offline
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?
Reply With Quote
  #4  
Old 12-23-2005, 10:24 AM
farshizzo farshizzo is offline
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)
Reply With Quote
  #5  
Old 01-03-2006, 05:18 AM
Eunice Eunice is offline
Member
 
Join Date: Oct 2005
Posts: 21
it looks much better. Thanks a lot
Reply With Quote
Reply

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

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 Jump


All times are GMT -7. The time now is 08:58 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2019, vBulletin Solutions, Inc.
Copyright 2002-2018 WorldViz LLC