/ #Game Off 2016 #Game Jam 

GitHub Game Off 2016 - Day 6

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:

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!

Author

Sas van der Westhuizen

Intermediate Software Engineer at Entelect; "engineering" since 2015; loves games, rock climbing; been trying my hand at making games, but nothing good yet!