 Rotate an object in blender engine

Hi!

I have the following script on a python controller at BGE

import serial #Comunicacion Serial
import bge #Funciones propias de blender
import bpy
import math
from bpy.props import *
from mathutils import Vector

ser = serial.Serial('COM5', baudrate = 9600, timeout = 1)

leitura = leitura.decode("utf_8")

if leitura == "&" :

leitura = leitura[1:] #Eliminamos el detector de errores
leitura = leitura[:-1] #Eliminamos el final de linea

cont = bge.logic.getCurrentController()
leme = cont.owner

rotation = leme.localOrientation.to_euler()

rotacao_atual = rotation.y * -1

if (rotacao_atual != angulo):
if (rotacao_atual > angulo):
leme.applyRotation([0,0.1, 0],1)
else:
leme.applyRotation([0,-0.1, 0],1)
else:
leme.applyRotation([0, 0, 0],True)

ser.close()

Its function is to read a value received from a serial connection, decode it and then convert its integer value to radians. I read the current rotation of my object and then logically so that the object rotates to the desired angle. In terms the code works, the problem is that apparently the object does not rotate from 0 to 360 degrees.

For example, if my angle is 180 degrees, and the current rotation is 0 degrees, applyRotation increases by 0.1 radius on the object but only up to 2.60 radians, after that value returns to 0 radians and decreases, then rises to 2.60 again. and it’s in an infinite loop, does anyone know why that

and something like this?

import serial #Comunicacion Serial
import bge #Funciones propias de blender
import bpy
import math
from bpy.props import *
from mathutils import Vector

ser = serial.Serial('COM5', baudrate = 9600, timeout = 1)

leitura = leitura.decode("utf_8")

if leitura == "&" :

leitura = leitura[1:] #Eliminamos el detector de errores
leitura = leitura[:-1] #Eliminamos el final de linea

cont = bge.logic.getCurrentController()
leme = cont.owner

rotation = leme.localOrientation.to_euler()

rotacao_atual = rotation.y * -1

if (rotacao_atual != angulo):

if (rotacao_atual > angulo):
rotation.y += rot_degree

elif (rotacao_atual < angulo):
rotation.y -= rot_degree

leme.worldOrientation = rotation

ser.close()

hi thanks for contributing

I tested the implementation that recommended me, but still the same problem remains. My angle is 180 degrees, the current angle is 0 degrees. So he must walk from 0 to 180 degrees, see that when it arrives at approximately 3.15 (approximately 180 degrees) it reverses the direction of rotation going to approximately -2.97 (approximately 170 grus), thus being in an infinite loop. What can it be?

off: What if I used:

helm = bpy.context.active_object
helm.rotation_mode = ‘XYZ’ #change rotation order
bpy.ops.transform.rotate (value = angle, axis = (0.0,1.0,0.0))

But when I press P nothing happens, only after exiting game mode by pressing ESC that the angle changes, not at run time, why is it?

BPY code does NOT work in Blenderplayer (it occasionally works in Blender View-Port tho)

This is why your BPY code works in Blender but not Blenderplayer.

If this is not what you were asking for sorry Hi @RandomPerson, thaks for you contribuition

Apparently it doesn’t work at all, is there anything to work around this problem? What about the other question, any tips?

BPY does indeed not work in BGE.

So your looking to rotate the wheel, well we didn’t use the math.degrees() correctly throughout the script.
I made you an example that at least works for a 179.9 degree rotation both ways and will go back to normal position if you release the key.

so is this what you are looking for?

steer test.blend (545.1 KB)

Hi again @Cotaks

thank you so much for contributing to my question again, thanks for the effort.

I’m both new to python and blender. I couldn’t understand your code because I haven’t seen keyboard sensors yet. In my personal project, I receive commands via serial connection from an arduino, so I went straight to learn about it.

I got what I wanted after failed attempts, so I decided to stop and analyze. First, the rotation (I don’t know why) reports values ​​as follows:

0 to 180 degrees: 0 to 3.14 radians

180 to 360 = -3.14 to <0 radians

So my angle value coming from the arduino would be 270 degrees, ie approximately 4.71, my rotation would loop, because after 3.14 it goes back to -3.14 for> 180 degrees.

So I took a note sheet and went back to the first class of algorithms and solved the problem with this code:

if (read> = 180 and read <= 360):
read = (read - 180) * -1

In this section, when my angle value is between 180 and 360 I convert it to a value between 0 and 180 negative, so according to the value that my program rotation offers.

Now I do a logic evaluating the whole situation:

if (current_ rotation! = angle):
if (angle> 0):
if (current_rotation <angle):
rotation.y + = rot_degree
elif (current_rotation> angle):
rotation.y - = rot_degree

elif (angle <0):
if (current_rotation> 0):
if (current_rotation <angle):
rotation.y + = rot_degree
elif (current_rotation> angle):
rotation.y + = rot_degree
elif (current_ rotation <0):
if (current_rotation> angle):
rotation.y - = rot_degree
elif (current_rotation <angle):
rotation.y + = rot_degree

leme.localOrientation = rotation

This way my blender’s virtual rudder is in the same position as the rudder attached to the arduino.

I know that from now on I will need even more help, and I will be happy to see you here on other issues of mine, thanks for now …

basically all you have to do is changing the sensors in the script to your commands for left and right
then you can remove those bricks

because you indeed use radians, for degrees you need to convert the radians