It looks like you're new here. If you want to get involved, click one of these buttons!
function CreeperBug:onEnterFrame() local currentX,currentY = self:getPosition() local nextX = self.scene.path[self.id].vertices.x[self.pathCounter] local nextY = self.scene.path[self.id].vertices.y[self.pathCounter] local xDiff = nextX - currentX local yDiff = nextY - currentY local distance = math.sqrt((xDiff*xDiff)+(yDiff*yDiff)) local angle = math.deg(math.atan2(yDiff,xDiff)) local nextX = currentX + (math.cos(angle) * 1) local nextY = currentY + (math.sin(angle) * 1) if(self.phase == "walk") then self:setPosition(nextX, nextY) end end |
Comments
my most common mistake is forgetting that Gideros has Cartesian coordinate system, thus y axis is always inverted
Other usual problem is that some math function might work not from the 0 to 360 degrees as expected, but for example from -180 to 180 degrees.
This is my usual way on getting angle:
Likes: Tom2012
local x1, y1 = 100, 100
local x2, y2 = 200, 200
local distanceBetween = math.ceil(math.sqrt( ((y1-y2) ^ 2) + ((x1-x2) ^ 2) ))
Likes: Tom2012
Here's a simple code that produces the problem I have:
This should work.
Likes: Tom2012
Thank you! That works perfectly. Will be using that code in my game.
I'm embarrassed to say I was passing degrees rather than radians to math.cos / sin.
I thought was getting better at this stuff. ">
Thank you again for the replies and help folks.