Jump to content

Camera matrix to velocity direction


Gallardo9944

Recommended Posts

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

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