Reputation: 81
I need to change the object speed after I changed his rotation.
function love.update(dt)
if car['speed'] > 0 then
car['x'] = car['x'] + math.cos(car['angle'])
car['y'] = car['y'] + math.sin(car['angle'])
end
if love.keyboard.isDown("w") then
car['speed'] = car['speed'] + dt
end
if love.keyboard.isDown("a") then
car['angle'] = car['angle'] - (1 * dt)
end
if love.keyboard.isDown("d") then
car['angle'] = car['angle'] + (1 * dt)
end
end
After I changing the speed in the "w" I want it to change the speed of the car (x,y). But when I trying to add the speed its just changing the rotation and ruining it..
Upvotes: 1
Views: 92
Reputation: 16753
The required change is very simple: just scale the cos
and sin
by the car speed:
if car['speed'] > 0 then
car['x'] = car['x'] + car['speed'] * math.cos(car['angle'])
car['y'] = car['y'] + car['speed'] * math.sin(car['angle'])
end
Also, in Lua, car['speed']
is equivalent to car.speed
(syntactic sugar). Some people find this easier to read. The code above could be written as:
if car.speed > 0 then
car.x = car.x + car.speed * math.cos(car.angle)
car.y = car.y + car.speed * math.sin(car.angle)
end
PS: Don't forget to slow down the car ;)
Upvotes: 2