Jump to content

moveObject question


dzek (varez)

Recommended Posts

Posted

hi

i read somewhere here (cant find it ;/) that you cannot move objects that are stored in .map file.

I'm asking becouse i did, and when i'm getting objects i can destroy it, it has type of "object" etc etc, but i cannot make it move

and if i make same object via script, then move, its all ok.

is that tru or there is something wrong here:

function moveTo(obj, movX, movY, movZ, movTime, posX, posY, posZ)
--destroyElement(obj)
local test=createObject(3043, posX, posY+10, posZ)
moveObject (test, 3000, posX, posY, posZ+10)
moveObject (obj, 3000, -2494.3918457031, 1199.9107666016, 43.207420349121)
--outputChatBox(tonumber(movTime))
--setTimer (moveTo, movTime+1000, 1, obj, posX, posY, posZ, movTime, movX, movY, movZ)
end
 
local myElements = getElementsByType ("object", getRootElement())
num=0
for key,val in ipairs(myElements) do
num=num+1
--outputChatBox(num)
local moving = getElementData(val, "moving")
if (moving) then
local posX = getElementData(val, "posX")
local posY = getElementData(val, "posY")
local posZ = getElementData(val, "posZ")
local movX = getElementData(val, "movX")
local movY = getElementData(val, "movY")
local movZ = getElementData(val, "movZ")
local movTime = getElementData(val, "movTime")
	moveTo(val, movX, movY, movZ, movTime, posX, posY, posZ)
end
end

Posted

everything moves fine, this'll move all objects:

function moveAll(ms, byX, byY, byZ)
for i,theObject in ipairs(getElementsByType('object')) do
local x, y, z = getElementPosition(theObject)
   x, y, z = x + byX, y + byY, z + byZ
moveObject(theObject, ms, x, y, z) 
end
end

and what are you trying to achieve with getElementData?

Posted

@Gamesnert,

actually I have replaced all values that should be loaded from map file by numbers (look at the code)

moveObject (obj, 3000, -2494.3918457031, 1199.9107666016, 43.207420349121)

@xbost,

i will keep trying.

i use getElementData for reading addional attributes in map file (i think its ok?)

Posted

here, this moves moving objects on the map like this one (simple elevator going up-down):

<object id="testobject" model="3115" interior="0" dimension="0" posX="3446.6684570313" posY="-1914.6517333984" posZ="1.2000885009766" rotX="0" rotY="0" rotZ="0" moving="true" movTime="5000" movX="3446.6684570313" movY="-1914.6517333984" movZ="13.75008392334" />

function moveTo(obj, movX, movY, movZ, movTime, posX, posY, posZ)
local x, y, z = getElementPosition(obj)
if (tostring(z) ~= tostring(movZ)) or (tostring(y) ~= tostring(movY)) or (tostring(x) ~= tostring(movX)) then 
moveObject(obj, movTime, movX, movY, movZ)
else 
moveObject(obj, movTime, posX, posY, posZ)
end  
end    
 
addCommandHandler("movetest", 
function ()
for key,obj in ipairs(getElementsByType("object")) do
if getElementData(obj, "moving") then
local posX = getElementData(obj, "posX")
local posY = getElementData(obj, "posY")
local posZ = getElementData(obj, "posZ")
local movX = getElementData(obj, "movX")
local movY = getElementData(obj, "movY")
local movZ = getElementData(obj, "movZ")
local movTime = getElementData(obj, "movTime")
     moveTo(obj, movX, movY, movZ, movTime, posX, posY, posZ)
setTimer(moveTo, movTime+1000, 0, obj, movX, movY, movZ, movTime, posX, posY, posZ)
end
end
end  
)

i had to put tostring() in moveTo function, cause for some reason Lua thinks 13.75008392334 never equal 13.75008392334, even if z and movZ are the same number o.O

and i've tried tonumber() everything, still the same. ofc maybe i was just missing something there. :D

  • 3 weeks later...
Posted
i had to put tostring() in moveTo function, cause for some reason Lua thinks 13.75008392334 never equal 13.75008392334, even if z and movZ are the same number o.O

and i've tried tonumber() everything, still the same. ofc maybe i was just missing something there. :D

You should not compare floating point numbers like this. It is best to compare them with an epsilon value as such:

local epsilon = 0.0000001
function compareFloats(a, b)
if math.abs(a - b) < epsilon then
return true
end
 
return false
end

I have not tested this code. Good luck!

http://lua-users.org/wiki/FloatingPoint

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