Page 1 of 2
Newbie trigonometry help
Posted: Fri Mar 18, 2011 9:59 pm
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?
Re: Newbie trigonometry help
Posted: Sat Mar 19, 2011 4:54 am
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).
Re: Newbie trigonometry help
Posted: Sat Mar 19, 2011 1:05 pm
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...
Re: Newbie trigonometry help
Posted: Sat Mar 19, 2011 7:17 pm
by Taehl
Cos and sin might be backwards. Try swapping them.
Re: Newbie trigonometry help
Posted: Sat Mar 19, 2011 7:24 pm
by kurai
Uhm, It's better, but it's still weird when not moving in orthogonal directions.
Re: Newbie trigonometry help
Posted: Sat Mar 19, 2011 7:43 pm
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.
Re: Newbie trigonometry help
Posted: Sat Mar 19, 2011 7:56 pm
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
Re: Newbie trigonometry help
Posted: Sat Mar 19, 2011 9:43 pm
by Robin
dt*300? That is almost 50 rotations per second. Multiplying dt by something in the range 1-10 will probably work better.
Re: Newbie trigonometry help
Posted: Sun Mar 20, 2011 12:47 am
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).
Re: Newbie trigonometry help
Posted: Sun Mar 20, 2011 2:20 pm
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