Here is some code to fit a door to two walls. It works like this;
1) When it creates a wall it attaches a plane to the left and right edge of the wall (You could make these invisible in a production environment)
2) It creates a door and sets it's pivot point to the left so it can swing open/closed
3) When it fits the door to the gap between the walls it takes the left marker of the first wall and the right marker of the second wall and uses those objects to calculate the gap. The the width of the door is gap/door_width. It also positions the door to the world position of the right edge marker of the first wall.
This way the orientation of the walls doesn't matter.
// Project: door
// Created: 21-12-14
// show all errors
SetErrorMode(2)
// set window properties
SetWindowTitle( "door" )
SetWindowSize( 1024, 768, 0 )
SetWindowAllowResize( 1 ) // allow the user to resize the window
// set display properties
SetVirtualResolution( 1024, 768 ) // doesn't have to match the window
SetOrientationAllowed( 1, 1, 1, 1 ) // allow both portrait and landscape on mobile devices
SetSyncRate( 30, 0 ) // 30fps instead of 60 to save battery
SetScissor( 0,0,0,0 ) // use the maximum available screen space, no black borders
UseNewDefaultFonts( 1 )
type tWall
id as integer
r as integer // plane at right hanbd edge
l as integer // plane at left hand edge
width as float
endtype
type tDoor
id as integer
width as float // origional width of door
endtype
walls as tWall[]
door as tDoor
walls.insert(CreateWall(5.0))
walls.insert(CreateWall(3.0))
SetObjectPosition(walls[0].id, -3, 0, 0)
SetObjectPosition(walls[1].id, 2.5, 0, 0)
door = CreateDoor(1.0)
FitDoorToWalls(walls[0], walls[1], door)
RotateStep as integer = 3
do
RotateObjectLocalY(door.id, RotateStep)
if GetObjectAngleY(door.id) > 60.0
RotateStep = RotateStep * -1
endif
if GetRawKeyPressed(39)
SetObjectPosition(walls[1].id, GetObjectX(walls[1].id)+0.5, GetObjectY(walls[1].id), GetObjectZ(walls[1].id))
FitDoorToWalls(walls[0], walls[1], door)
endif
if GetRawKeyPressed(37)
SetObjectPosition(walls[1].id, GetObjectX(walls[1].id)-0.5, GetObjectY(walls[1].id), GetObjectZ(walls[1].id))
FitDoorToWalls(walls[0], walls[1], door)
endif
if GetRawKeyPressed(asc("O"))
if Round(GetObjectAngleY(walls[1].id)) = 0
RotateObjectLocalY(walls[1].id, 90)
SetObjectPosition(walls[1].id, GetObjectX(walls[1].id)-walls[1].width / 2.0, 0, -walls[1].width / 2.0)
else
SetObjectRotation(walls[1].id, 0, 0, 0)
SetObjectPosition(walls[1].id, GetObjectX(walls[1].id)+walls[1].width / 2.0, 0, 0)
endif
endif
Print( "Right-Move right, Left-Move left, O-Flip orientation")
Sync()
loop
function CreateWall(width as float)
wall as tWall
// This function will create a plane and attach markers to the right and left hand sides of the plane
wall.id = CreateObjectPlane(width, 1)
wall.r = CreateObjectPlane(.2,.2) : SetObjectColor(wall.r, 0xff, 0, 0, 0xff)
wall.l = CreateObjectPlane(.2,.2) : SetObjectColor(wall.l, 0xff, 0, 0, 0xff)
SetObjectPosition(wall.r, width / 2.0, 0, 0) // Set the position of the marker to the right hand edge
SetObjectPosition(wall.l, -(width / 2.0), 0, 0) // Set the position of the marker to the left hand edge
FixObjectToObject(wall.r, wall.id)
FixObjectToObject(wall.l, wall.id)
wall.width = width
endfunction wall
function CreateDoor(width as float)
door as tDoor
// This function will create a plane and set it's pivot point to it's left hand side
door.id = CreateObjectPlane(width,1)
SetObjectColor(door.id, 0, 0xff, 0, 0xff)
SetObjectPosition(door.id, width / 2.0, 0, 0)
FixObjectPivot(door.id)
door.width = width // Set the doors pivot to the left hand side of the plane
endfunction door
function FitDoorToWalls(wall1 as tWall, wall2 as tWall, door as tDoor)
width as float
gap as float
scale as float
// Set the doors position to the right edge marker of the first wall
SetObjectPosition(door.id, GetObjectWorldX(wall1.r), GetObjectWorldY(wall1.r), GetObjectWorldZ(wall1.r))
// Calculate the distance between the right hand marker of the first wall and the laft hand marker of the second wall
gap = Distance(wall1.r, wall2.l)
// Calculate the xscale of the door
scale = gap / door.width
SetObjectScale(door.id, scale, 1, 1)
endfunction
function Distance(f as integer, t as integer)
dist as float
dist = sqrt((GetObjectWorldX(f) - GetObjectWorldX(t))^2 + (GetObjectWorldZ(f) - GetObjectWorldZ(t))^2 )
endfunction dist
