JAY.ANN Posted December 2, 2022 Share Posted December 2, 2022 (edited) 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 December 2, 2022 by JAY.ANN Link to comment
JAY.ANN Posted December 3, 2022 Author Share Posted December 3, 2022 (edited) 1 hour ago, AngelAlpha said: getGroundPosition 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 December 3, 2022 by JAY.ANN Link to comment
Shady1 Posted December 3, 2022 Share Posted December 3, 2022 try this function https://wiki.multitheftauto.com/wiki/FindRotation 1 Link to comment
JAY.ANN Posted December 3, 2022 Author Share Posted December 3, 2022 (edited) 6 minutes ago, Shady1 said: try this function https://wiki.multitheftauto.com/wiki/FindRotation 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 December 3, 2022 by JAY.ANN Link to comment
Shady1 Posted December 3, 2022 Share Posted December 3, 2022 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
JAY.ANN Posted December 3, 2022 Author Share Posted December 3, 2022 Just now, Shady1 said: I will wait for a positive or negative answer after trying I guess it won't work cuz there's no Z coordinate Link to comment
Shady1 Posted December 3, 2022 Share Posted December 3, 2022 can you show the code line 3 minutes ago, JAY.ANN said: I guess it won't work cuz there's no Z coordinate Link to comment
JAY.ANN Posted December 3, 2022 Author Share Posted December 3, 2022 When I'll back from job, yes Link to comment
Shady1 Posted December 3, 2022 Share Posted December 3, 2022 3 minutes ago, JAY.ANN said: When I'll back from job, yes I'll be waiting for you tag me Link to comment
JAY.ANN Posted December 3, 2022 Author Share Posted December 3, 2022 (edited) 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 December 3, 2022 by JAY.ANN Link to comment
Shady1 Posted December 3, 2022 Share Posted December 3, 2022 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
JAY.ANN Posted December 3, 2022 Author Share Posted December 3, 2022 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
Shady1 Posted December 3, 2022 Share Posted December 3, 2022 you can't create an unlimited range beam except for collision data this will fail Link to comment
JAY.ANN Posted December 3, 2022 Author Share Posted December 3, 2022 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
Shady1 Posted December 3, 2022 Share Posted December 3, 2022 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 1 Link to comment
JAY.ANN Posted December 3, 2022 Author Share Posted December 3, 2022 6 minutes ago, Shady1 said: 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 Link to comment
Shady1 Posted December 3, 2022 Share Posted December 3, 2022 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 Link to comment
JAY.ANN Posted December 3, 2022 Author Share Posted December 3, 2022 1 minute ago, Shady1 said: can you try the above code I already tried all the suggestions. Nothing works fine to me It still has no correct Z coordinate Link to comment
Shady1 Posted December 3, 2022 Share Posted December 3, 2022 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 ) 1 Link to comment
JAY.ANN Posted December 3, 2022 Author Share Posted December 3, 2022 (edited) 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 December 3, 2022 by JAY.ANN Link to comment
DiSaMe Posted December 4, 2022 Share Posted December 4, 2022 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 Link to comment
JAY.ANN Posted December 4, 2022 Author Share Posted December 4, 2022 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
DiSaMe Posted December 4, 2022 Share Posted December 4, 2022 I'm glad that was helpful 1 Link to comment
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now