Wednesday, September 12, 2018

Simple Solar System - C#

The second of my C#/Unity mini-projects is a simple scaled model of our solar system. While the graphics are anything but pretty (I had trouble finding nice planetary textures without having to pay for them), the planets themselves are scaled linearly with the sun being 10.2 cm in diameter. In addition to this, the distances between each planet and the Sun has been scaled in a logarithmic manner (base 10). Here are some images of the solar system, along with close-ups of the rocky planets and gas giants, respectively:


I also made each planet rotate around the sun at a specific speed by dividing their actual orbital velocities with that of the Earth to obtain a non-dimensional rate. Here is a snippet of code for one of those planets' rotation:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class rotate_earth : MonoBehaviour {

 void Start () {
  
 }
 
 void Update () {
        transform.RotateAround(transform.position, new Vector3(0, 1, 0), 1.0f);
    }
}
The rest were almost the same except for the class name and the third transform value, which would be different for every planet in accordance to their velocity. In addition to this, I added a first-person camera that can move along the x and y axes with the WASD keys, and rotate in 3 degrees of freedom using the mouse:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class first_person_cam : MonoBehaviour {

    public float speedH = 1.0f;
    public float speedV = 1.0f;

    private float yaw = 0.0f;
    private float pitch = 0.0f;

    float x = 1.0f;
    float y = 1.0f;
    float z = 1.0f;

 void Start () {
  
 }
 
 void Update () {

        yaw += speedH * Input.GetAxis("Mouse X");
        pitch -= speedV * Input.GetAxis("Mouse Y");

        transform.eulerAngles = new Vector3(pitch, yaw, 0.0f);

        x = Input.GetAxis("Horizontal");
        y = Input.GetAxis("Vertical");
        z = Input.GetAxis("ZMove");

        transform.Translate(x, y, z);
  
 }
}
That's it for today, nothing too involving. Tomorrow I will show my third C# program-a differential equation solver that makes use of Euler's method to complete its calculations.

No comments:

Post a Comment