I'm learning about steering behaviors and watched this nice explanation tutorial - https://www.youtube.com/watch?v=g1jo_qsO5c4&feature=youtu.be with the source code available at https://github.com/kidscancode/gamedev/blob/master/tutorials/examples/steering/part01.py. The example app is written in python and PyGame and behaves like:

The main code parts of it are:


def seek(self, target):
    self.desired = (target - self.pos).normalize() * MAX_SPEED
    steer = (self.desired - self.vel)
    if steer.length() > MAX_FORCE:
    return steer

def update(self):
    self.acc = self.seek(pg.mouse.get_pos())
    self.vel += self.acc
    if self.vel.length() > MAX_SPEED:
    self.pos += self.vel
    # ...

I tried to reimplement this example in lua with Love2d framework and HUMP vector utility, source code is available at - https://github.com/voronianski-on-games/sandbox/blob/master/src/steering/seek_love2d/main.lua.

The code is pretty similar with the same constant values of MAX_FORCE and MAX_SPEED:

function Mob:new ()
  -- ...
  self.maxVelocity = 5 -- a.k.a MAX_SPEED 
  self.maxSeekForce = 0.1 -- a.k.a MAX_FORCE
  -- ...

function Mob:seek (target)
  self.desired = (target - self.pos)
  self.desired = self.desired * self.maxVelocity

  local steer = (self.desired - self.vel)

  if steer:len() > self.maxSeekForce then
    steer = steer:trimmed(self.maxSeekForce)

  return steer

function Mob:update (dt)
  self.acc = self:seek(vector(love.mouse.getPosition()))
  self.vel = self.vel + self.acc * dt

  if self.vel:len() > self.maxVelocity then
    self.vel = self.vel:trimmed(self.maxVelocity)

  self.pos = self.pos + self.vel * dt
  -- ...

But behavior is different. Square is much more slower in lua implementation though the values are the same:

What could be a problem? Or what is the difference that I couldn't notice?

  • 2
    \$\begingroup\$ In Mob:update (dt) time is measured IN SECONDS. So, your maximal speed is self.maxVelocity = 5 pixels per second. That's exactly what you have got. In Python program 1 time unit equals to time between adjacent frames. In Lua program, your calculations do not depend on frame rate (BTW, this approach to game programming is much better). \$\endgroup\$ Jan 9 '17 at 19:03
  • \$\begingroup\$ @EgorSkriptunoff thanks! so in order to make it faster it's fine to make numbers bigger enough? \$\endgroup\$
    – Kosmetika
    Jan 9 '17 at 21:03
  • 1
    \$\begingroup\$ Yes. 1 second is 60 times longer than 1 frame interval. Just multiply max speed and max acceleration by about 60. \$\endgroup\$ Jan 10 '17 at 19:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.