[Solved] Spaceship movement (rotational)
Posted: Sun Jul 07, 2013 10:16 pm
I do not understand what is wrong with this picture I mean please help me!
http://pastebin.com/QDVf9xHz
http://pastebin.com/QDVf9xHz
Code: Select all
love.graphics.draw(
player.sprite,
player.x, player.y,
player.angle+math.pi/2,
1,1,
player.sprite:getWidth()/2,player.sprite:getHeight()/2)
Code: Select all
player = {}
player.sprite = love.graphics.newImage("graphics/player.png")
player.speed = 225
player.friction = 0.2
player.x = 100
player.y = 100
player.xvel = 0
player.yvel = 0
player.angle = 0
player.anglespeed=math.pi
function player.load()
width = love.graphics.getWidth()
height = love.graphics.getHeight()
end
function player.move(dt)
if love.keyboard.isDown("a") then
player.angle = player.angle - player.anglespeed*dt
end
if love.keyboard.isDown("d") then
player.angle = player.angle + player.anglespeed*dt
end
if love.keyboard.isDown("w") then
player.xvel = math.cos(player.angle)*player.speed
player.yvel = math.sin(player.angle)*player.speed
end
if love.keyboard.isDown("s") then
player.xvel = -math.cos(player.angle)*player.speed
player.yvel = -math.sin(player.angle)*player.speed
end
player.x = player.x + player.xvel*dt
player.y = player.y + player.yvel*dt
player.xvel = player.xvel * (1 - math.min(dt * player.friction, 1))
player.yvel = player.yvel * (1 - math.min(dt * player.friction, 1))
end
function player.update(dt)
player.move(dt)
end
function player.draw()
love.graphics.draw(player.sprite, player.x, player.y, player.angle+math.pi/2,1,1,player.sprite:getWidth()/2,player.sprite:getHeight()/2)
end
Wow thanks alot I learned quite a bit from your post, but I do have a question how can i make it be vertical by default, so 90° ?chezrom wrote:snip
You can set player.angle to -90° as default value (or -pi/2 in radians), to have initialy your ship pointing upward.Kripis wrote:Wow thanks alot I learned quite a bit from your post, but I do have a question how can i make it be vertical by default, so 90° ?
Code: Select all
vx = sin(angle) * speed
vy = -cos(angle) * speed
In this case, the only thing that you can't change is how love.graphics.draw understands its arguments. For this function the rotation is in radian, in clockwise orientation. You can define your angle (direction) as you want, but you must be coherent in your computations.Plu wrote:You can't change the default orientation, you'll just have to set the initial value of your rotation variable to 90 degrees.
Code: Select all
function player.move(dt)
local ax,ay=0,0
if love.keyboard.isDown("a") then
player.angle = player.angle - player.anglespeed*dt
end
if love.keyboard.isDown("d") then
player.angle = player.angle + player.anglespeed*dt
end
if love.keyboard.isDown("w") then
ax = math.sin(player.angle)*player.thrust
ay = -math.cos(player.angle)*player.thrust
end
player.xvel = player.xvel + ax*dt
player.yvel = player.yvel + ay*dt
player.x = player.x + player.xvel*dt
player.y = player.y + player.yvel*dt
end
Code: Select all
ax = ax - player.xvel * player.friction
ay = ay - player.yvel * player.friction
Code: Select all
player.friction = player.thrust/player.maxspeed
I cannot express how thankful I'am, your name will be in the credits of my game, Cheers!chezrom wrote:snip
Math.I do have 1 more question, how can I apply friction to the rotation?