pa3ck Posted October 19, 2014 Share Posted October 19, 2014 Hey. I just started to develop a builder resource for DayZ but the thing is, I'm not the best at maths. What I want to do is, move an object using keys. The problem is, I don't know how to calculate the X and Y positions when the player is not at a 0 degrees angle on the map. Any help or advice would be appreciated, thanks! Link to comment
pa3ck Posted October 19, 2014 Author Share Posted October 19, 2014 I don't think my code is needed. I'm sure people understand my question and can help me without me attaching any code to it. Link to comment
Drakath Posted October 19, 2014 Share Posted October 19, 2014 By whom coordinates are you trying to calculate? Link to comment
Saml1er Posted October 19, 2014 Share Posted October 19, 2014 Why don't you check map editor resource? I'm sure you'll see how they did it. Link to comment
pa3ck Posted October 19, 2014 Author Share Posted October 19, 2014 Map editor is a different concept. You're building while you're in freecam mode. Link to comment
pa3ck Posted November 5, 2014 Author Share Posted November 5, 2014 Anybody? Any comments, thoughts? Link to comment
MTA Team botder Posted November 6, 2014 MTA Team Share Posted November 6, 2014 Get the player's position. Get the object's position. Calculate the angle between those positions (FindRotation) and use that angle with combination with math.cos and math.sin to calculate a position from the object's position at some distance. (x0 + math.cos(angle [in rad]) * distance) Note: That will only move the object relative to the player (not camera) and it won't move the object up or down. Link to comment
pa3ck Posted November 6, 2014 Author Share Posted November 6, 2014 Thank you for your answer! However, I`m getting an error while using that piece of code. Debug console: unexpected symbol near `in` Link to comment
Dealman Posted November 6, 2014 Share Posted November 6, 2014 Could you show us the line where the error is occuring? findRotation doesn't use for loops as far as I can tell. Link to comment
pa3ck Posted November 6, 2014 Author Share Posted November 6, 2014 It`s the one Necktrox said: x0 + math.cos(angle [in rad]) * distance Link to comment
MTA Team botder Posted November 6, 2014 MTA Team Share Posted November 6, 2014 That is not even lua code. You have to use math.rad to get the degree in radian Link to comment
Dealman Posted November 6, 2014 Share Posted November 6, 2014 Since I'll be doing something similar in the future, I tried to throw something together a bit fast - I'm not sure if this is what you're asking for, but if it is, feel free to edit it to your hearts desire! Client: -- Variables local isBuildModeActive = false; local selectedObject = nil; -- Functions function startBuildMode_Handler() if(isPedInVehicle(localPlayer) == false) then if(isBuildModeActive == false) then isBuildModeActive = true; outputChatBox("#696969[server]: #00BB00Building Mode has been activated!", 0, 0, 0, true); elseif(isBuildModeActive == true) then isBuildModeActive = false; outputChatBox("#696969[server]: #BB0000Building Mode has been de-activated!", 0, 0, 0, true); end else outputChatBox("#696969[server]: #BB0000You can not enter Build Mode whilst in a vehicle!", 0, 0, 0, true); end end addCommandHandler("Build", startBuildMode_Handler, false); function selectObjectCommand_Handler(theCMD, arg1) local stringPattern = "[%a%p%s%c]"; if(isBuildModeActive == true) then if(string.match(arg1, stringPattern)) then outputChatBox("#696969[server]: #BB0000Invalid input detected! #FF2626("..tostring(arg1)..")", 0, 0, 0, true); outputChatBox("#696969[server]: #BB0000Syntax: /Select [ObjectID] (/Select 2898)", 0, 0, 0, true); else local objectID = tonumber(arg1); outputChatBox("#696969[server]: #00BB00Selected Object ID #26FF26("..(tostring(arg1))..")", 0, 0, 0, true); local pX, pY, pZ = getElementPosition(localPlayer); local pRX, pRY, pRZ = getElementRotation(localPlayer); local oX, oY = (pX+4*math.cos(math.rad(pRZ+90))), (pY+4*math.sin(math.rad(pRZ+90))); local theObject = createObject(objectID, oX, oY, pZ); selectedObject = theObject; addEventHandler("onClientPreRender", root, updateObjectLocation_Handler); end else outputChatBox("#696969[server]: #BB0000Building Mode is not activated!", 0, 0, 0, true); end end addCommandHandler("Select", selectObjectCommand_Handler, false); function updateObjectLocation_Handler() if(selectedObject ~= nil) then local pX, pY, pZ = getElementPosition(localPlayer); local pRX, pRY, pRZ = getElementRotation(localPlayer); local oX, oY = (pX+4*math.cos(math.rad(pRZ+90))), (pY+4*math.sin(math.rad(pRZ+90))); setElementPosition(selectedObject, oX, oY, pZ); end end Usage: /Build - Enters Building Mode /Select [iD] - Creates the specified object. As I said, I threw this together in less than half an hour. So it's not perfect Link to comment
MTA Team botder Posted November 6, 2014 MTA Team Share Posted November 6, 2014 You could remove the calculation from the "Select" command handler or call the function updateObjectLocation_Handler(). You could also add some other handy functionality for the height (optional, the object should stick to the ground all the time): function updateObjectLocation_Handler() if(selectedObject ~= nil) then local pX, pY, pZ = getElementPosition(localPlayer); local pRX, pRY, pRZ = getElementRotation(localPlayer); local pD = getElementDistanceFromCentreOfMassToBaseOfModel(localPlayer); local oX, oY = (pX+4*math.cos(math.rad(pRZ+90))), (pY+4*math.sin(math.rad(pRZ+90))); local oZ = (pZ - pD + getElementDistanceFromCentreOfMassToBaseOfModel(selectedObject)); setElementPosition(selectedObject, oX, oY, oZ); end end Edit: I am not sure if the - pD is neccessary Link to comment
Dealman Posted November 6, 2014 Share Posted November 6, 2014 You could remove the calculation from the "Select" command handler or call the function updateObjectLocation_Handler().You could also add some other handy functionality for the height (optional, the object should stick to the ground all the time): function updateObjectLocation_Handler() if(selectedObject ~= nil) then local pX, pY, pZ = getElementPosition(localPlayer); local pRX, pRY, pRZ = getElementRotation(localPlayer); local pD = getElementDistanceFromCentreOfMassToBaseOfModel(localPlayer); local oX, oY = (pX+4*math.cos(math.rad(pRZ+90))), (pY+4*math.sin(math.rad(pRZ+90))); local oZ = (pZ - pD + getElementDistanceFromCentreOfMassToBaseOfModel(selectedObject)); setElementPosition(selectedObject, oX, oY, oZ); end end Edit: I am not sure if the - pD is neccessary Of course it could be improved, it was just pieced together in a few minutes in hopes it would assist him in what he was looking for. Trigonometry has always been my one true enemy, so any feedback on how to improve it is of course welcome. Link to comment
pa3ck Posted November 8, 2014 Author Share Posted November 8, 2014 Ah, so that wasn`t a code... lol, silly me. Thanks for the replies and the codes, I`ll test your them whenever I get the chance! 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