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:
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?
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+.
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+.
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
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+.
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...
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