OpenGL代写:ENG1305 Simulation of Gravitational Interactions

代写OpenGL作业,模拟多物体之间的交互场景。

Introduction

Brief project description

This project comprises two parts. In the first part, which will be worth 5%, you will be undertaking some exercises to familiarise yourself with OpenGL graphics programming and animation. These tasks will help you with the second part, which forms the majority of the project.

In the second part, which will be worth 17%, you will be writing a C program that simulates the motion of N objects in space using some simple laws of physics. The motion of the N objects will be drawn graphically in a window using the OpenGL graphics library (an example is shown in Figure 1). You will be given a template C program that is fully functional. It calls pre-written functions from an external library (libgravity7.a).

Your task is to replace one-by-one each of these functions with your own code. The idea is that you will always be able to test your individual code units in a scaffolded framework, where the other working parts are already provided. The final mark that you receive will be based on how much pre-written code you have replaced with your own code.

There are two important requirements that need to be followed in order to receive marks for the assignment:

  • You must use this template program and utilise its functions in your computations.
  • You must implement the gravitational simulation equations described in this document yourself and use only the vector arithmetic functions contained in the template program. You may not use external code from other sources to perform these tasks, other than the provided external library.

You are to work in pairs on this assignment to distribute the workload of the programming. You will need to register your groups on [email protected] Please enrol in an assignment group along with your partner. The project can be divided into two major components that are to be allocated to each member of the group:

Physics component

This involves the correct programming of the mechanics and gravitational equations for computing the accelerations, velocities and displacements of all objects in the simulated system.

Graphics component

This involves the correct programming of the 3D graphics display using the OpenGL library to perform the drawing of the objects, their animation over time, display of simulation parameters, as well as the 3D rotation of the system.

Note that it is important for both members of the group to be fully aware of all aspects of the code and implementation. This will be assessed in a technical defence that will be held at the end of the trimester. Most importantly, the majority of this project should be completed in your own time, outside of class. The studio sessions will give you an opportunity to ask questions, but the two hours each week alone are not enough to complete the tasks.

Software required and compilation instructions

You will need to use a C compiler that has the OpenGL libraries installed. For Linux and Raspberry Pi users, please make sure you have the freeglut library installed. To do this, go to the software centre and install freeglut3-dev. Alternatively, you can go to a terminal and type:

sudo apt-get install freeglut3-dev

For Windows users, you can install Codeblocks to get the C compiler. To install the OpenGL and freeglut libraries, you will need to perform some additional steps, as listed on [email protected]
To compile the template, use the following command:

gcc gravity7_template.c libgravity7.a -lm -lglut -lGL -lGLU -O2 -o gravity7

The -O2 (which is ‘OH-two’, not ‘zero-two’) activates the code optimiser in gcc. This will improve the performance of the simulator, which is a computationally-intensive program.

Assessment methods

Summary of assessment items

Assessment item Marks Weighting % Due Date
Milestone report (Part 1) 5 5 Friday in Week 11
Project code and demonstration (Part 2) 50 17 Studio session in Week 12

Figure 1: Screenshot of an example simulation of N objects in space along with trajectory plots. Note that your program does not have replicate this interface exactly. Feel free to improve the layout and information presented.

Milestone report for Part 1

This milestone report is a Word document that contains:

  • a brief description of the coding you used to accomplish the tasks for Part 1 (there is no need to describe the OpenGL template);
  • a brief reflection on the functions used in each task and how they will be helpful to you in completing Part 2 of the project;
  • include a screenshot of the window from each of your programs (Tasks 1 to 3); and
  • C code should be included in an appendix.

This document should be converted to PDF format and submitted to the Part 1 submission point on [email protected] The C code for each task should also be submitted (as *.c files) as well at the same submission point.

Project demonstration

Part 2 of the project will be assessed in the studio laboratory session of Week 12. You and your partner need to present your working program and describe the various parts of the program to the teaching team, who will ask you questions to assess your knowledge of the program. This forms part of the technical defence, which you will encounter again in future years. Marks will be allocated based on this technical defence. You will also need to submit all your source code to [email protected] for checking in order to receive marks for the assignment. You are expected to write all the program yourselves. Academic misconduct, such as copying others’ work, will be closely monitored and not be tolerated.

Marks will also be given for code elegance (appropriate use of loops, indentation, suitable function length, etc.) as well as informative comments in the code.

Marking rubric for Part 2

This is the marking rubric that will be used for assessing Part 2 of the Project. Note that marks will only be assigned (according to the marking rubric below) to components that have been written by you. Any of the components that use provided code from the external library will not be awarded any marks.

Part 1: OpenGL programming

In this part, you will be undertaking some OpenGL tasks, which will prepare you for the major component of the project. Use the provided opengl_template.c for this task, not the one provided for the gravity project.

  1. Write an OpenGL program that draws a trapezoid (where each vertex is a different colour) using the GL_QUADS object. When the user presses the ‘h’ key, it should rotate the trapezoid clockwise by 5 degrees, while pressing the ‘g’ key will rotate it anti-clockwise by 5 degrees. Name this file task1.c.
  2. Write an OpenGL program that draws a 3D yellow cone in the middle of the window. When the user presses ‘w’, ‘a’, ‘s’, ‘d’ keys, the yellow cone should move left, down, right, and up the window, respectively by a small amount (0.05). If the user presses ‘g’, it should rotate along the axis (1, 1, 1) by 5 degrees. You should use the glutWireCone function for drawing the yellow cone. Use 32 slices and 8 stacks. The x and y co-ordinate should be printed out as well as the angle. Note that the text should not rotate along with the cone. See the figure below for an example. Replace the window title with your names and save this file as task2.c. (Hint: The glPushMatrix, glTranslatef, and glPopMatrix functions will be very useful).
  3. Write a program that draws an animation of a “seconds hand” on a clock. It should tick every second. This question will use the glutTimerFunc event handler. Name this file task3.c.
  4. (Optional) Modify the previous program to draw an animated “minute” and “hour” hand on a clock as well. See the figure below for an example.

