Hey, i had a bit of a go and had a pretty successful go at it as well.
It is all possible within one line hehe... well it is simply the code below placed after all movements have occurred.
- Code: Select all
physics.setRotation(bodyID, entity.getWorldOrientation(obj));
What i used: I made a door with the pivot in the bottom right corner and added a rigidbody with kinematic physics. I also used the convex mesh hull as it will always surround the mesh correctly and is much easier than the box as it requires an offset.
Added a microscript and programmed a door from it and used the above command to set the kinematic body position.
Here is the full script:
- Code: Select all
-- | User Vars
reverse = -1; -- | Set this to -1 to rotate the door the other way. Set 1 to move positive.
doorSpeed = 150; -- | Controls how fast the door opens and closes. Its large as it is using timedelta.
openAngle = 90; -- | This is the maximum angle the door can open. Great for blocked doors.
closeAngle = 0; -- | This is the close angle. 0 is starting point.
-- | System Vars
opendoor = false;
closedoor = false;
currentAngle = 0;
bodyID = -1;
obj = 0;
function onInit(objID)
obj = objID;
end
function postInit()
bodyID = physics.getBodyID(obj);
end
function onKeyDown( key )
if(key=="f")then
if(opendoor == true)then
opendoor = false;
closedoor = true;
elseif(opendoor == false)then
opendoor = true;
closedoor = false;
end
end
if(key=="e")then
opendoor = false;
closedoor = true;
end
end
function onUpdate( timeDelta )
elapsedTime = timeDelta;
if(elapsedTime > 0.25)then elapsedTime = 0.25; end
if(bodyID < 0)then return; end
if(opendoor == true )then
speed = doorSpeed * elapsedTime;
if(reverse > 0)then
if(currentAngle < openAngle)then
entity.turn(obj, 0, speed, 0);
currentAngle = currentAngle + speed;
end
elseif(reverse < 0)then
if(currentAngle > -openAngle)then
entity.turn(obj, 0, -speed, 0);
currentAngle = currentAngle - speed;
end
end
updatePhysics();
end
if(closedoor == true)then
if(reverse > 0)then
if(currentAngle > closeAngle)then
entity.turn(obj, 0, -speed, 0);
currentAngle = currentAngle - speed;
end
elseif(reverse < 0)then
if(currentAngle < -closeAngle)then
entity.turn(obj, 0, speed, 0);
currentAngle = currentAngle + speed;
end
end
updatePhysics();
end
end
function updatePhysics()
physics.setRotation(bodyID, entity.getWorldOrientation(obj));
end
Note: This door mechanic will work on a door that is set at any angle on the Y axis. It means you can rotate the door to any angle and then the door will rotate to max and back again.
I will be releasing a new demo soon which has an opening door and will be converting this script into a dynamic property script for re usability.
Hope this helps
![Very Happy :D](./images/smilies/icon_e_biggrin.gif)