WorldViz User Forum  

Go Back   WorldViz User Forum > Vizard

Reply
 
Thread Tools Rating: Thread Rating: 2 votes, 5.00 average. Display Modes
  #1  
Old 05-15-2008, 05:20 AM
nige777 nige777 is offline
Member
 
Join Date: Nov 2007
Location: UK
Posts: 78
RE:Slide Joints and physics optimization

Hi,

I have constructed a virtual environment containing, amongst other things, a forklift. As the "name of the game" for this project was realism I have constructed the truck using purely physics joints. This works well, the truck follows the lay of the land and behaves pretty well how i want it to. However, one of the tasks for the user is to load some items (steel cages) onto the back of a lorry, at this point things go a bit weird. I am using a slide joint for the forks and 'applyForce' on joystick button events (within a timer) to raise and lower them, but when I pick up the cages they bounce and jump all over the place - not good. I have tried everything i can think of -reducing the physics stepsize (0.0003) and accuracy (0.000001), altering the weight, bounce and friction of the forks and cages, all of which have an effect, but not really what I want.
Is applyForce the best way to operate this type of slide joint, or is having it within a timer making the forks movement jumpy???? HELP, i've run out of ideas and this has to be finished by beginning of next week!!

This is the code for the cages:
Code:
top =cage1.collideBox(node = 'Cage_Top',bounce=0,density=.1)
top.setFriction(80)
bottom = cage1.collideBox(node = 'Cage_Bottom',bounce=0,density=.4)
bottom.setFriction(80)
side1=cage1.collideBox(node='Left_Ones',bounce=0,density=0.05)
side2=cage1.collideBox(node='Right_Ones',bounce=0,density=0.05)
cage1.enable(viz.COLLIDE_NOTIFY)
This is the code for the FLT:
Code:
#Build FLT#
###########
flt = viz.add('flt8.ive')
body = flt.getChild('Body')
body.playsound('CE2a.WAV',viz.LOOP)
body.collideBox(bounce=0, density = 1.25)

lever1=flt.getChild('Lever1')
lever1.collideBox(bounce=0,density=0.05)
lever2=flt.getChild('Lever02')
lever2.collideBox(bounce=0,density=0.05)

#Get and process children
run = flt.getChild('Shaft')
run.collideBox(bounce=0, density = 0.1)
run.setPosition([0.0,0.2,0.0])
print run.getPosition()

guard = flt.getChild('ForkGuard')
guard.translate([0,0,0.005])
guard.collideBox(bounce=0,density=0.05)

fork1 = flt.getChild('Blades')
#fork1.setPosition(0,-0.1,0)
forks = fork1.collideBox(bounce=0,density = 0.1)
forks.setFriction(200)

steering = flt.getChild('Steering_Wheel')
steerBox = steering.collideSphere(bounce=0,density=0.01)

#Link
bodyLink = viz.link(body,view,dstFlag=viz.LINK_BODY)
bodyLink.preTrans([0,-0.12,0.28])

##--MOTORS & JOINTS--##

steerlink = viz.phys.addHingeJoint(body,steering,pos=[-0.005,1.153,0.813],axis0=[0,1,0])
steerlink.setMotorAngle(0,0,1,.25)

#lever1link = viz.phys.addHingeJoint(body,lever1,pos=[0.249,0.763,0.993],axis0=[1,0,0])
lever1link=viz.phys.addFixedJoint(body,lever1)
lever2link=viz.phys.addFixedJoint(body,lever2)
#lever1link.setMotorAngle(0,0,100,.5)

slideJoint = viz.phys.addSliderJoint(fork1,run,pos=[-0.005,0.5,1.317],axis0=(0,1,0))
slideJoint.setAxisFriction(0,12)
slideJoint.setAxisLimit(0,-0.2,2.8) # Limit sliding distance

