Few questions regarding mouse-driven movement

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
0x29a
Prole
Posts: 27
Joined: Sun Jul 06, 2014 10:22 pm

Few questions regarding mouse-driven movement

Post by 0x29a »

Hi there!
It's my first post, so first I'd like to say hello to everyone!

I've got the problem with my prototype movement system. Been through all the internets, but I just can't wrap my mind around it...

First, I load useful libs
loader = require("ATL/Loader")
local anim8 = require 'anim8/anim8'
loader.path = "MAPS/"
local gamera = require 'gamera'
... and I define shorthand to compute rotation angle:
function findRotation(x1,y1,x2,y2)
return (math.atan2(y2 - y1, x2 - x1))
end -- learned from this forum :)





Secondly, I'm initiating everything like this:
function love.load()
love.window.setFullscreen(true, "desktop")
--THIS WILL GO TO THE CLASSES ONE DAY
image = love.graphics.newImage("img/little_running_dude.png")
local g = anim8.newGrid(64,64,image:getWidth(), image:getHeight())
animation = anim8.newAnimation(g('1-6',1), 0.2)
map = loader.load("FloorTest.tmx"); -- LOAD MAP
gCamX,gCamY = 15*592,15*592 -- SET POSITION TO THE CENTER OF THE WORLD (30 _BIG_ TILES )
cam = gamera.new(0, 0, 30*592, 30*592); --
map:setDrawRange(0,0,30*592, 30*592)
speed = 20 -- THIS WILL BE PLAYER SPEED
So far so good.

Next:
function love.update(dt)
HeroDirection = findRotation(love.graphics.getWidth()/2, love.graphics.getHeight()/2, love.mouse.getX(), love.mouse.getY()) -- * arbitraly chosen constant to speed up things.
if love.keyboard.isDown("w") then
-- WHAT SHOULD I DO HERE?
-- dude should move forward, towards upper center of the screen
-- beside this:
animation:update(dt)
elseif love.keyboard.isDown("s") then
-- AND HERE?
-- opposite to above
animation:update(dt)
elseif love.keyboard.isDown("d") then
-- dude should rotate around mouse cursor //strafe
animation:update(dt)
elseif love.keyboard.isDown("a") then
-- in opposite direction
animation:update(dt)
elseif love.keyboard.isDown("escape") then
love.event.quit()
else
animation:gotoFrame(1); -- dude is standing still
end

cam:setPosition(gCamX,gCamY) -- MOVE CAMERA
cam:setAngle(HeroDirection) -- AND ROTATE IT

end
Finally:
function love.draw()
cam:draw(function()
map:draw()
animation:draw(image, gCamX, gCamY, HeroDirection )
end)
end
Everything works kind'a fine, in e. world rotates, dude rotates as well - always facing upper center of the screen - so that's good. But when it comes to movement, I just give up. Tried everything I could think of and every solution I've found and only wierd stuff is happening.

How should I move the dude to the direction he's facing?
And - that would be awesome - is there any way to lock the mouse cursor position on demand? So it would serve as stationary crosshair until one will enter, lets say, menu mode?
//Edit: Actually, it could just stay in a given radius around the center of a screen.

Thanks in advance!
davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

Re: Few questions regarding mouse-driven movement

Post by davisdude »

For movement towards mouse:

Code: Select all

function MoveTowardsMouse( MouseX, MouseY, Object, Distance ) -- Object = { x = x, y = y }
    local Slope = ( MouseY - Object.y ) / ( MouseX - Object.x )
    local Intercept = Object.y - ( Slope * Object.x )
    local NewX = Object.x + Distance
    local NewY = Slope * NewX + Intercept
    return NewX, NewY
end
That is just one way to do that. There are many more ways.

Also, next time, please upload a .love and use the code tags instead of quotes. ;)
GitHub | MLib - Math and shape intersections library | Walt - Animation library | Brady - Camera library with parallax scrolling | Vim-love-docs - Help files and syntax coloring for Vim
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 4 guests