Jump to content

[help]A rotating camera


Xierra

Recommended Posts

Hi guys!

I want to ask about this because I don't know how to move by moving and rotating.

Something like a car showroom, with camera rotating around the car.

I've read the wiki about camera functions. But I don't know how to do a rotating camera on an element (vehicle) like in some servers.

I don't know about those 1x like that, My maths aren't as good as that. :|

Can you give me an example of it, so I can make something like that?

Link to comment

try this, but its untested:

angle=0
 
function getPointFromDistanceRotation(x, y, dist, angle) --credits to robhol from wiki snippets
local a = math.rad(90 - angle)
local dx = math.cos(a) * dist;
local dy = math.sin(a) * dist;
return x+dx, y+dy;
end
 
function attachrotatingcamera(bool,element,Zoffset,distance)
if bool then
element,Zoffset,distance=element,Zoffset,distance --make values global
addEventHandler("onClientRender",getRootElement(),createRotRamera)
else
removeEventHandler("onClientRender",getRootElement(),createRotRamera)
end
end
 
function createRotRamera()
local x,y,z=getElementPosition(element)
local camx,camy=getPointFromDistanceRotation(x, y, distance, angle)
setCameraMatrix(camx,camy,z+Zoffset,x,y,z)
  angle=angle+1 --you can change value
if angle>359 then angle=0 end
end

Link to comment

It seems like you're supposed to type something like:

attachrotatingcamera(true,element,10,20)

However, it won't work because this line won't do anything:

element,Zoffset,distance=element,Zoffset,distance --(doesn't)make values global

This should fix it, although I haven't checked for any other mistake in the code.

-- EDIT ME
 
local rotSpeed = 1 -- How much degrees you want the camera to turn per frame
 
-- DON'T EDIT ME ANYMOAR
 
local angle = 0
local elem
local zOff
local dist
 
function getPointFromDistanceRotation(x, y, dist, angle) --credits to robhol from wiki snippets
local a = math.rad(90 - angle)
local dx = math.cos(a) * dist;
local dy = math.sin(a) * dist;
return x+dx, y+dy;
end
 
function attachrotatingcamera(bool,element,Zoffset,distance)
if bool then
     elem,zOff,dist=element,Zoffset,distance --make values global
addEventHandler("onClientRender",getRootElement(),createRotRamera)
else
removeEventHandler("onClientRender",getRootElement(),createRotRamera)
end
end
 
function createRotRamera()
local x,y,z=getElementPosition(elem)
local camx,camy=getPointFromDistanceRotation(x, y, dist, angle)
setCameraMatrix(camx,camy,z+zOff,x,y,z)
  angle=(angle+rotSpeed)%360
end

Link to comment

too bad signatures are disabled, i would put there disclaimer that my scripts wont work likely xD

just simple command handler added(/rotateme):

-- EDIT ME
 
local rotSpeed = 1 -- How much degrees you want the camera to turn per frame
 
-- DON'T EDIT ME ANYMOAR
local angle = 0
local elem
local zOff
local dist
local active = false
 
function getPointFromDistanceRotation(x, y, dist, angle) --credits to robhol from wiki snippets
local a = math.rad(90 - angle)
local dx = math.cos(a) * dist;
local dy = math.sin(a) * dist;
return x+dx, y+dy;
end
 
function attachrotatingcamera(bool,element,Zoffset,distance)
if bool then
     active=true
     elem,zOff,dist=element,Zoffset,distance --make values global
addEventHandler("onClientRender",getRootElement(),createRotRamera)
else
removeEventHandler("onClientRender",getRootElement(),createRotRamera)
setCameraTarget(getLocalPlayer(),getLocalPlayer())
     active=false
end
end
 
function createRotRamera()
local x,y,z=getElementPosition(elem)
local camx,camy=getPointFromDistanceRotation(x, y, dist, angle)
setCameraMatrix(camx,camy,z+zOff,x,y,z)
  angle=(angle+rotSpeed)%360
end
 
addCommandHandler("rotateme",function()
  attachrotatingcamera(not active,getLocalPlayer(),5,10)
end)

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...