Newbie trigonometry help

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.
kurai
Prole
Posts: 8
Joined: Fri Mar 18, 2011 9:52 pm

Newbie trigonometry help

Post by kurai »

Hi there!
I'm a total newbie and I'm trying to make a simple asteroids-like game.
Thing is, I want the movement with a constant velocity. No acceleration, no inertia. You press a button, you move. You release it, you stop. That's it.
For now I did this:

Code: Select all

function love.load()
	love.graphics.setMode(1024, 768, false, false)
	hero = {}
	hero.angle=0;
	hero.speed=100;
	hero.x=love.graphics.getWidth()/2;
	hero.y=love.graphics.getHeight()/2;
	hero.sprite=love.graphics.newImage("art/player.png");
	
end


function love.draw()
	love.graphics.draw(hero.sprite,hero.x,hero.y,hero.angle*(math.pi / 180),1,1,32,32);
end

function love.update(dt)
	if love.keyboard.isDown("left") then
		hero.angle=hero.angle-hero.speed*dt
	end
	if love.keyboard.isDown("right") then
		hero.angle=hero.angle+hero.speed*dt
	end
	if love.keyboard.isDown("up") then
		hero.x=(hero.x+(math.cos(hero.angle)))
		hero.y=(hero.y-(math.sin(hero.angle)))
	end
end
and then I got confused. My player is not moving with the correct angle. I don't know where to put speed and dt on my up check. All in all, I'm stuck. Can you help me out?
User avatar
Taehl
Dreaming in associative arrays
Posts: 1025
Joined: Mon Jan 11, 2010 5:07 am
Location: CA, USA
Contact:

Re: Newbie trigonometry help

Post by Taehl »

Hi. You got the math a little messed up. Try changing it to this:

Code: Select all

function love.update(dt)
	if love.keyboard.isDown("left") then
		hero.angle=hero.angle-dt
	end
	if love.keyboard.isDown("right") then
		hero.angle=hero.angle+dt
	end
	if love.keyboard.isDown("up") then
		hero.x=hero.x+math.cos(hero.angle)*hero.speed*dt
		hero.y=hero.y+math.sin(hero.angle)*hero.speed*dt
	end
end
You multiply the speed by the delta-time by the (co)sine, and add it to your coordinates. In the rotation, you may need to multiply dt by something to make spin decently fast (I would suggest a number around 3).
Earliest Love2D supporter who can't Love anymore. Let me disable pixel shaders if I don't use them, dammit!
Lenovo Thinkpad X60 Tablet, built like a tank. But not fancy enough for Love2D 0.10.0+.
kurai
Prole
Posts: 8
Joined: Fri Mar 18, 2011 9:52 pm

Re: Newbie trigonometry help

Post by kurai »

Uhm, not working.
Sometimes it runs in the right direction, some other times it goes nuts and moves backwards or just in a weird direction...
User avatar
Taehl
Dreaming in associative arrays
Posts: 1025
Joined: Mon Jan 11, 2010 5:07 am
Location: CA, USA
Contact:

Re: Newbie trigonometry help

Post by Taehl »

Cos and sin might be backwards. Try swapping them.
Earliest Love2D supporter who can't Love anymore. Let me disable pixel shaders if I don't use them, dammit!
Lenovo Thinkpad X60 Tablet, built like a tank. But not fancy enough for Love2D 0.10.0+.
kurai
Prole
Posts: 8
Joined: Fri Mar 18, 2011 9:52 pm

Re: Newbie trigonometry help

Post by kurai »

Uhm, It's better, but it's still weird when not moving in orthogonal directions.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Newbie trigonometry help

Post by Robin »

Taehl wrote:Cos and sin might be backwards. Try swapping them.
No. x <-> cos and y <-> sin.

Things to look at:
  • Going to the left increases the angle, going to the right decreases it.
  • Math assumes positive y is up, LÖVE assumes positive y is down. Try subtracting the sine from the y, instead of adding it.
Help us help you: attach a .love.
kurai
Prole
Posts: 8
Joined: Fri Mar 18, 2011 9:52 pm

Re: Newbie trigonometry help

Post by kurai »

Still nothing.
The movement is totally strange, I post my code here:

Code: Select all


function love.load()
	love.graphics.setMode(1024, 768, false, false)
	hero = {}
	hero.angle=0;
	hero.speed=100;
	hero.x=love.graphics.getWidth()/2;
	hero.y=love.graphics.getHeight()/2;
	hero.sprite=love.graphics.newImage("art/player.png");
	
end


function love.draw()
	love.graphics.draw(hero.sprite,hero.x,hero.y,hero.angle*(math.pi / 180),1,1,32,32);
end

function love.update(dt)
	if love.keyboard.isDown("left") then
		hero.angle=hero.angle-dt*300
	end
	if love.keyboard.isDown("right") then
		hero.angle=hero.angle+dt*300
	end
	if love.keyboard.isDown("up") then
      hero.x=hero.x+math.cos(hero.angle)*hero.speed*dt
      hero.y=hero.y-math.sin(hero.angle)*hero.speed*dt
	end
end
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Newbie trigonometry help

Post by Robin »

dt*300? That is almost 50 rotations per second. Multiplying dt by something in the range 1-10 will probably work better.
Help us help you: attach a .love.
User avatar
Taehl
Dreaming in associative arrays
Posts: 1025
Joined: Mon Jan 11, 2010 5:07 am
Location: CA, USA
Contact:

Re: Newbie trigonometry help

Post by Taehl »

Taehl wrote:In the rotation, you may need to multiply dt by something to make spin decently fast (I would suggest a number around 3).
Earliest Love2D supporter who can't Love anymore. Let me disable pixel shaders if I don't use them, dammit!
Lenovo Thinkpad X60 Tablet, built like a tank. But not fancy enough for Love2D 0.10.0+.
User avatar
Adamantos
Prole
Posts: 27
Joined: Sun May 16, 2010 10:47 pm

Re: Newbie trigonometry help

Post by Adamantos »

hi,

you made a little mistake... your calculations for the movement are correct. But in the draw function, you
are converting the angle of the sprite from radiant to degrees. That's all...

Code: Select all

function love.load()
   love.graphics.setMode(1024, 768, false, false)
   hero = {}
   hero.angle=0;
   hero.speed=100;
   hero.rotatespeed=3;
   hero.x=love.graphics.getWidth()/2;
   hero.y=love.graphics.getHeight()/2;
   hero.sprite=love.graphics.newImage("art/player.png");
   
end


function love.draw()
   love.graphics.draw(hero.sprite,hero.x,hero.y,hero.angle,1,1,32,32);
end

function love.update(dt)
   if love.keyboard.isDown("left") then
      hero.angle = hero.angle - dt*hero.rotatespeed
   end
   if love.keyboard.isDown("right") then
      hero.angle = hero.angle + dt*hero.rotatespeed
   end
   if love.keyboard.isDown("up") then
      hero.x = hero.x + math.sin(hero.angle)*hero.speed*dt
      hero.y = hero.y - math.cos(hero.angle)*hero.speed*dt
   end
end
... hope I could help ^^
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot] and 5 guests