I need help with absolute values!

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
User avatar
Fourex
Citizen
Posts: 53
Joined: Tue Nov 10, 2009 2:33 am
Location: Earth

I need help with absolute values!

Post by Fourex »

Specifically, this snippet of code. :

Code: Select all

Axvel = Axvel + ((Mx - Ax) / sqrt(abs((Mx-Ax)^2 + (My-Ay)^2))) * (Aacc / (sqrt(abs((Mx-Ax)^2 + (My-Ay)^2)))^2) * dt
FYI, Axvel, Mx, Ax, My, Ay, and Aacc are variables. And,

Ax = 100
Ay = 100
Axvel = 0
Aacc = 30
Mx = 0
My = 0

When I try to run the script this is in I get the ERROR! message "main.lua:43: attempt to call global 'abs' (a nil value)" (the line above is line 43)
Am I correct it assuming "abs()" is an absolute value function?
Can anyone help me? Thanks in advance!

This is the full code:

Code: Select all

function load()

	font = love.graphics.newFont(love.default_font, 12)
    love.graphics.setFont(font)

	Aball = love.graphics.newImage("greenball.png")
	Bball = love.graphics.newImage("pinkball.png")
	
	--Aball
	Ax = 100
	Ay = 100
	Axvel = 0
	Ayvel = 20
	
	Aacc = 30
	Afric = .99
	
	-- Bball
	Bx = 300
	By = 300
	
	Bxvel = 20
	Byvel = 0
	
	Bacc = .2
	Bfric = .999
	
	-- Mouse
	Mx = 0
	My = 0
	
end

function update(dt)
	
	-- Mouse
		Mx , My = love.mouse.getPosition() 
	
	-- Aball
		--Axvel = Axvel + (((Bx - Ax) * Aacc) * dt)
		--Ayvel = Ayvel + (((By - Ay) * Aacc) * dt)
		
		Axvel = Axvel + ((Mx - Ax) / sqrt(abs((Mx-Ax)^2 + (My-Ay)^2))) * (Aacc / (sqrt(abs((Mx-Ax)^2 + (My-Ay)^2)))^2) * dt
		Ayvel = Ayvel + ((MY - AY) / sqrt(abs((Mx-Ax)^2 + (My-Ay)^2))) * (Aacc / (sqrt(abs((Mx-Ax)^2 + (My-Ay)^2)))^2) * dt
		
		Axvel = Axvel * Afric
		Ayvel = Ayvel * Afric
		
	-- Bball
		Bxvel = Bxvel + (((Ax - Bx) * Bacc) * dt)
		Byvel = Byvel + (((Ay - By) * Bacc) * dt)
	
		Bxvel = Bxvel + (((Mx - Bx) * Bacc) * dt)
		Byvel = Byvel + (((My - By) * Bacc) * dt)
			
		Bxvel = Bxvel * Bfric
		Byvel = Byvel * Bfric
	
	if love.keyboard.isDown(love.key_space) then
	Axvel = 0
	Ayvel = 0
	Bxvel = 0
	Byvel = 0
	
	Ax = 100
	Ay = 100
	
	Bx = 300
	By = 300
	end
	
	Ax = Ax + Axvel
	Ay = Ay + Ayvel
	
	--Bx = Bx + Bxvel
	--By = By + Byvel
	
	
	
end

function draw()

	-- balls
	love.graphics.draw(Bball, Bx, By)
	love.graphics.draw(Aball, Ax, Ay)
	-- mouse
	love.graphics.draw("mouse", Mx, My)
	-- x coordinates
	love.graphics.draw("000", 0, 10)
	love.graphics.draw("100", 100, 10)
	love.graphics.draw("200", 200, 10)
	love.graphics.draw("300", 300, 10)
	love.graphics.draw("400", 400, 10)
	love.graphics.draw("500", 500, 10)
	love.graphics.draw("600", 600, 10)
	love.graphics.draw("700", 700, 10)
	love.graphics.draw("800", 800, 10)
end
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: I need help with absolute values!

Post by bartbes »

It's math.abs.
User avatar
Fourex
Citizen
Posts: 53
Joined: Tue Nov 10, 2009 2:33 am
Location: Earth

Re: I need help with absolute values!

Post by Fourex »

*sigh* it seems like all of the problems I have with love are so simple to fix... I guess that's a good thing though.
Post Reply

Who is online

Users browsing this forum: No registered users and 8 guests