Jump to content

Camera matrix to velocity direction


Gallardo9944

Recommended Posts

Posted

Hello.

I'm looking for a way to make an element "boost" into the direction of the camera. Obviously, it has to be done with matrix stuff. I'm not fond of matrix-related stuff, so yeah, I'd like to set element's velocity to +1 into the camera direction point. Any tips or examples?

Thanks in advance.

Posted

You need to use math to get the forward direction of the perspective of the camera, and then set velocity of player that direction using math again in a similar way. Search setting velocity forward, or spawning something infront of a player.

Posted

well, getting a vector is not a huge deal, but getting the damn velocity... Here's what I have for now:

local vehicle = getPedOccupiedVehicle(localPlayer) 
local mx,my,mz,mpx,mpy,mpz = getCameraMatrix() 
local vector = {mx-mpx,my-mpy,mz-mpz} 

And I'm stuck at this point.

Posted

Okay i found it out myself. Posting the code if someone needs:

local vehicle = getPedOccupiedVehicle(localPlayer) 
local mx,my,mz,mpx,mpy,mpz = getCameraMatrix() 
local vector = {mx-mpx,my-mpy,mz-mpz} 
setElementVelocity(vehicle,-vector[1],-vector[2],-vector[3]) 

It doesn't calculate the difference between velocities, but in fact it does set the direction.

Posted
It doesn't calculate the difference between velocities, but in fact it does set the direction.

If you still need to make it only set velocity to 1 in the direction, then divide the components of the vector with the length (normalization)

local vehicle = getPedOccupiedVehicle(localPlayer) 
local mx, my, mz, mpx, mpy, mpz = getCameraMatrix() 
local dist = getDistanceBetweenPoints3D(mx, my, mz, mpx, mpy, mpz) 
local vector =  
{ 
    (mx - mpx) / dist, 
    (my - mpy) / dist, 
    (mz - mpz) / dist 
} 
setElementVelocity(vehicle, -vector[1], -vector[2], -vector[3]) 

It may give better result if you use the vehicle position and camera position instead of camera position and camera target position.

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