Cuboid Rotation on a plane (UPBGE 0.3)

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)

2 Likes

Version for blender2.xx or UPBGE 0.2.5.

Differences:

  1. Keyboard inputs
  2. all matrix-multiplications @ replaced with *
  3. Object Dimensions are not taken from the object but are defined manually.

Blender2xx_rotate_Cuboid_on plane.blend (491.8 KB)