[Solved]Having trouble making a flail weapon

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
User avatar
NickRock
Citizen
Posts: 76
Joined: Thu Dec 25, 2014 9:33 pm
Location: Earth
Contact:

[Solved]Having trouble making a flail weapon

Post by NickRock »

I'm making a game where you swing the flail with the mouse, I want the distance between the player and the square to be less than 150 pixels but I can't get it done

I tried this

Code: Select all

d = math.sqrt( (spikeball.x - chain.x)^2 + (spikeball.y - chain.y)^2 )
	if d < 150 then
		spikeball.x = love.mouse.getX()
		spikeball.y = love.mouse.getY()
	end
but it doesn't really work like I expected it to work

Image

You can download the .love file here
https://www.dropbox.com/s/kmq41zsksn21e ... .love?dl=0

I hope I explained everything well :)
Last edited by NickRock on Tue Aug 18, 2015 12:30 pm, edited 1 time in total.
Weeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeooow!!
User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: Having trouble making a flail weapon

Post by Nixola »

I think you should use vectors, or at least some vector math:

Code: Select all

local newX, newY = love.mouse.getPosition()
local vX, vY = newX - chain.x, newY - chain.y -- let's obtain the distance along the X and Y axes
d = math.sqrt( vX * vX + vY * vY ) -- here we calculate the actual distance
if d > 150 then -- if the distance is higher than what we want 
  vX, vY = vX/d, vY/d -- here we normalize the vector (divide its componenta by its length)  making it exactly one unit long; this allows us to multiply it by whatever we want, thus making it long exactly 150 units without changing its direction. 
  vX, vY = vX*150, vY*150
end
spikeball.x = chain.x + vX -- since vX is the horizontal distance, we should add it to the chain position
spikeball.y = chain.y + vY -- same as above
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
User avatar
NickRock
Citizen
Posts: 76
Joined: Thu Dec 25, 2014 9:33 pm
Location: Earth
Contact:

Re: Having trouble making a flail weapon

Post by NickRock »

Thank you, it makes sense now
Weeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeooow!!
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot] and 2 guests