Hello all,
I am new to the forums, but have been playing around with Leadwerks for about 6 months or so. My experience is in 3D modeling, texture creation, and world building, but I am trying to learn a little about Lua scripting. My capabilities are pretty much limited to taking existing examples, picking them apart, splicing them together, and tweaking them to try and achieve the result I am going for.
I recently started playing around with hinges and have had pretty much been able to figure them out with the locker example except for one problem. Right now, anytime I place a hinge that runs vertically (like a door) it works great. When I place one horizontally however, I run into a problem (like an oven door, or a toilet seat). In this case, the hinge only works correctly when the model is aligned to the hinge axis. If I rotate the model, the door rotates with it, but the hinge still tries to rotate toward the global hinge axis. Here is my lua script:
require("scripts/class")
local class=CreateClass(...)
function class:CreateObject(model)
local object=self.super:CreateObject(model)
function object:ClearJoint()
if self.door~=nil then
self.door:Free()
self.door=nil
self.joint=nil
end
end
function object:UpdateJoint()
self:ClearJoint()
if self.hinge~=nil then
self.door=LoadModel("abstract::x_stove_01_Door01.gmf")
self.door:SetPosition( self.hinge:GetPosition(1) )
self.door:SetRotation( self.model.rotation )
self.door:SetMass(1)
self.joint = CreateJointHinge( self.model, self.door,self.door:GetPosition(1), Vec3(1,0,0) )
self.joint:SetLimits(0,60)
self.door:SetCollisionType(COLLISION_PROP)
end
end
function object:Reset()
self:UpdateJoint()
end
function object:Free(model)
self:ClearJoint()
self.super:Free()
end
function object:Init()
self.model:SetCollisionType(COLLISION_PROP)
self.hinge=self.model:FindChild("doorhinge")
self:UpdateJoint()
end
object:Init()
end
And here is the result. The object on the left has not been rotated in the editor, the object on the right has.
My understanding of the script is limited, but I think the problem is in this line:
self.joint = CreateJointHinge( self.model, self.door,self.door:GetPosition(1), Vec3(1,0,0) )
or more specifically the Vec3(1,0,0) value. This is a fixed orientation with respect to the world and not the model, right? What I need is for the hinge axis to adjust with the rotation of the model. Is this possible, and if so what would the code look like to accomplish it? Please feel free to give any advice on the rest of the script as well if I am doing anything else wrong or inefficiently.