Jump to content

Help with math


JAY.ANN

Recommended Posts

5 minutes ago, JAY.ANN said:

It returns a face to face rotations, but I guess I can try this one

The problem is a Z coordinate, not the XY or an angle

I guess the solution is a similarity of triangles, but how can I implement that?

I will wait for a positive or negative answer after trying

Link to comment
ray = {}
addEventHandler( "onClientPlayerWeaponFire", getRootElement(), function( _, tAmmo, cAmmo, hX, hY, hZ, hElement, sX, sY, sZ )
local rx, ry, rz = getElementRotation( source )
local muzzleX, muzzleY, muzzleZ = getPedWeaponMuzzlePosition( source )
local muzzle_eX, muzzle_eY, muzzle_eZ = getPedTargetEnd( source )		
	if not getPedTargetCollision( source ) then
	local offset = math.rad( rz + 90 )
	local l_sX, l_sY, l_sZ = muzzleX, muzzleY, muzzleZ
	local l_eX, l_eY, l_eZ = ( muzzle_eX + 50 * math.cos( offset ) ), ( muzzle_eY + 50 * math.sin( offset ) ), muzzle_eZ
	table.insert( ray, { l_sX, l_sY, l_sZ, l_eX, l_eY, l_eZ } )
	else
	local l_sX, l_sY, l_sZ = muzzleX, muzzleY, muzzleZ
	local l_eX, l_eY, l_eZ = getPedTargetCollision( source )
	table.insert( ray, { l_sX, l_sY, l_sZ, l_eX, l_eY, l_eZ } )
	end
end)

function drawRays()
	for i, v in ipairs( ray ) do
	dxDrawLine3D( v[ 1 ], v[ 2 ], v[ 3 ], v[ 4 ], v[ 5 ], v[ 6 ], tocolor( 255, 0, 0, 100 ), 2 )
	end
end
addEventHandler( "onClientRender", root, drawRays )

That's the script. I want to draw a ray from weapon's muzzle XYZ to (XYZ + range) but if i use getPedTargetEnd() it has a weapon's range limitations and I can't change that correctly because of RenderWare engine and MTA limits

Edited by JAY.ANN
Link to comment
14 minutes ago, JAY.ANN said:
ray = {}
addEventHandler( "onClientPlayerWeaponFire", getRootElement(), function( _, tAmmo, cAmmo, hX, hY, hZ, hElement, sX, sY, sZ )
local rx, ry, rz = getElementRotation( source )
local muzzleX, muzzleY, muzzleZ = getPedWeaponMuzzlePosition( source )
local muzzle_eX, muzzle_eY, muzzle_eZ = getPedTargetEnd( source )		
	if not getPedTargetCollision( source ) then
	local offset = math.rad( rz + 90 )
	local l_sX, l_sY, l_sZ = muzzleX, muzzleY, muzzleZ
	local l_eX, l_eY, l_eZ = ( muzzle_eX + 50 * math.cos( offset ) ), ( muzzle_eY + 50 * math.sin( offset ) ), muzzle_eZ
	table.insert( ray, { l_sX, l_sY, l_sZ, l_eX, l_eY, l_eZ } )
	else
	local l_sX, l_sY, l_sZ = muzzleX, muzzleY, muzzleZ
	local l_eX, l_eY, l_eZ = getPedTargetCollision( source )
	table.insert( ray, { l_sX, l_sY, l_sZ, l_eX, l_eY, l_eZ } )
	end
end)

function drawRays()
	for i, v in ipairs( ray ) do
	dxDrawLine3D( v[ 1 ], v[ 2 ], v[ 3 ], v[ 4 ], v[ 5 ], v[ 6 ], tocolor( 255, 0, 0, 100 ), 2 )
	end
end
addEventHandler( "onClientRender", root, drawRays )

That's the script. I want to draw a ray from weapon's muzzle XYZ to (XYZ + range) but if i use getPedTargetEnd() it has a weapon's range limitations and I can't change that correctly because of RenderWare engine and MTA limits

"GetPedTargetCollision" can you try this function, otherwise the collision data will not be loaded

GTA also doesn't load an entire map collisions,just enough

Link to comment
2 minutes ago, Shady1 said:

"GetPedTargetCollision" can you try this function, otherwise the collision data will not be loaded

GTA also doesn't load an entire map collisions,just enough

It will return false if there's no collision or if it's not loaded

Link to comment
1 minute ago, Shady1 said:

you can't create an unlimited range beam except for collision data this will fail

I'm not going to create infinite ray from getPedTargetCollision() - that's the debug stuff in my script for now

I'm going to use offset to create my ray, but the trouble is in Z coordinate

Link to comment

https://community.multitheftauto.com/index.php?p=resources&s=details&id=652

I did a lot of research, but I didn't find much information, there was a study similar to this in the community, can you check it, maybe there is some code that will work for you

ray = {}
addEventHandler("onClientPlayerWeaponFire", getRootElement(), function( _, tAmmo, cAmmo, hX, hY, hZ, hElement, sX, sY, sZ)
   local muzzleX, muzzleY, muzzleZ = getPedWeaponMuzzlePosition(source)
   local targetX, targetY, targetZ = getPedTargetEnd(source)
   local collisionX, collisionY, collisionZ = getPedTargetCollision(source)
   if(collisionX) then
      table.insert(ray, {muzzleX, muzzleY, muzzleZ, collisionX, collisionY, collisionZ})
   else
      table.insert(ray, {muzzleX, muzzleY, muzzleZ, targetX, targetY, targetZ})
   end
end)

