Forest Defender Devlog 2: Basic Camera and Movement
Tags: gamedev, pygame, python, devjournal
Published: 2022-10-28 17:59:12

## Forest Defender Devlog 2: Basic Camera and Movement

_Written on October 28th, 2022_

---

## Table of Contents

* <a href="#preface"> Preface </a>
    * <a href="#general-accomplishments"> General Accomplishments </a>
    * <a href="#movement-importance"> Why movement and camera motion is so important </a>
* <a href="#movement"> Movement </a>
* <a href="#gravity-collisions"> Gravity and Collisions </a>
    * <a href="#gravity"> Gravity </a>
    * <a href="#aspects"> A bit about aspects</a>
    * <a href="#collisions"> Collisions </a>
    * <a href="#other-physics"> Other Additions </a>
* <a href="#camera-enhancements"> Camera Enhancements </a>
* <a href="#conclusion"> Finishing Notes </a>
* <a href="#resources"> Resources / Further Reading </a>

---

<img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r141ab70wqjtase7jrh5.gif" alt="GIF of current game state." loading="lazy" style="min-width:300px; max-width:600px;" />

_This week's results of development. Each piece will be explained below._

---

<a id="preface"></a>

## Preface

<a id="general-accomplishments">

#### General Accomplishments

Since the previous devlog, [Forest Defender: development setup](https://dev.to/braeday/forest-defender-devlog-1-development-setup-21a2), a lot of groundwork has been completed. 

<img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/249geoizgj75cnz6rs12.png" alt="Spritesheet" loading="lazy" style="min-width:300px; max-width:600px;" />

We now have created a simple tileset. After some searching for a nice but small color palette, we settled on using the [Wild Jungle Color Scheme](https://www.schemecolor.com/wild-jungle.php) as it had the right feel of jungle vibes. It is also very narrow in its color range, really just being brown and green, which is exactly what we were looking for. I am a big fan of games built around minimal color differences since it reminds me of original Nintendo GameBoy games. In the created tileset, you can see we have just a handful of tiles to start with and two entities: a mushroom entity and the player. These will be expanded into animations later on.

We then took that tileset and created a basic testing level for movement by using [Tiled](https://www.mapeditor.org/). Building the level is easy and there are plenty of tutorials for reading the export from Tiled into a Python project via a custom import function. I will have a couple tutorials and examples linked in the further reading section at the end of this devlog.

Once the level was created, we plopped in our player's character and added in some gravity and controls. Using force based physics, it is easy to tweak movement to feel just right. We also added in a nice camera to follow the player around to get the first steps of this project complete. I have detailed how I did these movement and physics additions in the following sections.

<a id="movement-importance">

#### Why movement and camera motion are the most important piece of a game

General motion of the game (camera and player movement) is often overlooked by first time game developers. Many people either put in a simple system early on and work on improving it later in development, or they just use an out of the box solution. In my opinion, general motion is the most important part of a game and should be designed and made as perfect as possible for your end goal at the beginning of development. I am willing to play most games with great motion feel regardless of graphics, animation, etc. but I will usually get frustrated in a game if there are lots of collision problems, clunky movement, etc. even with beautiful graphics.

Let's start with what makes good movement in a game. In general, good movement refers to smooth motion, intuitive controls, and accurate collisions. Since there are various types of games, all with different ideal movements, let's just focus on our Forest Defender game. 

Since our game is 2-D, collisions are pretty easy. We really only need to worry about vertical collisions and horizontal collisions. We aren't going to have any tiles with special collision properties (such as steep slopes), so there isn't much more to plan for.

Our character is a 'fairy' type creature, so we designed the sprite with wings. While this won't help the fairy fly, this helps the character not fall too quickly and lets the player change direction mid-air. Therefore, when the character is falling or has jumped we want the player to be able to still move the character left and right just like the player was still on the ground.

Camera movement comes in too many varieties to describe them all. For our game, we are going with a simple camera movement. Essentially, when your character moves right, the camera moves right. When your character moves left, your camera moves left, etc. The biggest quality of life enhancement we are making to the camera is to give it a slight delay in its movement by using forces on the camera. This gives the camera a smoother look instead of jarring motion. This will be described more further down in this devlog.

---

<a id="movement"></a>

## Movement

The goal of our character movement is to be smooth and intuitive while fitting the character design.

Keeping motion smooth is generally easy. We will be using a force based system. So if the player presses the key to move right, a force will be applied to the character and it's acceleration will raise. The acceleration is then applied to the character's velocity each frame, which will move the character's position. The calculations are below, with dt being the time between last frame and current frame to make sure this process is frame-rate independent:

```python
force = 1 * dt
acceleration = acceleration + (force * dt)
velocity = velocity + (acceleration * dt)
position = position + (velocity * dt)
```

<a id="gravity-collisions"></a>

## Gravity and Collisions

<a id="gravity"></a>

#### Gravity

Gravity as a concept in 2-D physics is relatively straightforward: if down in the window is the ground, that is where the acceleration of gravity will point.

<a id="aspects"></a>

To add gravity to our character (and any future entities), we will include it as an _aspect_. An _aspect_ is essentially an additional property you can add to an entity in the Python game engine we are creating for this project. To explain it simply, each entity can have 0+ _aspects_ that it runs through each frame of the game. These _aspects_ can be for animation, physics, random checks, etc. We could create each of these as their own function or class, but that would make our entity class contain many properties that need to be maintained and entered individually. Instead, we can have a base _aspect_ class that other classes, like our gravity or physics movement, can inherit from. Then, we can simply add this new _aspect_ into a list of _aspects_ the entity holds onto and runs through each frame. Much easier and cleaner!

Back to gravity. Since in our game gravity will always be pointing downward in the window, we can just give the gravity acceleration a positive _y_ value. This is because in Pygame, downward is a positive value and upward is a negative value. You will notice I mentioned giving gravity a positive __acceleration__, not a __force__. This is due to gravity being a constant, and therefore the acceleration never changes.

To add our gravity in, we do a calculation very similar to the calculation for our movement, since it is essentially the same thing:

```python
acceleration.y = acceleration.y + (gravity * dt)
velocity.y = velocity.y + (acceleration * dt)
position.y = position.y + (velocity * dt)
```

Here we only modify the y value of the vectors since gravity only affects the player's vertical position. The result can be seen in the GIF below:

<img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zr7dhpxip710op4u7ghe.gif" alt="GIF showing gravity working in the game." loading="lazy" style="min-width:300px; max-width:600px;" />

<a id="collisions"></a>

#### Collisions

Collisions can become as complicated as you need them to be. For us, collisions will be on the simpler side. We will only be adding vertical collisions and horizontal collisions. Put even simpler, checking if an entity hits an object with the top of its head or the bottom of its body, or if it hits an object with the left or right side of its body.

While it sounds easy and simple, the code for this still looks a bit complicated. In the code snippet below, we are adding the movement to the entity in the x and y directions, then checking if it is within (therefore _hitting_) another object. For checking if there is a hit, we are using Pygame's built in sprite hit detection function _colliderect_. In the snippet, this is called with self.get_hits(tiles) which is a wrapper function I wrote and is included below.

If there was a collision detected with the entity, we add that hit to the collision_type dictionary to be referenced later in repositioning the entity so they are just outside the collided object (therefore colliding instead of going through). We also modify the velocity of the entity so the entity loses its inertia upon a collision.

The code we used for now is as follows:
```python
    def get_hits(self, tiles) -> list:
        hits = []
        for tile in tiles:
            if self.entity.rect.colliderect(tile):
                hits.append(tile)
        return hits

    def move(self, rect, velocity, tiles, dt):
        collision_types = {"top": False, "bottom": False, "right": False, "left": False}
        rect.x += velocity.x * dt
        hits = self.get_hits(tiles)
        for tile in hits:
            if velocity.x > 0:
                rect.right = tile.rect.left
                velocity.x = 0
                collision_types["right"] = True
            elif velocity.x < 0:
                rect.left = tile.rect.right
                velocity.x = 0
                collision_types["left"] = True
        rect.y += velocity.y * dt
        hits = self.get_hits(tiles)
        for tile in hits:
            if velocity.y > 0:
                rect.bottom = tile.rect.top
                velocity.y = 0
                collision_types["bottom"] = True
            elif velocity.y < 0:
                rect.top = tile.rect.bottom
                velocity.y = 0
                collision_types["top"] = True
        return rect, collision_types, velocity
```

<a id="other-physics"></a>

#### Other

Besides the above mentioned additions to the game, we mainly added quality of life additions. These mainly include applying maxes to velocity so the player can't move infinitely fast, a slight delay allowed when jumping i.e. you can be a few frames off a platform (like walking off) and still be able to jump, which improves the feeling of the game, and small force tweaks.

The applying of maxes is simple, as shown below:

```python
    def apply_maxes(self) -> None:
        if self.entity.velocity.x > self.entity.max_speed.x:
            self.entity.velocity.x = self.entity.max_speed.x
        elif self.entity.velocity.x < -self.entity.max_speed.x:
            self.entity.velocity.x = -self.entity.max_speed.x

        if self.entity.velocity.y > self.entity.max_speed.y:
            self.entity.velocity.y = self.entity.max_speed.y
        elif self.entity.velocity.y < -self.entity.max_jump_speed:
            self.entity.velocity.y = -self.entity.max_jump_speed
```

---

<a id="camera-enhancements"></a>

## Camera Enhancements

Basic camera movement is easy to implement. Simply place your camera's center position on the player character at the end of each frame when all the updates are done. This leads to harsh behavior of the camera though. The motion of the camera would feel jarring when changing directions.

We resolved this by instead applying forces to the camera for movement. Essentially we take the difference in position between the player's center and the camera's center positions and apply that to the default force applied to the camera. This means the further away the camera is from the player, the faster the camera will move toward the player. This is done easily in the code:

```python
self.position.x += (
            player.rect.centerx - (self.position.x + self.width / 2)
        ) / 5
self.position.y += (
            player.rect.centery - (self.position.y + self.height / 2)
        ) / 5

```

This gives a sort of 'lag behind' motion of the camera that smooths any jarring movements. You can see that in the GIF below.

<img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/58vsu0ca17s6jn3lki8g.gif" alt="GIF showing smoothed camera motion." loading="lazy" style="min-width:300px; max-width:600px;" />

You can see that instead of the camera being exactly on the player at all times, the camera sort of 'lags' behind the player. The distance * force feature we have added means the camera will never fall too far behind.

---

<a id="conclusion"></a>

## Finishing Notes

That is about all that was completed during this last bit of development. I will try to get these blogs out at least every two weeks, though the next few may be quicker since I have a lot of development worked on and just need to get it into devlogs.

If you have any questions, feel free to leave a comment and I will do my best to answer it!

--- 

<a id="resources"></a>

## Resources / Further Reading

<b> Reading from Tiled into Python </b>
* [Pygame](https://www.pygame.org/project-Tiled+TMX+Loader-2036-.html)
* [CDcodes](https://www.youtube.com/watch?v=37phHwLtaFg&ab_channel=CDcodes)

<b> Resources for Physics </b>
* [DaFluffyPotato Frame Independence](https://www.youtube.com/watch?v=OmkAUzvwsDk)
* [DaFluffyPotato Physics](https://www.youtube.com/watch?v=abH2MSBdnWc&t=1291s)
* [Quorum](https://quorumlanguage.com/tutorials/physics/forceandvelocity2D.html)


<b> Other </b>
* [Aseprite](https://www.aseprite.org/)
* [Pygame](https://www.pygame.org/news)
* [Tiled](https://www.mapeditor.org/)