Part 2: N-body simulation

You should use the provided template program gravity7_template.c to complete this task. It includes a header file (gravity7.h) and links to an external library (libgravity7.a), which contains the object code for the complete program. Inside the template program, you will see that the main and initialise functions have been written for you. The other functions contain a function call to their library equivalent (with a lib suffix). You are to comment out these function calls and enter your own C code in their place.

This task can be broken down into a number of stages:

  • Write a set of routines for performing 3D vector arithmetic using the provided C template;
  • Construct a system of N objects, each with their own masses, positions, initial velocities, and accelerations;
  • Initialise the system by placing objects (initially at random positions) within the window;
  • Compute the force vectors among the objects using Newton’s laws of motion and universal gravitation;
  • Compute the new positions of the objects at each time step;
  • Draw the moving objects in an OpenGL window in different colours along with technical information and parameters of the simulation;
  • Assign keys on the keyboard for changing the parameters within the simulation;
  • Assigns a key for ‘resetting’ the system of objects to an initial state.
  • Assigns keys to rotate the 3D view of the simulation
  • Extend the sophistication of the simulation system (e.g. setting up ellipsoid planetary motion, solar system simulation, etc.)

Background theory

Note: Bolded variables are vector quantities.

Newton’s Law of Universal Gravitation

All objects in space exert a force on each other due to gravity. Gravity is always an attractive force. Let us consider two objects that are at a distance of r m away from each other, each with a mass of m1 and m2 kg, respectively. According to Newton’s law of universal gravitation, the magnitude of the gravitational force between them is. Note that the force is a vector, so as well as magnitude, it will also have direction. The gravitational force vector is in the same direction as a vector that passes through the centres of the objects. This direction can be expressed as a unit vector of the displacement vector, as we shall see later.

Let us consider Figure 2, where two objects are located at their respective co-ordinates relative to an arbitrary origin. These positions can be represented as displacement vectors r 1 and r2.

Using Newton’s second law of motion

Now that we have computed the vector for the gravitational force on one object due to another, we will need to compute the change in displacement vector r of the object within a small time step t. Using Newton’s second law of motion (F = ma)

Simulating the movements of N objects

In the previous sections, we have outlined the mathematical equations for computing the force on one object due to the gravity of another, as well as computing the change of displacement (or position) within a small time step. Note that this describes the interaction between two objects only, but how do we apply this to a system of N objects?

Avoiding singularities in the simulation

Let us consider the law of gravitation formula in Eq. (1). In a practical simulation environment, there will be cases when the distance r between two objects becomes very small. As r approaches zero, the gravitational force and velocities will be extremely large, causing the objects to ‘fly away’ at such a rate that gravity will not be able to pull them back.

In order to avoid these singularities, we can add a softening or smoothing length greater than 0 into the equation.

This smoothing length effectively suppresses the action of gravity for distances less than. Choose a suitable for your simulation.

Neglecting the gravitational constant

The universal gravitational constant G can be dropped in the gravity simulations because it is a proportionality constant. Unless an accurate astronomical simulation is required, the gravitational constant can be dropped or equivalently, set G = 1.

Approximating elastic collisions of two objects

When two objects of masses m1 and m2 approach each other with velocities, respectively, they will collide with and ‘bounce off’ each other, with new velocities. The collision is elastic if the total kinetic energy before the collision is equal to the total kinetic energy after. In this simulation, we are going to assume perfectly elastic collisions in two-dimensions. Even though the simulation is in a three dimensional (3D) environment, the equations for 3D are very complicated2. Therefore, we are going to approximate them using the 2D version of the equations.

Implementation tips

Tips for implementing in C

  • Start by simulating two objects in space. Their trajectories are quite predictable;
  • Note that the trajectories of N objects in space (when N ] 2) is unpredictable and chaotic;
  • Choose a suitable time step for the simulation (small enough for an accurate simulation but not small enough to slow down the simulation);
  • Do a Google search on the mechanics equations for perfectly elastic collisions. In particular, the last vectorised equation on the Wikipedia page will be useful; and
  • Since the objects have no ‘size’, they will probably never collide. So set up a small radius (reps) around objects, such that when other objects enter this region, only one collision is detected.

Tips for OpenGL animation

You will find the following functions useful for creating a smooth animation in OpenGL. Do some research on the internet to find out how to use them. If you are using Linux and have installed the freeglut development libraries (freeglut3-dev), each of these functions will have their own man page, e.g. man glutSolidSphere

  • glutTimerFunc
  • glutPostRedisplay
  • glPushMatrix
  • glPopMatrix
  • glutSolidSphere
  • glTranslatef
  • glLoadIdentity
  • glRotatef