BrokenPillar Posted February 4, 2010 Share Posted February 4, 2010 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. Quote Vista | AMD Dual Core 2.49 GHz | 4GB RAM | nVidia GeForce 8800GTX 768MB Online Portfolio | www.brianmcnett.com Link to comment Share on other sites More sharing options...
Fredrik Hansson Posted February 4, 2010 Share Posted February 4, 2010 had some troubles with something similar myself a few days ago and i found it easiest to first copy the main objects rotation into a temporary variable then set the rotation to 0,0,0 create the hinges and finally rotate things back i first tried to calculate the new up vector for the hinge but it was just to much trouble compared to this heres parts of the code i used for getting the hinges to work on mine self.seat=LoadModel("abstract::office_rollerchair_seat.gmf") Rot=self.model:GetRotation(1) self.model:SetRotation(Vec3(0,0,0)) self.joint = CreateJointHinge( self.model, self.seat,self.seat:GetPosition(1),Vec3(0,1,0)) self.model:SetRotation(Rot) self.seat:SetRotation( self.model:GetRotation(1),1 ) self.seat:SetPosition( self.model:GetPosition(1) ) Quote Link to comment Share on other sites More sharing options...
BrokenPillar Posted February 4, 2010 Author Share Posted February 4, 2010 Thanks Fredrick, I played around with that a little and got it to work with my model, but I had to change it a little. Here is the new part to my script: function object:UpdateJoint() self:ClearJoint() if self.hinge~=nil then self.door=LoadModel("abstract::x_stove_01_Door01.gmf") Rot=self.model:GetRotation(1) self.model:SetRotation(Vec3(0,0,0)) self.door:SetPosition( self.hinge:GetPosition(1) ) self.joint = CreateJointHinge( self.model, self.door,self.door:GetPosition(1),Vec3(1,0,0)) self.joint:SetLimits(0,60) self.model:SetRotation(Rot) self.door:SetRotation( self.model:GetRotation(1),1 ) self.door:SetPosition( self.hinge:GetPosition(1) ) end end The only major difference between this and yours was that I had to add the self.door:SetPosition( self.hinge:GetPosition(1) ) line twice to get the door to stay in the right position. I am not really sure why, but I have to have both instances in the exact sequence they are in, otherwise the door ends up in completely the wrong position relative to the stove. Anyway, this works for me now. If anyone sees anything obviously inefficient or has any other feedback on it please let me know, otherwise this will be the method I use going forward. Thanks again. Quote Vista | AMD Dual Core 2.49 GHz | 4GB RAM | nVidia GeForce 8800GTX 768MB Online Portfolio | www.brianmcnett.com Link to comment Share on other sites More sharing options...
Scott Richmond Posted February 5, 2010 Share Posted February 5, 2010 I haven't really read everything here and have not played around with this stuff just yet, but I thought this might help - As far as I know there are 2 types of movement in 3D space commonly used in engines - World relative and Object relative. My guess is that the hinge is being rotated in relation to the World, and not the object. So when the object rotation changes, the hinge is still rotating long the unchanged world...if that makes sense. Most, if not all, modelling programs have world and object relational rotation features so you should know what I'm trying to get at BrokenPillar. I don't have a direct solution here, but maybe that is the root cause of the problems. Quote Programmer, Modeller Intel Core i7 930 @ 3.5GHz | GeForce 480 GTX | 6GB DDR3 RAM | Windows 7 Premium x64 Visual Studio 2008 | Photoshop CS3 | Maya 2009 Website: http://srichnet.info Link to comment Share on other sites More sharing options...
Josh Posted February 5, 2010 Share Posted February 5, 2010 You can also transform the vector and position from the parent model space to global space to get the correct position and pin, without rotating the model. See TFormPoint() and TFormVector(). Quote My job is to make tools you love, with the features you want, and performance you can't live without. Link to comment Share on other sites More sharing options...
BrokenPillar Posted February 5, 2010 Author Share Posted February 5, 2010 DJDD, I kind of figured that was the problem, I just didn't know how to solve it. Fredrik was able to point me in a direction that I was able to get working. Josh, Thanks for the tip. I will look into those commands and see if I can figure them out. Thanks again for the help. I have spent some time trying to learn on my own by reading through several forum threads and this seems to be a great community. Quote Vista | AMD Dual Core 2.49 GHz | 4GB RAM | nVidia GeForce 8800GTX 768MB Online Portfolio | www.brianmcnett.com Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.