GitHub Game Off 2016 - Day 6

- 4 min read

What a day it was! Happy birthday to my cousin! Now because of all the celebrations I did not get to spend a lot of time working on my game, but I did manage to do something. The something I did was to implement some basic gravity, this time much more simpler with the possibility of me using my gravity calculation to show an arrow that points in the direction that gravity is currently pulling your spaceship.

Let’s see some code!

So the code for this is very simple:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
using System.Collections.Generic;
using UnityEngine;

public class GravityBody : MonoBehaviour
{
    private const float bigG = 0.9f;

    private readonly HashSet<GravityBody> bodies;
    private Rigidbody2D rigidbody2D;

    public GravityBody()
    {
        this.bodies = new HashSet<GravityBody>();
    }

    // Use this for initialization
    private void Start()
    {
        this.rigidbody2D = GetComponentInParent<Rigidbody2D>();

        var bodies = FindObjectsOfType<GravityBody>();

        foreach(var body in bodies)
        {
            this.AddBody(body);
        }
    }

    private void AddBody(GravityBody bodyToAdd)
    {
        if(bodyToAdd == this || this.bodies.Contains(bodyToAdd))
        {
            return;
        }

        this.bodies.Add(bodyToAdd);

        bodyToAdd.AddBody(this);
    }

    // Update is called once per frame
    private void Update() {}

    private void FixedUpdate()
    {
        foreach(var body in this.bodies)
        {
            var force = body.GetGravityBetweenBody(this);

            this.rigidbody2D.AddForce(force);
        }
    }

    private Vector2  GetGravityBetweenBody(GravityBody body)
    {
        var heading = this.rigidbody2D.position - body.rigidbody2D.position;

        var r = heading.magnitude;

        var force = GravityBody.bigG * (this.rigidbody2D.mass * body.rigidbody2D.mass) / (r * r);

        return heading.normalized * force;
    }
}

This gravity calculation will most likely not be the final version yet as it will currently calculate the force to apply for all objects using this script. In my Gravity Fight game this was what I wanted as I liked the concept of all asteroids interacting with each other. I am however going for a much more relaxed game whereby all asteroids are locked into their position and they’ll only affect the player spaceship flying by. One thing you’ll notice is that I’m using the F = (G * m1 * m2)/(r * r) equation albeit with an incorrect Gravitational Constant. The main problem with using the correct Gravitational Constant is that with low mass settings on the objects the gravitational pull between them become so minimal they might as well just be ignored.

For this process I start by loading all GravityBody objects into a HashSet taking care not to add duplicates. The actual calculation then just iterates through each body, calculates the heading and force to apply and simply asks the physics engine to apply the force. This will result in a nice net force with which the players ship will start moving. My thought on instead of applying each force individually I can sum up all the forces, because these forces are calculated as nice vectors, and only then apply the net force.

This net force can then also be used to allow me to display an arrow that can indicate to the player what the current net gravity force is that’s applied. I’ve got some experience playing Kerbal Space Program so I understand how gravity affects your flight, but it’s not always obvious to everybody. I’ve also been looking at moving towards a proper DDD type design that would allow me to write tests and unit test my game state.

Todo

So now that I’ve got basic flight and gravity going I’m most likely going to start looking at moving logic into a nice domain and building my game state in something like JSON then just loading it on startup. Another direction I might go in is to randomly generate some game states and just have the player jump into those scenarios.

The reason I want to look into these options is to allow me to quickly setup some scenarios that would allow me to test what I’m doing. Once I then have some playable scenarios in place I can start tweaking and adding some juicy things as well as have the camera follow the player.

So to sum up what I need to do (TL;DR):

  • Move game state into domain
  • Simpler way to compose scenarios
  • Camera movement to follow player
  • Add some juice

That’s it for today!