Using slope to determine angles/modifiers between two points?
Posted: Fri Jun 24, 2016 5:46 am
Hi all, I'm currently working on a top-down RPG styled game and in addition to WASD movement, I'm attempting to implement "click and hold" movement where the character moves towards your cursor. To do this, I'm attempting to use slope (and the x and y differences that calculate it) to help guide the character toward the cursor. (spoiler -- it isn't working)
The following is the function I'm using to determine slope, where x/y1 are the character's coords and x/y2 are the mouse's coords:
I'm using two libraries currently, being Lume and STI to help with development. Inside of the layer update function of STI, I currently have these equations to help figure out a few more required variables:
After gathering that information, I attempt to use the following code (when placed in mouseDown from the above snippet) to determine where the player should go, and how far in each direction (and in which direction) based upon the angle and slope:
However, the code isn't fully working. It chooses the correct direction (mostly) and initially takes what appears to be the correct speed/angle, yet the character does strange curves and only stops at either xDif == 0 or yDif == 0, never the "origin" of the two.
I've spent many, many hours toiling over this feature and I felt it was time to turn to the community for a nudge in the right direction. Is my current method almost correct/salvageable, or should I take an entirely different approach to this issue?
(attached will be the .love file and a pastebin paste. Much of the ground-work code comes from a tutorial on STI and Tiled implementation from Karai. I have received some help from Bartbes and EntranceJew on the IRC with some of the STI code. Thank you in advance to anybody who attempts to help out in solving this issue!)
Pastebin: http://pastebin.com/1JrrAjz4 (111 lines)
Gif of the issue: (please ignore strange colours/artifacting, and as a side note, are spoilers a thing on this forum?)
The following is the function I'm using to determine slope, where x/y1 are the character's coords and x/y2 are the mouse's coords:
Code: Select all
function slopeFinder(x1, y1, x2, y2)
local xDif, yDif = (x1 - x2), (y1 - y2) -- ensure that x1/y1 are the player!
local slope = yDif/xDif
return slope, yDif, xDif
end
Code: Select all
layer.update = function(self, dt)
local speed = 50
mouseDist = math.floor(lume.distance(self.player.x, self.player.y, mX, mY)) -- find distance from character and mouse position
timeM = mouseDist/speed -- finds the time in seconds it'll take at the current speed to reach the required location.
slope, xDif, yDif = slopeFinder(self.player.x, self.player.y, mX, mY) -- acquire the variables I need from the first function.
mouseDown = love.mouse.isDown(1)
if mouseDown then
-- mouse stuff here, will post in another code snippet
end
end
Code: Select all
if lume.sign(slope) == 1 then -- positive
if xDif or yDif ~= 0 or inf then
self.player.x = self.player.x - (xMod) * dt
self.player.y = self.player.y - (yMod) * dt
end
elseif lume.sign(slope) == -1 then -- negative
if xDif or yDif ~= 0 or inf then
self.player.x = self.player.x + (xMod) * dt
self.player.y = self.player.y + (yMod) * dt
end
end
I've spent many, many hours toiling over this feature and I felt it was time to turn to the community for a nudge in the right direction. Is my current method almost correct/salvageable, or should I take an entirely different approach to this issue?
(attached will be the .love file and a pastebin paste. Much of the ground-work code comes from a tutorial on STI and Tiled implementation from Karai. I have received some help from Bartbes and EntranceJew on the IRC with some of the STI code. Thank you in advance to anybody who attempts to help out in solving this issue!)
Pastebin: http://pastebin.com/1JrrAjz4 (111 lines)
Gif of the issue: (please ignore strange colours/artifacting, and as a side note, are spoilers a thing on this forum?)