Jump to content

Trigonometry - Object Z Rotation.


Cyanide

Recommended Posts

Hey, I'm trying to allow someone to shoot a object and it would rotate to them so the facing side would look away from the wall. Example: A fish shooting script, and the fish point towards you!

Everything seems to work but depending on angle the object would lean to much on one side and cause the object to not exactly point at the player. Here's the script below, I commented all my attempts to figure this out. Anyone notice anything wrong?

  
local a = getPedRotation( source ) 
         
--x = x + distance * math.sin( -ax ) * 180 / math.pi 
--y = y + distance * math.sin( -ay ) * 180 / math.pi 
--z = z + distance * math.sin( -az ) * 180 / math.pi 
         
--local rx = x + math.sin (math.rad(-a)) * distance  
--local ry = y + math.cos (math.rad(-a)) * distance  
--local r = a + 90.0 
         
         
createObject ( object, hitX, hitY, hitZ, 0, 0, a + 90 ) 

Link to comment
local x,y,z = getPedRotation( source ) 
createObject ( object, hitX, hitY, hitZ, 0, 0, z + 90 ) 

or use a matrix

getPedRotation returns one variable, and that is the rotation of the player.

I think I understand what you mean, but I don't get what the comments in your code mean.

Try a + 180 in your createObject

That definitively wouldn't work. The code in the comments is called Trigonometry.

Link to comment
The code in the comments is called Trigonometry.

I meant, what do the variables mean ...

Anyway, try this

local ex, ey, ez = hitX, hitY, hitZ            -- 3D point that needs to be rotated 
local px, py, pz = getElementPosition(source)  -- 3D point that needs to be rotated to 
  
local rotx = math.atan2 (pz - ez, getDistanceBetweenPoints2D ( px, py, ex, ey ) ) 
local roty = 0 
local rotz = math.pi - math.atan2 ( ( ex - px ), ( ey - py ) )  
  
rotx, roty, rotz = math.deg (rotx), math.deg(roty), math.deg(rotz) 
return setElementRotation(element, rotx, roty, rotz) 
  
createObject ( object, hitX, hitY, hitZ, rotx, roty, rotz ) 

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