View Full Version : How to create mini car driving by wiimote
superantTTY
05-13-2010, 09:46 AM
Hello, all !!
Like the example "joystick_driving.py", I can control the mini car driving by joystick.
However, now I want to control the mini car driving by wiimote.
I think that key points may be one import,import vizjoy, and two definition,def joyMove(e) & def updateCar().
And there is a Initialize global variable,joyPOS, do I need to initial something like that for wiimote to create the car driving??
How can I do ??
Thank you very much!!
Best Regards!
You could modify the updateCar function to use the wiimote buttons to control the car. For example, you could replace:
#Use y position of joystick to compute forward car movement
forwardMovementAmount = viz.elapsed() * MOVE_SPEED * -joyPOS[1]
car.setPosition( [ 0, 0, forwardMovementAmount], viz.REL_LOCAL )
with:
if wiimote.isButtonDown(wii.BUTTON_UP):
forwardMovementAmount = viz.elapsed() * MOVE_SPEED
car.setPosition( [ 0, 0, forwardMovementAmount], viz.REL_LOCAL )
superantTTY
05-14-2010, 06:12 AM
You could modify the updateCar function to use the wiimote buttons to control the car. For example, you could replace:
#Use y position of joystick to compute forward car movement
forwardMovementAmount = viz.elapsed() * MOVE_SPEED * -joyPOS[1]
car.setPosition( [ 0, 0, forwardMovementAmount], viz.REL_LOCAL )
with:
if wiimote.isButtonDown(wii.BUTTON_UP):
forwardMovementAmount = viz.elapsed() * MOVE_SPEED
car.setPosition( [ 0, 0, forwardMovementAmount], viz.REL_LOCAL )
I already try the code that you provide, the car can move, but something is strange...
According to "joystick_driving.py", the car can move smoothly instead of moving step by step. For example, when I press the joystick's up button or left button, it can always move forward or turn left until I release the button.
However, now I try the code you provide, it can move, but its movement is step by step. For example, when I press the wiimote's up button or left button, it can just move a small step and then stop. If I keep pressing the button, it becomes not to move!
I would like to use wiimote to make the same preference.
How can I modify it ??
Thanks for your help
Kevin
Can you post the script you are using?
superantTTY
05-15-2010, 09:07 PM
Of course! This is my script.
################################################## #########
MOVE_SPEED = 10
TURN_SPEED = 90
import viz
viz.go()
import vizinfo
vizinfo.add('This is a car driving test by wiimote')
viz.phys.enable()
ground = viz.add('tut_ground.wrl')
ground.collidePlane()
car = viz.add('mini.osgx')
car.setPosition(0,0,-10)
car.setScale(1,1,1)
car.setEuler(0,0,0)
car.collideBox()
wii = viz.add('wiimote.dle')
wiimote = wii.addWiimote()
wiimote.led = wii.LED_1
def updateCar():
if wiimote.isButtonDown(wii.BUTTON_UP):
forwardMovementAmount = viz.elapsed() * MOVE_SPEED
car.setPosition( [ 0, 0, forwardMovementAmount], viz.REL_LOCAL )
if wiimote.isButtonDown(wii.BUTTON_DOWN):
forwardMovementAmount = viz.elapsed() * MOVE_SPEED
car.setPosition( [ 0, 0, -forwardMovementAmount], viz.REL_LOCAL )
if wiimote.isButtonDown(wii.BUTTON_LEFT):
rotationAmount = viz.elapsed() * TURN_SPEED
car.setEuler( [ -rotationAmount, 0 , 0 ] , viz.REL_LOCAL )
if wiimote.isButtonDown(wii.BUTTON_RIGHT):
rotationAmount = viz.elapsed() * TURN_SPEED
car.setEuler( [ rotationAmount, 0 , 0 ] , viz.REL_LOCAL )
vizact.onsensordown(wiimote,wii.BUTTON_UP,updateCa r)
vizact.onsensordown(wiimote,wii.BUTTON_DOWN,update Car)
vizact.onsensordown(wiimote,wii.BUTTON_LEFT,update Car)
vizact.onsensordown(wiimote,wii.BUTTON_RIGHT,updat eCar)
viz.MainView.move(0,0,-15)
################################################## #########
And I also have a question.
Why my car can jump in the beginning? I'd like it to be static in the beginning.
How can I do ?
Thanks for your help, Jeff !!
Kevin
superantTTY
05-15-2010, 09:28 PM
Notice: vizact.onsensordown(........................,updat eCar'''the letters can be separate''')
It's updateCar.
Darkmax
05-17-2010, 07:17 AM
use code tags please try this update car:
#Update position of car every frame
def updateCar():
if wiimote.isButtonDown(wii.BUTTON_UP):
forwardMovementAmount = viz.elapsed() * MOVE_SPEED
car.setPosition( [ 0, 0, forwardMovementAmount], viz.REL_LOCAL )
if wiimote.isButtonDown(wii.BUTTON_DOWN):
backwardMovementAmount = viz.elapsed() * MOVE_SPEED
car.setPosition( [ 0, 0, -backwardMovementAmount], viz.REL_LOCAL )
if wiimote.isButtonDown(wii.BUTTON_RIGHT):
rotationAmount = viz.elapsed() * TURN_SPEED
car.setEuler( [ rotationAmount, 0 , 0 ] , viz.REL_LOCAL )
if wiimote.isButtonDown(wii.BUTTON_LEFT):
rotationAmount = viz.elapsed() * TURN_SPEED
car.setEuler( [ -rotationAmount, 0 , 0 ] , viz.REL_LOCAL )
vizact.ontimer(0,updateCar)
And i think your car jump at first because the physics, maybe because they are colliding
superantTTY
05-18-2010, 05:18 AM
Thank you Darkmax ! It can run smoothly !!
^^y
vBulletin® v3.8.7, Copyright ©2000-2024, vBulletin Solutions, Inc.