Rotate Script Problem

Well I’m to the point that logic bricks alone will not cut it for my games.
It’s time to learn Python :smiley:

I’m starting from scratch here and I’m stuck already.
All I’m trying to do at the moment is rotate a plane.
I have a plane and a camera in my scene.

I’ve set up 4 sensors: UpSen, DownSen, LeftSen, RightSen, each being a keyboard sensor for the arrow keys.
Then I’ve got them all wired to a Python controller which is wired to a motion actuator called actRotate.

My Python controller fires up my first script:


import GameLogic

contr=GameLogic.getCurrentController()
owner=contr.getOwner()

sensors=contr.getSensors()

actRotate=contr.getActuator("actRotate")

if UpSen.isPositive():
	actRotate.setDRot(0.0,1,0.0)
elif DownSen.isPositive():
	actRotate.setDRot(0.0,-1,0.0)
elif RightSen.isPositive():
	actRotate.setDRot(1,0.0,0.0)
elif LeftSen.isPositive():
	actRotate.setDRot(-1,0.0,0.0)
else:
	actRotate.setDRot(0.0,0.0,0.0)

I know this is an uber noob attempt, but it’s the best I have for now.
Any tips on how to do this the right way?
Thanks.

after you ‘setDRot’ you need to activate the actuator
like:


	#activate 
	actRotate.setDRot(0.0,1,0.0)
	GameLogic.addActiveActuator(actRotate,1)
	
	#deactivate
	GameLogic.addActiveActuator(actRotate,0)

Cool!
I’ve got it working, sort of:


import GameLogic

contr=GameLogic.getCurrentController()
owner=contr.getOwner()

sensors=contr.getSensors()

actRotate=contr.getActuator("actRotate")

UpSen=contr.getSensor("UpSen")
DownSen=contr.getSensor("DownSen")
RightSen=contr.getSensor("RightSen")
LeftSen=contr.getSensor("LeftSen")

if UpSen.isPositive():
	actRotate.setDRot(0.0,0.09,0.0,1)
	GameLogic.addActiveActuator(actRotate,1)
elif DownSen.isPositive():
	actRotate.setDRot(0.0,-0.09,0.0,1)
	GameLogic.addActiveActuator(actRotate,1)
elif RightSen.isPositive():
	actRotate.setDRot(0.09,0.0,0.0,1)
	GameLogic.addActiveActuator(actRotate,1)
elif LeftSen.isPositive():
	actRotate.setDRot(-0.09,0.0,0.0,1)
	GameLogic.addActiveActuator(actRotate,1)
else:
	actRotate.setDRot(0.0,0.0,0.0,1)

GameLogic.addActiveActuator(actRotate,0)

But why will it only rotate in 1 direction at a time?
What I mean is that if I hold down 1 key and press a second key, it only goes in the direction of the second key instead of a combination of the two keys?

Try changing your elif’s to if’s.

Thanks, but no luck. Still the same result.
Oh, I need to clarify that, it stays with the first key I press and ignores the second direction.

i’m not 100% sure why,
but i think is because of the conditions set up
if one sensor is positive
then python will call ‘if’ or ‘elif’ statement, which ever one returns true
and the rest of the code will be skipped, untill it is evaluated again

one work around would be to

split up/down movement from the left/right

and use two motion actuators like so:


import GameLogic 

contr=GameLogic.getCurrentController() 
owner=contr.getOwner() 

sensors=contr.getSensors() 

actRotate0=contr.getActuator("actRotate0") 
actRotate1=contr.getActuator("actRotate1") 

UpSen=contr.getSensor("UpSen") 
DownSen=contr.getSensor("DownSen") 
RightSen=contr.getSensor("RightSen") 
LeftSen=contr.getSensor("LeftSen") 

if UpSen.isPositive(): 
   actRotate0.setDRot(0.0,0.009,0.0,1) 
   GameLogic.addActiveActuator(actRotate0,1) 
elif DownSen.isPositive(): 
   actRotate0.setDRot(0.0,-0.009,0.0,1) 
   GameLogic.addActiveActuator(actRotate0,1) 
else:
	actRotate0.setDRot(0.0,0.0,0.0,1) 


if RightSen.isPositive(): 
   actRotate1.setDRot(0.009,0.0,0.0,1) 
   GameLogic.addActiveActuator(actRotate1,1) 
elif LeftSen.isPositive(): 
   actRotate1.setDRot(-0.009,0.0,0.0,1) 
   GameLogic.addActiveActuator(actRotate1,1) 

else:
	actRotate1.setDRot(0.0,0.0,0.0,1) 

GameLogic.addActiveActuator(actRotate0,0) 
GameLogic.addActiveActuator(actRotate1,0) 


Yes! That’s what I’m after, thank you.

O.K. yeah. It’s not ignoring the second if, the code is rezeroing the prior value. You could also use a flag or get the rotation value from the actuator and only change the needed value. It might not change even then. I’ve found that two calls to the actuator on the same pulse does give funny results and sometimes one command is ignored. Another way is to store values in variables and make a single change.



xValue = 0
yValue = 0
if UpSen.isPositive():
   yValue = .09
if DownSen.isPositive():
   yValue = -.09
if RightSen.isPositive():
   xValue = .09
if LeftSen.isPositive():
   xValue = -.09
 actRotate.setDRot(xValue,yValue,0.0,1)
 GameLogic.addActiveActuator(actRotate,1)


Might work. Anyway, it’s fun to experiment and try different ways to see if it will work or not. You’ve got the right idea about using python. Start with a simple test, do lots of print statements and check your results in the dos box to see if it’s doing what you want.

yes so much cleaner!
use fireside’s example :slight_smile:

Fireside’s idea does indeed look cleaner, but when I plug it into my script like so:



import GameLogic 

contr=GameLogic.getCurrentController() 
owner=contr.getOwner() 

sensors=contr.getSensors() 

actRotate=contr.getActuator("actRotate") 


UpSen=contr.getSensor("UpSen") 
DownSen=contr.getSensor("DownSen") 
RightSen=contr.getSensor("RightSen") 
LeftSen=contr.getSensor("LeftSen") 

xValue = 0
yValue = 0
if UpSen.isPositive():
   yValue = .09
if DownSen.isPositive():
   yValue = -.09
if RightSen.isPositive():
   xValue = .09
if LeftSen.isPositive():
   xValue = -.09
 actRotate.setDRot(xValue,yValue,0.0,1)
 GameLogic.addActiveActuator(actRotate,1)


nothing happens when I press the keys.

I never said it would work. :stuck_out_tongue:

When nothing happens, the first thing to do is to make the dos box visible by pressing the button for it on the task bar. If there are any script problems, they will show up as errors. Usually, by reading those errors, you can figure out what’s going wrong. If it doesn’t say anything, then it’s a logic error, but I don’t see one right off. It’s just changing the actuator in the same way. I didn’t include the false reading at the end:

GameLogic.addActiveActuator(actRotate,0)

I think I set the:
addActiveActuator(actRotate,1)

first and the set the DRot after when I do it.

Oh, it might have freaked out on the floats. I probably should have said:
xValue = 0.0
yValue = 0.0

Oh man, my bad.
When I checked the dos box it said it couldn’t find the actuator.
I forgot to get rid of the second actuator and rename the first one.
Your code works great, thanks.