# Automatically triggering standard spaceship controls to stop its motion

Filed under:
|
##### love2d

I have been working on a 2D top-down space strategy/shooting game. Right now it is only in the prototyping stage (I have gotten basic movement) but now I am trying to write a function that will stop the ship based on it's velocity. This is being written in Lua, using the Love2D engine.

My code is as follows (note- object.dx is the x-velocity, object.dy is the y-velocity, object.acc is the acceleration, and object.r is the rotation in radians):

``````function stopMoving(object, dt)
local targetr = math.atan2(object.dy, object.dx)
if targetr == object.r + math.pi then
local currentspeed = math.sqrt(object.dx*object.dx+object.dy*object.dy)
if currentspeed ~= 0 then
object.dx = object.dx + object.acc*dt*math.cos(object.r)
object.dy = object.dy + object.acc*dt*math.sin(object.r)
end
else
if (targetr - object.r) >= math.pi then
object.r = object.r - object.turnspeed*dt
else
object.r = object.r + object.turnspeed*dt
end
end
end
``````

It is implemented in the update function as:

``````if love.keyboard.isDown("backspace") then
stopMoving(player, dt)
end
``````

The problem is that when I am holding down backspace, it spins the player clockwise (though I am trying to have it go the direction that would be the most efficient at getting to the angle it would have to be) and then it never starts to accelerate the player in the direction opposite to it's velocity.

What should I change in this code to get that to work?

EDIT :

I'm not trying to just stop the player in place, I'm trying to get it to use it's normal commands to neutralize it's existing velocity.

I also changed math.atan to math.atan2, apparently it's better. I noticed no difference when running it, though.

© Game Development or respective owner

### Related posts about lua

• #### Building Awesome WM

as seen on Ask Ubuntu - Search for 'Ask Ubuntu'
Hello, I am following these steps in order to build Awesome window manager on 10.04 I am building 3.4 while the tutorial is for 3.1 I installed all of the specified dependencies including cairo. After running cd awesome-3.4 && make I get the following missing dependencies error: Running… >>> More

• #### Building Awesome WM

as seen on Server Fault - Search for 'Server Fault'
Hello, I am following these steps in order to build Awesome window manager on 10.04 I am building 3.4 while the tutorial is for 3.1 I installed all of the specified dependencies including cairo. EDIT I ran: sudo apt-get install libxcb-xtest0-dev libxcb-property1-dev libxdg-basedir-dev libstartup-notification0-dev… >>> More

• #### Lua/SWIG wrap userdata from within Lua

as seen on Stack Overflow - Search for 'Stack Overflow'
I am using swig-lua. I have a function in Lua which I call and it returns me a pointer (userdata). right now I know what this pointer is, but how can I tell Lua from within Lua? >>> More

• #### tolua++: Adding lua functions to a C++ class that has been exported to Lua

as seen on Stack Overflow - Search for 'Stack Overflow'
I am using tolua++ to export some C++ classes to Lua. My understanding that each class is 'implemented' on the lua side, as a lua table. I am wondering if it is possible therefore, to add new (Lua) methods to the C++ object table so that I can achieve the effect of adding new methods to the C++ class… >>> More

• #### conf.lua not being read (Lua/LOVE 0.8.0)

as seen on Stack Overflow - Search for 'Stack Overflow'
I need a higher resolution for my program to run. For some reason I cannot discern alone, LOVE is not applying (or finding?) conf.lua. My folder architecture is as follows: basefolder/ basefolder/main.lua basefolder/conf.lua basefolder/Resources/ My conf.lua reads: function love.conf(t) … >>> More

### Related posts about love2d

• #### Love2D engine for Lua; What about 3D?

as seen on Game Development - Search for 'Game Development'
Lua has been really awesome to learn, it's so simple. I really enjoy scripting languages, and I had an equally enjoyable time learning Python. The Love engine, http://love2d.org/, is really awesome, but I'm looking for something that can handle 3D as well. Is there anything that accommodates 3D in… >>> More

• #### How to use batch rendering with an entity component system?

as seen on Game Development - Search for 'Game Development'
I have an entity component system and a 2D rendering engine. Because I have a lot of repeating sprites (the entities are non-animated, the background is tile based) I would really like to use batch rendering to reduce calls to the drawing routine. What would be the best way to integrate this with… >>> More

• #### Turning off antialiasing in Löve2D

as seen on Stack Overflow - Search for 'Stack Overflow'
I'm using Löve2D for writing a small game. Löve2D is an open source game engine for Lua. The problem I'm encountering is that some antialias filter is automatically applied to your sprites when you draw it at non-integer positions. love.graphics.draw( sprite, x, y ) So when x or y is not round… >>> More

• #### How can i run my .LÖVE game directly from the lua interpreter?

as seen on Game Development - Search for 'Game Development'
I've just started with LOVE and LUA , i'm interested in LOVE because i want to play around with something different from my dayjob(i'm a webdeveloper) and since it uses LUA and is interpreted , i though it would be a great way to try out the API. but i couldn't find how to run my .LÖVE game directly… >>> More

• #### How should I share variables between instances/classes?

as seen on Game Development - Search for 'Game Development'
I'm making a game using LOVE, so everything is programmed in Lua. I've been experimenting with using classes and object orientation recently. I've found out that a nice system to use is having most of the game's code in different classes, and having a table of instances with all of the instances of… >>> More