guardJoint = viz.phys.addFixedJoint(guard,fork1)
#cwLink = viz.phys.addFixedJoint(cw,body)
#runJoint = viz.phys.addFixedJoint(run,body)
runJoint = viz.phys.addHingeJoint(run,body,pos=[-0.005,0.279,1.21],axis0=[1,0,0]) 
#runJoint.setAxisFriction(0,10)
#runJoint.setAxisLimit(0,-5,8)
runJoint.setMotorAngle(0,-1,26,1)
 
fr = viz.add('F_wheel1.ive',scale = [1,1,1],pos=[0.58,0.194,0.9])
fright = fr.collideSphere(bounce = 0, density = 1)
fright.setFriction(5)
frJoint = viz.phys.addHingeJoint(body,fr,pos=[0.58,0.194,0.9],axis0=(-1,0,0))
frJoint.setAxisFriction(0,1.5)

fl = viz.add('F_wheel1.ive',scale = [1,1,1],pos=[-0.58,0.194,0.9])
fleft = fl.collideSphere(bounce = 0, density =1)
fleft.setFriction(5)
flJoint = viz.phys.addHingeJoint(body,fl,pos=[-0.58,0.194,0.9],axis0=(-1,0,0))
flJoint.setAxisFriction(0,1.5)

rr = viz.add('F_wheel1.ive',scale=[1,1,1],pos=[0.0,0.194,-0.727])
#rr.playsound('CE2a.WAV',viz.LOOP)
rearr = rr.collideSphere(bounce = 0, density = 3)
rearr.setFriction(2)
rrJoint = viz.phys.addWheelJoint(body,rr,pos=[0.0,0.194,-0.727],axis0=(0,1,0),axis1=(1,0,0))
rrJoint.setAxisBounce(0,0)
rrJoint.setAxisBounce(1,0)
rrJoint.setAxisFriction(1,.75)
print rrJoint.getPosition()


def steer(val):
    rrJoint.setMotorAngle(0,val,0xFF,.05)
    
def steerWheel(val):
    steerlink.setMotorAngle(0,val,3,.1) 
    
def SetThrottle(val):
    rrJoint.setMotorThrottle(1,-val,9,1100)

#Control Forks#
def buttonTimer():

    
    if joy.isButtonDown (7):
        print "Button is 7 down"
        fork1.applyForce([0,215,0])
        
    elif joy.isButtonDown (8):
        print "Button 8 is down"
        fork1.applyForce ([0,-205,0])
        
    if joy.isButtonDown (5):
        runJoint.setMotorAngle(0,-6,6,.03)
    elif joy.isButtonDown (6):
        runJoint.setMotorAngle(0,2,6,.03)

        
vizact.ontimer(viz.FASTEST_EXPIRATION,buttonTimer)
Any help, suggestions, information will be VERY gratefully received.

Cheers,

Nige
Reply With Quote
  #2  
Old 05-16-2008, 06:22 PM
farshizzo farshizzo is offline
WorldViz Team Member
 
Join Date: Mar 2003
Posts: 2,849
I've created a sample script that creates a slider joint and uses the arrow keys to manually adjust the distance between the joint. Have a look at the SliderMotor class I made. It will update the motor velocity of the joint every frame to keep it at the desired distance. You can tweak the maxTorque and speed variables to control how quickly the it will go to the desired distance. This should be the most stable way of manually controlling a slider joint.
Code:
import viz
viz.go()

viz.clearcolor(viz.GRAY)

#Enable physics
viz.phys.enable()

#Add ground plane
ground = viz.add('tut_ground.wrl')
ground.collidePlane()

#Add 2 balls
ball1 = viz.add('box.wrl',pos=(0,2.8,6))
ball1.collideSphere()

ball2 = viz.add('ball.wrl',pos=(0,1.8,6))
ball2.collideSphere()

ANCHOR_POS = (0,1.8,5)

#Add a joint
joint = viz.phys.addSliderJoint(ball1,ball2,pos=ANCHOR_POS,axis0=(0,1,0))
joint.setAxisLimit(0,-0.5,0.5)