function drawRays()
    for i, v in ipairs( ray ) do
    dxDrawLine3D( v[ 1 ], v[ 2 ], v[ 3 ], v[ 4 ], v[ 5 ], v[ 6 ], tocolor( 255, 0, 0, 100 ), 2 )
    end
end
addEventHandler( "onClientRender", root, drawRays )

bro can you test it i tested it works

  • Like 1
Link to comment
Just now, JAY.ANN said:

I already tried all the suggestions. Nothing works fine to me

I tried the code I sent you and observed that there is no problem with the z coordinate and it works properly.

ray = {}
addEventHandler("onClientPlayerWeaponFire", getRootElement(), function( _, tAmmo, cAmmo, hX, hY, hZ, hElement, sX, sY, sZ)
   local muzzleX, muzzleY, muzzleZ = getPedWeaponMuzzlePosition(source)
   local targetX, targetY, targetZ = getPedTargetEnd(source)
   local collisionX, collisionY, collisionZ = getPedTargetCollision(source)
   if(collisionX) then
      table.insert(ray, {muzzleX, muzzleY, muzzleZ, collisionX, collisionY, collisionZ})
   else
      table.insert(ray, {muzzleX, muzzleY, muzzleZ, targetX, targetY, targetZ})
   end
end)

function drawRays()
    for i, v in ipairs( ray ) do
    dxDrawLine3D( v[ 1 ], v[ 2 ], v[ 3 ], v[ 4 ], v[ 5 ], v[ 6 ], tocolor( 255, 0, 0, 100 ), 2 )
    end
end
addEventHandler( "onClientRender", root, drawRays )

 

  • Like 1
Link to comment

The ray will end at the weapon range maximum

getPedTartgetEnd()

"Returns three floats, x,y,z, representing the position where the ped's target ends according to his range, or false if it was unsuccessful".

I need to continuous it or make it less

I've tried all MTA functions and events that may help to me but nothing works correct. I guess it's all about the math or matrix

I still think that the best solution is to use triangles similarity stuff

Edited by JAY.ANN
Link to comment

I'm not sure if I understood what your problem is because it doesn't make sense to somehow get some Z coordinate from two XY points and some angle which was used to get one of those XY points.

But I'll assume this is what you're trying to achieve: you have two points, origin and target, and you want to get a third point, which is in the same direction from origin as target is, but at arbitrary distance. So you could effectively push the target towards or away from origin. Is that right?

If so, then it's some basic vector math.

local newDistance = 50

-- set dX, dY, dZ to offset from origin to target
local dX, dY, dZ = targetX-originX, targetY-originY, targetZ-originZ

-- using Pythagorean theorem, calculate the length of dX, dY, dZ vector
local oldDistance = math.sqrt(dX*dX + dY*dY + dZ*dZ)

-- multiply each component dX, dY, dZ by newDistance and divide by oldDistance
-- so newX, newY, newZ becomes the offset from origin to your desired point
local newX, newY, newZ = dX*newDistance/oldDistance, dY*newDistance/oldDistance, dZ*newDistance/oldDistance

-- add origin to newX, newY, newZ
newX, newY, newZ = originX+newX, originY+newY, originZ+newZ

-- newX, newY, newZ now contains the coordinates we're looking for

newDistance/oldDistance expression repeats 3 times so we can calculate it once, store it in a variable and reuse for each component. Also, multiplication/division line can be combined with addition line. Then we get this:

local newDistance = 50

local dX, dY, dZ = targetX-originX, targetY-originY, targetZ-originZ
local oldDistance = math.sqrt(dX*dX + dY*dY + dZ*dZ)
local multiplier = newDistance/oldDistance
local newX, newY, newZ = originX+dX*multiplier, originY+dY*multiplier, originZ+dZ*multiplier
  • Thanks 1
Link to comment
39 minutes ago, Reyomin said:

I'm not sure if I understood what your problem is because it doesn't make sense to somehow get some Z coordinate from two XY points and some angle which was used to get one of those XY points.

But I'll assume this is what you're trying to achieve: you have two points, origin and target, and you want to get a third point, which is in the same direction from origin as target is, but at arbitrary distance. So you could effectively push the target towards or away from origin. Is that right?

If so, then it's some basic vector math.

local newDistance = 50

-- set dX, dY, dZ to offset from origin to target
local dX, dY, dZ = targetX-originX, targetY-originY, targetZ-originZ

-- using Pythagorean theorem, calculate the length of dX, dY, dZ vector
local oldDistance = math.sqrt(dX*dX + dY*dY + dZ*dZ)

-- multiply each component dX, dY, dZ by newDistance and divide by oldDistance
-- so newX, newY, newZ becomes the offset from origin to your desired point
local newX, newY, newZ = dX*newDistance/oldDistance, dY*newDistance/oldDistance, dZ*newDistance/oldDistance

-- add origin to newX, newY, newZ
newX, newY, newZ = originX+newX, originY+newY, originZ+newZ

-- newX, newY, newZ now contains the coordinates we're looking for

newDistance/oldDistance expression repeats 3 times so we can calculate it once, store it in a variable and reuse for each component. Also, multiplication/division line can be combined with addition line. Then we get this:

local newDistance = 50

local dX, dY, dZ = targetX-originX, targetY-originY, targetZ-originZ
local oldDistance = math.sqrt(dX*dX + dY*dY + dZ*dZ)
local multiplier = newDistance/oldDistance
local newX, newY, newZ = originX+dX*multiplier, originY+dY*multiplier, originZ+dZ*multiplier

That's a brilliant one! Exactly what I've been looking for! Thanks a lot

This topic can be closed

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