Jump to content

Face toward at dxDrawMaterialLine3D depending on rotations


Dzsozi (h03)

Recommended Posts

Hello!

I really need some math experts' help, I can't figure out what should I do. I am using the dxDrawMaterialLine3D function to draw light shadows on the ground, and these are attached to a vehicle. My only problem is that I don't know how should I make the light always face up, even when the vehicle is rotated and keep the rotation, so I won't experience problems like this:

PwcHZ6P.png

So as you can see, when I'm not on an even surface, the "light" goes through the ground, that's why I would like to make the image face upwards depending on the vehicle's rotation, or at least adjust it and not make it face always up. I hope you understand what I am trying to do.

Here's what it should look like (it is on an even surface):

cOvnCgW.png

So, I don't really know how should I calculate these numbers to make it work with the faceTowardX, faceTowardY and faceTowardZ parameters. Could somebody help me out with the calculations?

local x, y, z = getElementPosition(matching.element)
local rx, ry, rz = getElementRotation(matching.element)
local faceX, faceY, faceZ = --?, ?, ?

dxDrawMaterialLine3D(worldX, worldY-1, fixedGroundPosition, worldX, worldY+1, fixedGroundPosition, data.renderTarget, 2, tocolor(lightColor[1],lightColor[2],lightColor[3],lightColor[4]), faceX, faceY, faceZ)

Thank you for your reply in advance!

Link to comment
function elementOffset(matrix, x, y, z)
	return x * matrix[1][1] + y * matrix[2][1] + z * matrix[3][1] + matrix[4][1], x * matrix[1][2] + y * matrix[2][2] + z * matrix[3][2] + matrix[4][2], x * matrix[1][3] + y * matrix[2][3] + z * matrix[3][3] + matrix[4][3]
end

 

Use the getElementMatrix and the elementOffset function to get the material start/end and facing toward position.

Like this:

local vehicleMatrix = getElementMatrix(vehicleElement)
local x1, y1, z1 = elementOffset(vehicleMatrix, 0, 1, 0)
local x2, y2, z2 = elementOffset(vehicleMatrix, 0, -1, 0)
local x3, y3, z3 = elementOffset(vehicleMatrix, 0, 1, 0.5)
				
dxDrawMaterialLine3D(x1, y1, z1, x2, y2, z2, data.renderTarget, 2, tocolor(lightColor[1],lightColor[2],lightColor[3],lightColor[4]), x3, y3, z3)

 

Or another way (not tested):

local vehiclePosX, vehiclePosY, vehiclePosZ = getElementPosition(vehicleElement)
local _, _, vehicleRotation = getElementRotation(vehicleElement)
vehicleRotation = math.rad(vehicleRotation)

local rotatedX = math.cos(vehicleRotation) * 1.05
local rotatedY = math.sin(vehicleRotation) * 1.05

local hit, hitPosX, hitPosY, hitPosZ = processLineOfSight(vehiclePosX, vehiclePosY, vehiclePosZ + 1, vehiclePosX, vehiclePosY, vehiclePosZ - 1, true, false, false, false, false)
hitPosZ = hitPosZ + 0.02

dxDrawMaterialLine3D(hitPosX + rotatedX, hitPosY + rotatedY, hitPosZ, hitPosX - rotatedX, hitPosY - rotatedY, hitPosZ, data.renderTarget, 2, tocolor(lightColor[1],lightColor[2],lightColor[3],lightColor[4]), hitPosX, hitPosY, hitPosZ + 1)

 

Play with the values :)

Link to comment

I have been trying to change the values, but these doesn't seem to work, well I can't make them functioning the way I would like to.

But I have one more idea, I don't know if it is possibble, but is there any way to get the surface rotation or anything like this? I'm thinking about the default headlight shadow's functionality, is there any way to recreate the way it is working?

Link to comment
  • 2 weeks later...
  • 2 weeks later...
  • Moderators
getElementMatrix(getCamera())

function getElementMatrix(element)
    local rx, ry, rz = getElementRotation(element, "ZXY")
    rx, ry, rz = math.rad(rx), math.rad(ry), math.rad(rz)
    local matrix = {}
    matrix[1] = {}
    matrix[1][1] = math.cos(rz)*math.cos(ry) - math.sin(rz)*math.sin(rx)*math.sin(ry)
    matrix[1][2] = math.cos(ry)*math.sin(rz) + math.cos(rz)*math.sin(rx)*math.sin(ry)
    matrix[1][3] = -math.cos(rx)*math.sin(ry)
    matrix[1][4] = 1
    
    matrix[2] = {}
    matrix[2][1] = -math.cos(rx)*math.sin(rz)
    matrix[2][2] = math.cos(rz)*math.cos(rx)
    matrix[2][3] = math.sin(rx)
    matrix[2][4] = 1
    
    matrix[3] = {}
    matrix[3][1] = math.cos(rz)*math.sin(ry) + math.cos(ry)*math.sin(rz)*math.sin(rx)
    matrix[3][2] = math.sin(rz)*math.sin(ry) - math.cos(rz)*math.cos(ry)*math.sin(rx)
    matrix[3][3] = math.cos(rx)*math.cos(ry)
    matrix[3][4] = 1
    
    matrix[4] = {}
    matrix[4][1], matrix[4][2], matrix[4][3] = getElementPosition(element) --image position.
    matrix[4][4] = 1
    
    return matrix
end

 

https://wiki.multitheftauto.com/wiki/GetElementMatrix

》getPositionFromElementOffset《

 

And full control depending on the camera.

 

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