Jump to content

[HELP] Matrix rotation - position problem


Incama

Recommended Posts

Hello everyone!

 

So i'm trying to make a script that places a object in front of the player.

Problem is: 
I have everything client sided and i stream the object with a simple create function and elementDatas (streamin, streamout, elementdatachange),

but everytime that it asks for the player's (not localPlayer) position it doesn't take into count the rotation of the player (not localPlayer). For me it places it in the right places, but for other people it shows in way if i would have 0 rotation.

 

I'm using matrixs and transformPosition. Any idea?

 

(i've tryed asking for the localPlayer.matrix and source.matrix in the runcode resource and it indicates everything well)

Edited by Incama
Link to comment
local bobbers = {}
local lines = {}

function start()
	setElementData(localPlayer, "bobber:create", true)
end

function createBobber(player, m)
	if m then
		if not bobbers[player] then
			local m = player.matrix:transformPosition(Vector3(0,math.random(3,10),0))
			local z = getWaterLevel(m[1], m[2], m[3]) or -0.55
			bobbers[player] = createObject(1974,m[1], m[2], z)
			setObjectScale(bobbers[player], 2)
			lines[player] = true
		end
	else
		if isElement(bobbers[player]) then
			destroyElement(bobbers[player])
			bobbers[player] = false
			lines[player] = false
		end
	end
end

addEventHandler("onClientResourceStop", resourceRoot, function()
	if getElementData(localPlayer, "bobber:create") then
		setElementData(localPlayer, "bobber:create", false)
	end
end)

addEventHandler("onClientElementDataChange", root, function(data, oldv) 
	if data == "bobber:create" then
		if isElement(source) and getElementType(source) == "player" and isElementStreamedIn(source) then
			local m = getElementData(source, "bobber:create") or false
			createBobber(source, m)
		end
	end
end)

addEventHandler("onClientElementStreamIn", root, function()
	if isElement(source) and getElementType(source) == "player" and getElementData(source, "bobber:create") then
		if source ~= localPlayer then
			local m = getElementData(source, "bobber:create") or false
			if m then
				createBobber(source, true)
			end
		end
	end
end)

addEventHandler("onClientElementStreamOut", root, function()
	if isElement(source) and getElementType(source) == "player" and getElementData(source, "bobber:create") then
		if source ~= localPlayer then
			local m = getElementData(source, "bobber:create") or false
			if m then
				createBobber(source, false)
			end
		end
	end
end)

function drawLine()
	for i,v in pairs(lines) do
		if v then
			if isElementStreamedIn(i) then
				local p = bobbers[i].position
				local m = fishingSticks[i].matrix:transformPosition(Vector3(0.124, 0.034, 1.29))
				dxDrawLine3D(p.x, p.y, p.z, m.x, m.y, m.z, tocolor(0,0,255,200), 0.5)
			end
		end
	end
end
addEventHandler("onClientRender", root, drawLine)

Thanks for the quick answer but the problem lies here. As i said, it works perfectly fine with localPlayer but when i try it with other players on the client side, it just simply doesn't work.

I don't know if i'm an idiot or it's a bug.

 

EDIT: This is a piece of the script, i deleted some parts because it's private

Edited by Incama
Link to comment

I've figured out in the end. Here's the solution:

Since you can't pass matrixs with setElementData (just trust me with that) you have to split the matrix into a table and then do the setElementData part.

So the scripts now looks like this:
 

setElementData(localPlayer, "bobber:create", true)
setElementData(localPlayer, "bobber:pos", {localPlayer.matrix.position.x, localPlayer.matrix.position.y, localPlayer.matrix.position.z, localPlayer.matrix.rotation.x, localPlayer.matrix.rotation.y, localPlayer.matrix.rotation.z})

 

and then at the onClientElementDataChange side of things you have to do this:

local mat = getElementData(player, "bobber:pos")
local matrix = Matrix(Vector3(mat[1], mat[2], mat[3]), Vector3(mat[4], mat[5], mat[6]))

That's all, and some other minor fixes and tweaks solved my problem.

 

You can lock the post know and thanks for the advices.

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