• Register

Star Apocalypse is the reinvention of the 4X genre. The game is a real time game of intergalactic civilization, using the computation power of modern computers to run advanced simulations on cultural, technological and societal interaction and development.

Report article RSS Feed 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 dirtbag007 on Jun 12th, 2010

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:

csharp code:
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
click to sign in

You are not logged in, your comment will be anonymous unless you join the community today (totally free - or sign in with your social account on the right) which we encourage all contributors to do.

2000 characters limit; HTML formatting and smileys are not supported - text only

Icon
Star Apocalypse
Platform
Windows
Developer & Publisher
Darkmatter Development
Engine
XNA
Contact
Send Message
Official Page
Darkmatterdevelopment.eu
Release Date
TBD
Game Watch
Track this game
News
Browse
News
Views
82 (2 today)
Report Abuse
Report article
Related Games
Star Apocalypse
Star Apocalypse Single, Multiplayer & Co-Op 4X
Related Engines
Unknown
Unknown Commercial TBD
Related Groups
Darkmatter Development
Darkmatter Development Developer & Publisher with 6 members