Orientate plane with off-center origin

Hello,

Atm. i created a box selection, so far everything is working. But a problem rises up.

How can i orientate a plane (the box selection) with the camera’s orientation while the planes origin is in the top left corner?

So as said the box selection it self works like a charm (from top down perspective) but if i rotate my camera the orientation will be off, because rotating an object with an off-center origin will of course rotate in an other way then when the origin was in the dead center.

Anyone knows a way to rotate the plane the same way as when the origin was at the center, or how to calculate the same ‘rotation/orientation’?

Thank you.

going to be a math nightmare.

here is a workaround that might be useful.

orientation.blend

updated: removed the parented empty from the plane.

Thanks for the option, that’s a nice workaround, but after more testing aligning don’t need the group/center it aligns without it the same, so it’s actually something else that messes it up, maybe it’s the scaling part, not sure because it does work.

so you want to make a box select system ?

Well i want to fix mine, but yes that’s exactly what i’m making/after.

i cant look at the blend until i get back to my pc, but this sounds like a screen space solution.

  • activate the mouse, get logic.mouse.position
  • move the mouse and scale box to difference of mouse position X and Y
  • release mouse, loop through objects testing getScreenPosition.

to line up the box, just use an overlay camera and optimize the position differences to ortho scale. why someone wouldnt use ortho for an overlay is beyond me.

if all your objects are at a similar elevation, you could create a selection box on the ground, and not screen space, and test a collision sensor.

That could be but i’m not using screenspace, just the 3d space.
by using 3 sensors, collision (to grab the selected objects), left click (truepulse) and mouse over any.

with this i get the hit vectors (click and the dragging vector). then i create a diagonal vector, with that vector i calculate the scaling of the plane.

The only problem i have is that or my calculation is wrong or the rotation is doing wierd stuff because setting just the z orientation doesn’t do anything either.

here a video:

switch your RTS_controls.py with this and it works.

####################################################
# RTS building placement + Box selection by cotaks #
#           for preview/testing only               #
####################################################

# Grey cubes can be selected, orange cubes will be ignored
# P and left click + drag
# middlemouse: hold to rotate camera, scroll to zoom in/out
# plane/box selection is in top right of camera (in 3d view)

# i removed the group, because it don't need one, seems it actually alligns correctly. but with a twist.
# Top down everything works how it should be. Rotate the camera (look into a corner) and select something.
# It seems that the alignment works but also flipped the selection direction? very strange. 
# this is the same with- and without the group option

# any help would be apriciated, thanks!

import math

def box_select(cont):
    
    own = cont.owner
    
    col_sen = cont.sensors['Collision']
    l_click = cont.sensors['Mouse']
    moa = cont.sensors['moa']

    if not 'box_selected' in own:
        own['selected'] = own.get('selected', []) #global selection(with and without box selection)
        own['box_selected'] = [] #purely the box selected objects
            
    if l_click.positive and moa.positive:
        
        own.color = [0,1,1,0.2]
        
        if not 'pos' in own:
            own['pos'] = moa.hitPosition.copy()
            
        else:
            hook = own.scene.active_camera.parent
            own.worldOrientation = hook.worldOrientation
            
            own['pos2'] = moa.hitPosition.copy()
            own.worldPosition = [own['pos'][0],own['pos'][1], 0.02]
            
            point1 = own['pos'] #click position
            point2 = own['pos2'] # drag/release position
            
            x = point2.x - point1.x
            y = point2.y - point1.y
            
            rz = hook.worldOrientation.to_euler().z
            rx = x * math.cos(-rz) - y * math.sin(-rz)
            ry = x * math.sin(rz) - y * math.cos(rz)
            
            own.scaling = [rx,ry,0]
                        
            if col_sen.positive:

                #only select units with a property unit
                objects = [obj for obj in col_sen.hitObjectList if 'unit' in obj]
                
                if objects:
                    own.color =[1,0,0,0.2]
                    own['box_selected'] = objects
        
                else:
                    own['box_selected'] = ''
            else:
                    own['box_selected'] = ''
                    
    elif 'pos2' in own:
        
        #make the plane/box smaller and hide it below terrain
        own.scaling = [0.1, 0.1, 0]
        own.worldPosition = [0,0, -1000]
        
        #convert selection to selected objects
        if own['box_selected']:
            
            for obj in own['box_selected']:
                if obj not in own['selected']:
                    own['selected'].append(obj)

            del own['box_selected']   
        
        del own['pos2']  
        del own['pos']    
    
    #debug section -> properties and code below for debugging only (can/may be removed)
    if 'box_selected' in own:
        own['debug_selection'] = str(own['box_selected']) 
    if 'selected' in own:      
        own['debug_selected'] = str(own['selected'])              
        
1 Like

Thanks a lot, it aligns perfectly now.