AggrorJorn Posted May 2, 2010 Share Posted May 2, 2010 I tried converting my C++ third person camera to Lua, but I am having some dificulties with an unstable camera. The camera looks fine but ones I start to move around I notice that there are bumps and stuttering moments. The code below shows a ball rolling of a slope. use the mouse to for third person camera movement. You notice that the level starts to shivver or something like. Sometimes you will get an exception error as wel.. If someone has a good suggestion to what might be wrong here, let me know! require("Scripts/constants/collision_const") require("Scripts/constants/engine_const") require("Scripts/LinkedList") require("Scripts/filesystem") require("Scripts/math/math") --Register abstract path RegisterAbstractPath("") --Set graphics mode Graphics(1024,768) --Create framewerk object and set it to a global object so other scripts can access it fw=CreateFramework() SetGlobalObject("framewerk",fw) --Variables mx=0.0 my=0.0 ballMass= 60 --camera camera=fw.main.camera camera:SetPosition(Vec3(0,2,-6)) camera:SetRotation(Vec3(30,0,0)) --light light=CreateDirectionalLight() light:SetRotationf(45,45,45) --material material=LoadMaterial("abstract::cobblestones.mat") --ground groundBody=CreateBodyBox(20,0.1,4000,ground) groundBody:SetPosition(Vec3(0,-4,1)) groundBody:SetRotation(Vec3(25,0,0)) ground=CreateCube() ground:SetParent(groundBody) ground:SetScale(Vec3(20,0.1,4000)) ground:Paint(material) --ground --ball ballBody=CreateBodySphere(1) ballBody:SetPosition(Vec3(0,8,1)) ballBody:SetMass(ballMass) ball=CreateSphere(28) ball:SetParent(ballBody) ball:SetScale(Vec3(2,2,2)) --ball --collisions DebugPhysics(0) Collisions(1,1,1) ballBody: SetCollisionType (1) groundBody: SetCollisionType (1) HideMouse() MoveMouse(GraphicsWidth()/2,GraphicsHeight()/2) FlushKeys() FlushMouse() -- third person camera pivot ballPivot = CreatePivot(ballBody) camera:SetParent(ballPivot) camrotation=Vec3(0,0,0) while KeyDown(KEY_ESCAPE)==0 do mx=Curve(MouseX()-GraphicsWidth()/2,mx,6) my=Curve(MouseY()-GraphicsHeight()/2,my,6) MoveMouse(GraphicsWidth()/2,GraphicsHeight()/2) camrotation.y = camrotation.y-mx/10.0 camrotation.x = camrotation.x+my/10.0 ballPivot:SetRotation(camrotation, 1) fw:Update() fw:Render() Flip(0) end Quote Link to comment Share on other sites More sharing options...
paramecij Posted May 3, 2010 Share Posted May 3, 2010 maybe because the pivot is parented to the body, you rotate the cam by rotating the pivot, but the pivot is also rotated by the body? dunno where those exceptions errors are coming from though, maybe some division by 0 lua mixing floats with ints? Have no idea really.. Quote Link to comment Share on other sites More sharing options...
ZioRed Posted May 3, 2010 Share Posted May 3, 2010 Why do you parent the pivot to the body and then rotate the pivot? I think you should parent the body to the pivot and then the SetRotation of ballPivot makes sense... Or else, if you still parent pivot to body, try to rotate the body and not the pivot. Quote ?? FRANCESCO CROCETTI ?? http://skaredcreations.com Link to comment Share on other sites More sharing options...
AggrorJorn Posted May 3, 2010 Author Share Posted May 3, 2010 Hi guys, thanks for answer. ah I just realise that the CreatePivot sets the body as parent, instead of placing it at the body. I tried setting the body as parent to the pivot, but hen the camera controls move the ball. ballPivot = CreatePivot() ballBody:SetParent(ballPivot) camera:SetParent(ballPivot) Setting the body for rotation makes this even worse, since the camera rotates around the rolling body. I converted this code diirectly from working C++ code: http://leadwerks.com/werkspace/index.php?/files/file/121-third-person-camera-ball-game-example/ Quote Link to comment Share on other sites More sharing options...
ZioRed Posted May 3, 2010 Share Posted May 3, 2010 ah I just realise that the CreatePivot sets the body as parent, instead of placing it at the body. It is that... as told you, CreatePivot(parent) create a pivot with "parent" as its parent entity (at least that is on wiki). Probably I misunderstood what you needed. Unfortunately I am not at home now and cannot try your code, I will try it in the next 4 hours. 1 Quote ?? FRANCESCO CROCETTI ?? http://skaredcreations.com Link to comment Share on other sites More sharing options...
paramecij Posted May 3, 2010 Share Posted May 3, 2010 just use ballPivot=CreatePivot() and then add a PositionEntity(ballPivot, EntityPosition(ballBody,1),1) inside your main loop .. i think it might translate to something like ballPivot:SetPosition(ballBody:getPosition()) in your case, also make sure to use world space this way the pivot will follow the ball but have independent rotation 1 Quote Link to comment Share on other sites More sharing options...
AggrorJorn Posted May 3, 2010 Author Share Posted May 3, 2010 thanks paramecij, I tried positioning before but I used it the wrong way: ballPivot:SetPosition(ballBody) it was like you said: ballPivot:SetPosition(ballBody:getPosition(1)) thanks ZioRed and paramecij +1 Quote Link to comment Share on other sites More sharing options...
ZioRed Posted May 3, 2010 Share Posted May 3, 2010 I tried your 1st code and putting the SetRotation call after fw:Update the camera seems to work correctly: .. ballPivot = CreatePivot(ballBody) camera:SetParent(ballPivot) camrotation=Vec3(0,0,0) .. while KeyDown(KEY_ESCAPE)==0 do fw:Update() mx=Curve(MouseX()-GraphicsWidth()/2,mx,6) my=Curve(MouseY()-GraphicsHeight()/2,my,6) MoveMouse(GraphicsWidth()/2,GraphicsHeight()/2) camrotation.y = camrotation.y-mx/10.0 camrotation.x = camrotation.x+my/10.0 ballPivot:SetRotation(camrotation, 1) fw:Render() Flip(0) end Quote ?? FRANCESCO CROCETTI ?? http://skaredcreations.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.