I'm developing 2d space shooter with LOVE2D in Lua and as I'm not math and physics expert I got some questions with implementing steering behavior for enemies.
I finally managed to make enemy "seeking" for a player and it looks like:
Code that makes it work (formula is taken from this answer - https://stackoverflow.com/a/2561054/2117550):
function Enemy:seek ()
local dx = self.player.x - self.x - self.xvel
local dy = self.player.y - self.y - self.yvel
-- normalize
local len = math.sqrt(dx * dx + dy * dy)
dx, dy = dx / len, dy / len
return dx, dy
end
Then I use my seek
function in update
:
function Enemy:update (dt)
-- seeking acceleration
local dx, dy = self:seek()
-- what is the proper way of calculating enemies rotation?
-- self.rotation = math.atan2(dx, dy) + ANGLE_ACCELERATION * dt
-- update velocity
self.xvel = self.xvel + dx * MAX_SPEED * dt -- * math.cos(self.rotation) it's not needed anymore?
self.yvel = self.yvel + dy * MAX_SPEED * dt -- * math.sin(self.rotation) it's not needed anymore?
-- moving entity in camera
_.checkWorldBounds(self)
local futureX = self.x + self.xvel * dt
local futureY = self.y + self.yvel * dt
local nextX, nextY, collisions, len = self.world:move(self, futureX, futureY, self.collisionFilter)
self.x = nextX
self.y = nextY
self.xvel = self.xvel * 0.99
self.yvel = self.yvel * 0.99
end
So the only problem now is that enemy's rotation is not changing though the front of the spaceship should always look into player's side.
I tried with math.atan2(dx, dy)
but it makes entity rotating constantly.
What is missing or what am I doing wrong?
I'm not looking for someone to code this for me (although it would be very nice) but some formula or piece of advice will be highly appreciated.
If you're interested the source code is available at - https://github.com/voronianski-on-games/asteroids-on-steroids-love2d
I can't tell you what is wrong with your code, but hopefully this will help you out.
Note that by using
body:setAngle(angle)
('body' is your objects body) your objects will be instantly rotated to given angle. If you want them to rotate at certain pace you could use:Also note that the 'front' of your ship/body/object is to the right along the x-axis. In other words the angle is calculated from the x-axis and 0 means that the object is facing right.