Jump to content

Unstable camera.


AggrorJorn
 Share

Recommended Posts

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

Link to comment
Share on other sites

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..

Link to comment
Share on other sites

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/

Link to comment
Share on other sites

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.

  • Upvote 1

?? FRANCESCO CROCETTI ??

http://skaredcreations.com

Link to comment
Share on other sites

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

  • Upvote 1
Link to comment
Share on other sites

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

?? FRANCESCO CROCETTI ??

http://skaredcreations.com

Link to comment
Share on other sites

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...