Post news RSS Unit Movement

The first phase of gameplay code is well underway! First thing to tackle was unit movement. Read along here to check out how I've done it.

Posted by on

Welcome to this update.

I've been working on unit movement now. This is a major problem to tackle, but there's a very easy solution for it.

Unit movement is important for any RTS game (with the odd exception of some tower defense games). Any mobile unit is required to move. If you do it right, you will have excellent unit movement, but if you do it wrong, you can seriously make a game unbearable for many hardware setups.

The first thing to do is to look up some literature or tutorials about the subject. I've used Riemer's Angle to Direction and Direction to Angle tutorials to provide a basic guide to get it working. I used several parts of the code he supplied, like the transformation matrix.

The code is supplied here:

public void MoveTo()
        {

            
            if (destinations.Count > 0)
            {
                    Vector2 coordinatesdifference = new Vector2(destinations[0].X - location.X, destinations[0].Y - location.Y);
                    Matrix rotMatrix = Matrix.CreateRotationZ(rotation);
                    direction = Vector2.Transform(up, rotMatrix);
                    double lengthtocover = Math.Sqrt((double)((coordinatesdifference.X * coordinatesdifference.X) + (coordinatesdifference.Y * coordinatesdifference.Y)));
                    double acceleration = design.thrust / design.weight;
                    double timeneededtostop = speed / (acceleration);
                    double distancecoveredbetweenstop = (speed * timeneededtostop) - 0.5 * ((acceleration) * timeneededtostop * timeneededtostop);
                    if (speed == 0)
                    {
                        speed += acceleration;
                    }
                    else
                    {
                        if (lengthtocover < distancecoveredbetweenstop)
                        {
                            speed -= acceleration;
                        }
                        else
                        {
                            if (speed < design.maxspeed)
                            {
                                speed += (float)acceleration;
                            }
                        }
                    }
                    direction += coordinatesdifference * design.turnspeed;
                    rotation = (float)Math.Atan2(direction.X, -direction.Y);
                    location += (direction * (float)speed) * 0.05f;
                    


                    

                    if ((coordinatesdifference.X < 30 &amp;&amp; coordinatesdifference.X > -30) || (coordinatesdifference.Y < 30 &amp;&amp; coordinatesdifference.Y > -30))
                    {
                        destinations.RemoveAt(0);
                        if (destinations.Count == 0)
                        {
                            destinations.Add(new Vector2(r.Next(5000), r.Next(5000)));
                        }
                    }
                
            }
            
        }

That isn't much code, but it's critical. The trick to performance is keeping your code compact and efficiƫnt, and this code does that. You can check out the video how smoothly the game runs.

So far, I've spent about 8-9 hours programming on this entire engine, spread over 2 months. As you can see, I'm a very casual programmer. Including studying, this would be 23 hours. Casual programming works the best for me, so I'll continue down this line.

I will also add a download of the entire code plus installer (I prefer that you'll use the code instead of the installer in the publish folder).

Here's a video of the unit movement prototype:

Unit Movement Demonstration video - Star Apocalypse Game - Mod DB

Next steps will be to add mouse support, so you can really give orders to units!

Till the next time,
Roberto

Post a comment
Sign in or join with:

Only registered members can share their thoughts. So come on! Join the community today (totally free - or sign in with your social account on the right) and join in the conversation.