# help! Help with math

## Recommended Posts

Hello everybody. I need a help. I have a ray that casts from XY to X₁Y₁ with random angle.

I need to get a Z₁ coordinate from X₁Y₁.

How can I do it? Help me, please, I'll be very grateful for that!

Edited by JAY.ANN
1 hour ago, AngelAlpha said:

I don't need a ground position, the ray may end in the air

What we've got:

start XYZ, end X1Y1 found by random angle. We need to get Z1 from that angle, XY and X1Y1

Edited by JAY.ANN

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?

Edited by JAY.ANN
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

I will wait for a positive or negative answer after trying

I guess it won't work cuz there's no Z coordinate

can you show the code line

3 minutes ago, JAY.ANN said:

I guess it won't work cuz there's no Z coordinate

When I'll back from job, yes

3 minutes ago, JAY.ANN said:

When I'll back from job, yes

I'll be waiting for you tag me

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

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

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

"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

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

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

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

bro can you test it i tested it works

• 1

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

That's what I used tho. That resource has a weapon range limitations and a collision one

Just now, JAY.ANN said:

That's what I used tho. That resource has a weapon range limitations and a collision one

can you try the above code

can you try the above code

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

It still has no correct Z coordinate

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

• 1

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

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```
• 1
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

• 1

## Create an account

Register a new account