1
\$\begingroup\$

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:

MAX_SPEED = 5
MAX_FORCE = 0.1

def seek(self, target):
    self.desired = (target - self.pos).normalize() * MAX_SPEED
    steer = (self.desired - self.vel)
    if steer.length() > MAX_FORCE:
        steer.scale_to_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.vel.scale_to_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
  -- ...
end

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

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

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

  return steer
end

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)
  end

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

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?

\$\endgroup\$
  • \$\begingroup\$ Could be a simple difference in the frame rate? \$\endgroup\$ – Alexandre Vaillancourt Jan 9 '17 at 16:25
  • \$\begingroup\$ @AlexandreVaillancourt possibly.. my lua code runs 60 fps I don't know about pygame example. Also in python example they don't use dt at all... \$\endgroup\$ – Kosmetika Jan 9 '17 at 16:29
  • \$\begingroup\$ That's the first thing I'd try to verify: what's the framerate of the python code.. \$\endgroup\$ – Alexandre Vaillancourt Jan 9 '17 at 16:41
  • 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\$ – Egor Skriptunoff Jan 9 '17 at 19: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\$ – Egor Skriptunoff 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.