#Class that will force slider joint to specified distance
class SliderMotor(viz.EventClass):
	def __init__(self,joint,maxTorque=40.0,speed=20.0):
		viz.EventClass.__init__(self)
		self.distance = 0.0
		self.maxTorque = maxTorque
		self.speed = speed
		self.joint = joint
		
		self.callback(viz.TIMER_EVENT,self.onTimer)
		self.starttimer(0,0,viz.FOREVER)
		
		#Account for bug in current version of Vizard
		if viz.compareVersion('3.00.2708') >= 0:
			self.factor = viz.RAD_TO_DEG
		else:
			self.factor = 1.0
		
	def onTimer(self,num):
		vel = self.factor * self.speed * (self.distance - self.joint.getAxisDistance(0))
		self.joint.setMotorVelocity(0, vel, self.maxTorque)
		
motor = SliderMotor(joint)

#Use arrow keys to adjust motor desired distance
def IncrementAngle(inc):
	motor.distance += inc * viz.elapsed()
	print motor.distance
vizact.whilekeydown(viz.KEY_UP,IncrementAngle,0.1)
vizact.whilekeydown(viz.KEY_DOWN,IncrementAngle,-0.1)

JointDistLabel = viz.addText('Joint Distance: 0.00',viz.ORTHO)
JointDistLabel.fontSize(24)
JointDistLabel.translate(10,10)

def UpdateAxisInfo():
	JointDistLabel.message('Joint Distance: %.2f' % (joint.getAxisDistance(0)))
vizact.ontimer(0,UpdateAxisInfo)
Reply With Quote
  #3  
Old 06-03-2008, 04:06 PM
nige777 nige777 is offline
Member
 
Join Date: Nov 2007
Location: UK
Posts: 78
Thanks man,

Haven't been around for a while - finishing final year work etc etc , however I am continuing to develop this environment further so thanks, I shall be looking at this tomorrow

Cheers
Reply With Quote
  #4  
Old 06-12-2009, 02:35 AM
nige777 nige777 is offline
Member
 
Join Date: Nov 2007
Location: UK
Posts: 78
Hey, been a while since you posted this example for me farshizzio - been busy busy busy with a literature review / search since then so I have only just got back to finishing this project off!!

I have (fairly) successfully implemented yr slider motor class & it works great THANKS, however, I still have a couple of problems that I would like to run past u :-

- How could I replace the keyboard control event for joystick buttons? I was using a timer to look for a joy.isButtonDown event but seem unable figure out a way of using this method with yr wonderful SliderMotor class!
- When lowering the forks sometimes the item that I was carrying (cages or pallets) get 'stuck' in thin air and only drop if they get knocked by the forks - have tried various combinations of settings for 'setStepSize' and 'setAccuracy' but nothing seems to help that much!
Here is the code for the cages:
Code:
top =cage1.collideBox(node = 'Cage_Top',bounce=0)
top.setFriction(0.5)
bottom = cage1.collideBox(node = 'Cage_Bottom',bounce=0)
bottom.setFriction(0.5)
side1=cage1.collideBox(node='Left_Ones',bounce=0)
side2=cage1.collideBox(node='Right_Ones',bounce=0)
cage1.enable(viz.COLLIDE_NOTIFY)
-the pallets are dealt with in a similar manner.

The forklift is a purely physics based affair rather than being based upon animations or links (as I had already mentioned last yr), with the blades of the fork lift being dealt with like this:
Code:
fork1 = flt.getChild('Blades')
forks = fork1.collideBox(bounce=0)
fork1.enable(viz.COLLIDE_NOTIFY)
forks.setFriction(1)
What, in your opinion, would be the best approach to sort this out? I would REALLY appreciate any light you may be able to throw on this problem,

Thanks in advance
Reply With Quote
  #5  
Old 06-22-2009, 04:21 PM
Jeff Jeff is offline
WorldViz Team Member
 
Join Date: Aug 2008
Posts: 2,471
Using a timer and joy.isButtonDown should work. Have you tried something like this.
Code:
def checkButtons():
	if joy.isButtonDown(1):
		IncrementAngle(0.1)
	if joy.isButtonDown(2):
		IncrementAngle(-0.1)
	
vizact.ontimer(0,checkButtons)
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 03:34 AM.


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