# Implementing type-safe position, velocity and acceleration types

As discussed in this post there are some potential advantages in representing physical quantities like position, velocity, and similar in a type-safe way.

Instead of using simple floating point types, we can write our own wrapper types that have semantic meanings like ‘length’, ‘speed’, or ‘time’ and only allow physically correct operations between them.

That is exactly what we will look into here. We will implement the basic types for such a system, and show what operations between them are relevant and well defined.

# Type-safe physical unit representation

When developing games, working with positions and velocities, time stamps and time spans and similar physical quantities becomes second nature to us.

I would like to explore how we can represent the physical nature of these values, and their relation to each other in code.

# Basic game physics – fluid movement

An important aspect of almost any game is that things move right. The movement of objects needs to feel natural or intuitive, so that the players can interact with them effortlessly. We can use the manipulation of velocities and accelerations in order to achieve this.

# Basic game physics – from Newton to code

While we my not often think about it, movement of objects is one of the key aspects of most video games. In any game that has any sort of characters, enemies or items interacting in a fictional – or real – world, these agents and objects have to move in order for anything to happen.

In most cases, we want these movements to appear natural, or at least believable and consistent. This allows the player to better put themselves in the feet of the protagonist, or avatar, as they can control their movements in an intuitive way – not having to think about each key press individually, but merely willing to move in a certain direction and letting muscle memory take over from there.

# Brainstorming civilian AI

One of the projects I am currently working on is a game called Centipede in which the player controls a giant centipede in a city with the goal to cause as much havoc as possible.

Next to damaging and destroying buildings, vehicles, and other static objects, there are of course a large number of humans to serve as snacks, or – if armed – nuisances.

To make the game fun and engaging, it is important that these simulated humans react at least somewhat realistically to the chaos developing around them.

Ideally, their behaviour would also be reasonably complex and not entirely predictable, to make interacting with them more interesting.

# LINQ Batch extensions: A correction

In a post last year I explored how we can use LINQ to batch or page items from a collection in such away as to return a sequence of sub-sequence, each having equal length (except for possibly the last one). In that post I made a significant mistake that I would like to correct here.

# LINQ: Writing an Interweave extension

As we have seen in our last few posts on LINQ, the framework comes with some great extension methods to handle the merging of multiple collections into a single one.

However, there are things that LINQ can not easily do on its own. One of them is interweaving collections, i.e. merging them by taking elements from each input collection alternatively.

For example, given the inputs `1, 2, 3` and `a, b, c` we would get the output `1, a, 2, b, 3, c`.

Today, we will look into how to best implement this ourselves.

# LINQ: Flatten nested collections with SelectMany

Last time we looked at some of the possible ways of using LINQ to merge two collections into one.

Today we are going to look at another method that can be used to combine collections: `SelectMany`.

As an example of how multiple LINQ methods can be combined for interesting results we will use `SelectMany` and `Zip` to quickly implement a merging operation that LINQ does not have a method for: Interweaving elements from multiple collections.

Spoiler: Our interweaving implementation with `SelectMany` will be nice and short, but it will be sub-optimal with regards to performance. We will tackle that problem in a follow up post.

# LINQ: merging collections with Concat, Zip, and Join

Continuing my series of posts on LINQ, today I want to write about a few of the LINQ extension methods that take multiple input collections and return a single one.

Specifically, I want to take a look at the following methods: `Concat()`, `Zip()` and `Join()`. These all take two input collections, and combine their elements into a single resulting collection in different ways.

# Linq and set theory: Union() and Intersect()

So far, in my series of posts on LINQ we have covered only methods acting on single collections. Today we are going to look at some of the LINQ methods that take a second collection as parameter.

Specifically, we will at the classical set theory operators of union, intersection, and relative complement, which in LINQ are implemented with the `Union()` and `Intersect()` methods.