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)