How do you go about "Inertia movement" and rotating sprites in Lua?

Hi everyone,

I'm an artist and just started to learn how to develop in Lua for the Playmate. I am building my first game where you control a spaceship and I'm wondering how do you go about "inertia movement" in Lua? As I been trying to research and cannot find anything, also I'm trying to rotate my sprite but I keep crashing on button press.

Thank youuu in advance

In short, for inertia movement (momentum) you adjust delta values, let's call them dx and dy, rather than x and y directly. If you do this using small fractional values it should become clear very quickly how this works.

For rotation the best approach is to pre-rotate your sprites and store them as an image table, then you can select which one to display at any moment based on your current angle.

Ahh I see :slight_smile: I will try and see, makes sense.

Thanks a lot Matt

1 Like

Keep asking questions, it's good

Will do and will definitely have more :slight_smile:

Can you explain a bit more about the delta values? I'm working on a top-down racing game, and I want the player to drift a little as they turn. Here's what I have for the current movement:

  if pd.buttonIsPressed(pd.kButtonUp) then
    local angle = math.rad(crankAngle - 90)
    local moveToX = self.x+ math.cos(angle) * self.speed
    local moveToY = self.y + math.sin(angle) * self.speed
    if moveToX > 0 and moveToX < 400 and moveToY > 0 and moveToY < 240 then
      self:moveWithCollisions(moveToX, moveToY)
    end
  end

I'm a designer/front-end dev, and completely new to game development. Are there any good resources around these concepts? I'm not having much luck with my searches other than SquidGodDev's amazing YT channel.

You can add inertia by varying the value of self.speed when the player is pressing/not pressing up. Something like:

if pd.buttonIsPressed(pd.kButtonUp) then
    self.speed = math.min(self.speed + self.acceleration, self.maximumSpeed)
else
    self.speed = math.max(self.speed - self.deceleration, 0)
end
local angle = math.rad(crankAngle - 90)
local moveToX = self.x + math.cos(angle) * self.speed
local moveToY = self.y + math.sin(angle) * self.speed
if moveToX > 0 and moveToX < 400 and moveToY > 0 and moveToY < 240 then
    self:moveWithCollisions(moveToX, moveToY)
end

However, the car will not drift. Drifting means that the car will not turn perfectly along its angle but will instead continue to drift a bit forward.

If you want to add a (basic) drift effect, you can keep two variables for the angle: the car angle (= crankAngle) and an other angle for the trajectory. You will then use the car angle to display the car rotation, and the other one to calculate moveToX and moveToY values.

You can make the angle for the trajectory vary the same way than speed. If the crank relative angle is positive, you add a predefined value. If it is negative, you subtract the same value.

2 Likes

Yes, that's the sort of thing.

This math.cos(angle) * self.speed is an example of what I would call the Delta value. It's the small change you're adding on this pass through the update loop.

For drifting, it's an extra thing you need to track - rotation. So you need to keep a heading angle and a delta angle.

You might want to look for examples of this in a language you are more familiar with? I'm sure there will be some JavaScript implementations on GitHub, which would be very easy to convert to Lua.

1 Like

Thank you so much for this. I will give the inertia a shot first, since that seems more straightforward to me. I'm not looking for super realistic drifting, just enough to smooth out the turns.

For your suggestion on how to do a basic drift effect, do you apply that with the inertia, or instead of the inertia?