Jump to content

[QUESTION] Move free camera while rotation is not a particular case


Platin

Recommended Posts

I'm working on a type of RTS camera style and I encounter a problem. The camera can rotate on the same axis without problem, it can zoom and thats about it, but it needed to be looking at an element.

Now I'm working on a freecam mode where by getting to the edges of the screen you can move to north, east, west or south. It is easy when the camera rotation is 0, because you know that the top of the screen will be allways X cardinal position, it looked something like this:
 

local camSpeed = 0.2
local sx, sy = getCursorPosition()
local x, y, z = getElementPosition(camElement)
if sy == 0 then -- top
  setElementPosition(camElement, x, y-camSpeed, z)
end
if sy == 1 then -- down
  setElementPosition(camElement, x, y+camSpeed, z)
end
if sx == 0 then -- left
  setElementPosition(camElement, x+camSpeed, y, z)
end
if sx == 1 then -- right
  setElementPosition(camElement, x-camSpeed, y, z)
end

As I said, this works fine while my rotation is 0, but when I move to something like 180º, my now top of the screen will not go to the south but rather north, so it would move like my mouse was on the bottom of the screen.

The question here is if there is a algorithm that i can use to determinate how much of X and how much of Y i need to add to the position of my camElement in order to go "up" or "down"...

If you didn't get what I want yet, think of The Sims 3 for example, when you put your mouse on the top of your screen you go up, whatever your rotation is, it doesn't matter, you will always go up. With the code I have I can only go UP if im facing South, on the other hand if im facing North then when my mouse is on the TOP i will go DOWN instead of UP.

Hope you can help me with this one, and thanks!

Edited by Platin - xNikoXD
Link to comment
  • Moderators

Everything you need is on this page: https://wiki.multitheftauto.com/wiki/GetElementMatrix

 

Use getElementMatrix on to the camera element to get it's matrix. And use the functions in the examples to get the offset from the orientation(matrix) of the camera. Which you can later move to with your camera.

It gives you a lot of freedom to do really crazy stuff.

 

Edited by IIYAMA
Link to comment

Just try it, with the getPositionFromElementOffset() function given by the wiki on getElementMatrix

I made the camera element to rotate at the same time the camera rotates, then get his front, back, right and left. Here's the code if someone wants it:

-- camaraElemento is my cameraElement, is a ped in my case
function getPositionFromElementOffset(element,offX,offY,offZ) -- As I said, this is from the Wiki: https://wiki.multitheftauto.com/wiki/GetElementMatrix
    local m = getElementMatrix(element)
    local x = offX * m[1][1] + offY * m[2][1] + offZ * m[3][1] + m[4][1]
    local y = offX * m[1][2] + offY * m[2][2] + offZ * m[3][2] + m[4][2]
    local z = offX * m[1][3] + offY * m[2][3] + offZ * m[3][3] + m[4][3]
    return x, y, z
end

local rotX = math.deg(rot) -- My rotation is on rad, so i need to convert it to degress | The X isn't from X axis, is because i already had rot defined
local velCam = 0.02 -- Vel of the camera moving by frame
local sx, sy = getCursorPosition()
local rx, ry, rz = getElementRotation(camaraElemento)
if rz ~= rotX then setElementRotation(camaraElemento, 0, 0, rotX) end

-- My default rot 0 == looking to the south
if sy == 0 then -- top
  x, y, z = getPositionFromElementOffset(camaraElemento, 0, -velCam, 0)
end
if sy == 1 then -- down
  x, y, z = getPositionFromElementOffset(camaraElemento, 0, velCam, 0)
end
if sx == 0 then -- left
  x, y, z = getPositionFromElementOffset(camaraElemento, velCam, 0, 0)
end
if sx == 1 then -- right
  x, y, z = getPositionFromElementOffset(camaraElemento, -velCam, 0, 0)
end
setElementPosition(camaraElemento, x, y, z)

It would be nice if some mod could close this topic, my problem is resolved.

Edited by Platin - xNikoXD
Fixed code
  • Like 1
Link to comment
  • Moderators

You want to close it, but:

  • There is one thing that you might consider handy, if you want every player to have to same camera speed.

 

local velCam = 0.02 * (timeSlice / 17)

 

Parameters

 
  1. float timeSlice
  • timeSlice: The interval between this frame and the previous one in milliseconds (delta time).

https://wiki.multitheftauto.com/wiki/OnClientPreRender

 

 

Good luck with developing/finishing it!

 

  • Like 1
Link to comment
  • 2 weeks later...

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