Rotation of cuboid on a plane.
Cuboid can have any dimensions, origin must be centered.
Python only, no helpers (empties) required.
import bge
import math
from mathutils import Vector
scene = bge.logic.getCurrentScene()
cont = bge.logic.getCurrentController()
own = cont.owner
if 'timer' not in own:
own['timer']=0
own['move_x']=0
own['move_y']=0
keyboard = bge.logic.keyboard.inputs
W=bge.events.WKEY
A=bge.events.AKEY
S=bge.events.SKEY
D=bge.events.DKEY
own['move_x']=0
own['move_y']=0
JUST_ACTIVATED = bge.logic.KX_INPUT_JUST_ACTIVATED
if (JUST_ACTIVATED in keyboard[A].queue):
own['move_y']=1
if (JUST_ACTIVATED in keyboard[D].queue):
own['move_y']=-1
if (JUST_ACTIVATED in keyboard[W].queue):
own['move_x']=-1
if (JUST_ACTIVATED in keyboard[S].queue):
own['move_x']=1
speed=3 # 1,2,3,5,6,9,10,15,18,30 or 90
if abs(own['move_x']+own['move_y'])!=0 and own['timer']==0:
vec_dim=own.blenderObject.dimensions/2
vec_dim_global=own.worldOrientation.copy() @ vec_dim
own['axis'] = Vector([own['move_y'],own['move_x'],0])*math.radians(1*speed)
offset = Vector([abs(vec_dim_global[0])*own['move_x'],-abs(vec_dim_global[1])*own['move_y'],-abs(vec_dim_global[2])])
own['pivot'] = own.worldPosition.copy() + offset
own['offset'] = own.worldOrientation.copy().inverted() @ -offset
own['timer']=1
if 0<own['timer']<91:
own.applyRotation(own['axis'],False)
own['timer']+=1*speed
own.worldPosition = own.worldOrientation @ own['offset'] + own['pivot']
bge.render.drawLine(own.worldPosition ,own['pivot'],[1.0, 0.0, 0.0])
if own['timer']>90: own['timer']=0
UPBGE_0.3_Rotate.blend (977.6 KB)