Hi. I posted this in the Game Engine side, but it might be more suited here, since I will eventually want the ease of simply changing numbers in a python code to result in different rotation values.
I’m modeling a robot arm with 6 joints, each of with allows free movement along one axis. I’m using the bge to control it, but my lack of experience with Python is making things a bit challenging. What I would like is if I could input a degree angle into the “Rotation_Amount” section and have the model rotate there by clicking the space bar once. This will only be for one of the joints, and then I’ll adjust accordingly for the other joints.
I’ve made two different blend files to try and get this, but both have slight issues. I’ve used 100 degrees as my test angle. Help would be greatly appreciated.
import bge
import time
from math import pi
def main():
Rotation_Amount = 100
cont = bge.logic.getCurrentController()
owner = cont.owner
keyboard = bge.logic.keyboard
spaceKey = bge.logic.KX_INPUT_JUST_ACTIVATED== keyboard.events[bge.events.SPACEKEY]
unit = pi/180
if spaceKey and Rotation_Amount > 0:
for n in range(0,Rotation_Amount,1):
owner.applyRotation((0,0,unit), True)
if spaceKey and Rotation_Amount < 0:
for n in range(0,0-Rotation_Amount,1):
owner.applyRotation((0,0,-unit), True)
main()
This file rotates the model to its desired location, but it just sort of “teleports” there. I want to visually see the model rotate itself over to that spot. I would imagine there’s a way to finagle with this code to get it to do that, but I haven’t found a way yet. I’m thinking that the “teleporting” effect is actually just the speed of the computer; i.e. it rotates there, but extremely fast. I tried slowing it down using a time.sleep function, but it didn’t work. Is there any way to get this to work?
Then, here’s the other code.
import bge
from math import pi
def rotate():
cont = bge.logic.getCurrentController()
owner = cont.owner
keyboard = bge.logic.keyboard
spaceKey = bge.logic.KX_INPUT_ACTIVE == keyboard.events[bge.events.SPACEKEY]
Rotation_Amount = 100
curRot = owner.localOrientation.to_euler()[2]
unit = pi/180
Rotation_Radians = Rotation_Amount*unit
if spaceKey and curRot < Rotation_Radians:
owner.applyRotation((0,0,unit), True)
if spaceKey and curRot > Rotation_Radians:
owner.applyRotation((0,0,-unit), True)
rotate()
This code shows the visuals of the model actually rotating to its destination, but the problem here is that I have to hold the spacebar to get it there; I just want to be able to press it once and have the model rotate on its own. This one also has the problem of “jittering” once it gets to its destination because of holding the spacebar down, which obviously wouldn’t be a problem if it were just pressed once.
Any help on the matter would be great. Thanks!