UM-SJTU JI VG101(23SU)
Lab Demo Project 1: Gravity Simulator
Gravity exists between all objects. According to Newton's Second Law , a force applied to an object will change its acceleration, affecting its velocity and position indirectly . The three-body problem is a problem regarding gravity . Imagine that, in an empty space, there are three objects of the same mass. They will begin to move due to gravity between them. Their motion is actually very hard to predict because even a very tiny dif ference in the initial setup can af fect their loci tremendously . To see this, computers are often used to simulate their movement.
This project asks you to follow our guidance to implement a gravity simulator in MATLAB.
The ﬁnal product should be a program that animates the movement of several (more than three!) spherical objects, or balls, with dif ferent color , radius (mass) as well as initial location, velocity and acceleration. The balls attract each other due to gravity , and collide completely elastically .
You can ﬁnd on Canvas a demo video showing how the program is supposed to work.
Milestones and grading policy
This project will last for 3 lab sessions. At the end of each lab session, you are expected to complete a part of this project, called a milestone . The requirements for each milestone will be documented in the corresponding lab worksheet.
Each milestone is due Thursday at 10:00 p.m. Y ou must submit your code to Canvas before the deadline to receive points for this part. Y our code will be graded on its completeness , not correctness. Students who make reasonable ef forts to complete the tasks for a milestone will receive full marks for the demo project section of the corresponding lab. On the contrary , non- sensical code, too few lines of code or irrelevant submissions will receive no point. However , a deduction may apply if your code style is considered bad. See the "Code quality" section for details.
The due date for demo project submission is Thursday at 10:00 p.m. No late submission will be aceepted. Demo projects are aimed to help you get a sense of what a software project is like and also practice your programming skills learnt from the lectures. The goal is not to judge your ability . So you will not need to worry about your grade as long as you try your best to follow the TAs during the lab.
This project basically asks you to produce an animation. Animations are just pictures played sequentially at a high rate. Each picture is called a frame . Thus, you goal for this project is just to generate and render each frame.
The rate of frame update is measured in frame per second , or FPS.
MVC : model , view and controller
The MVC architecture is commonly used to develop large-scale projects. It divides a program into three main components:
The model . It is a data structure that contains all information about the current state of your program.
The view. It is responsible for visualizing the model and rendering the canvas, or providing the UI (User Interface).
The controller . It contains all the logic behind the program.
A program with the MVC architecture ﬁrst initiates the model using some predeﬁned default values and renders the view according to the initial model, then runs such a main loop: The view passes all user input gathered, if any , to the controller . The controller then decides how to update the model accordingly , and instructs the view to render the updated model.
In this project, you are expected to use the MVC architecture: The model is several arrays that store the properties and statuses of each ball. The view is a function that calls MA TLAB built-in plotting functions to render the balls on the canvas.
The controller is some code that calculates updated values of position, speed and acceleration for each ball using formulae from physics. Setup and initialization The canvas is the region . Your MATLAB plot window should always display this region. To initialize the model, ﬁrst ask the user to input a natural number . This will be the number of balls. For each ball, its initial position should be randomly chosen, but must be within the canvas. Feel free to play with dif ferent orders of magnitude to ﬁnd appropriate ranges for random radii, initial velocities and accelerations. Colors can also be randomly generated. See the documentation for ColorSpec for details about how to represent a color .
Note: In this project, we ignore the units of all physical quantities. Do not do this in VP150 or VP160!
The main loop of the program is executed once every seconds. W e assume that within , the speed and acceleration of any ball remains unchanged. Thus, the main loop should update each ball with these formulae: where , and are the initial position, velocity and total acceleration, and and are the updated position and velocity .
The acceleration of each ball should also be updated. T o calculate the updated acceleration, we ﬁrst need to calculate the gravity .
As we know , , the gravity that ball exerts on ball can be calculated as where has the magnitude equal to the distance between and and points from to ; are the masses of and . In this project, we assume that the gravitational constant is to scale things properly .
We use the formula− 1 0 ≤ x ≤ 1 0 , − 1 0 ≤ y ≤ 1 0 n > 1 d t = F P S1 d t = x′ + x , = v v′ + v a x v a x′ v′ F i j i j = F i j − G ∥ r i j ∥3m m i jr i j r i j i j i j m , m i j i j G = 1 0 0 0 to calculate the total force exerted on ball .
According to Newton's Second Law , acceleration of a given object and the total force exerted on it are related by the formula where is the mass of the object. W e assume that the balls all have the density so that the mass of a ball is equal in magnitude to . Finally , we can calculate the updated acceleration of a given ball as
Elastic collision between balls
It is inevitable that sometimes balls do collide. In this project, you are asked to detect such collisions and deal with them as if all collisions were completely elastic. If two balls collide completely elastically , we can calculate their updated velocities after the collision through the following steps. These will be justiﬁed later in VP150 or VP160.
- Calculate the unit direction vector:
- Calculate the radial and tangential components of the velocity , and , of each ball :
- Calculate the updated radial component of the velocity for each ball: = F i , t o t i = j ∑ F j i i = F m ⋅ a m ρ = 4 π3 m r3 i = a i .r i3 F i , t o t = r i j . − ∥ x j x i ∥ − x j x i v r v τ = v i , r ∘ v i ⋅ r i j . r i j = v i , τ − v i . v i , r = v i , r′ .m + m i j( m − m ) + 2 m i j v i , r j v j , r = v j , r′ .m + m i j( m − m ) + 2 m j i v j , r i v i , r
- Add the updated radial component with the original tangential component to obtain the
updated velocity for each ball:
To avoid balls ﬂying outside the canvas, we set boundaries at and . Balls that touch the boundaries will be reﬂected. The calculations are trivial, thereby omitted here.
You are not required to check for invalid input though it is very easy to do, as the only input is a number . You can decide on the FPS, range for radii, etc. on your own. The only requirement is that your values should make sense, i.e., a user should be able to understand what your animation is by simply watching it. Code quality Your code quality will be inspected. A 20% deduction may apply if your code style is considered bad. Please stick to our rules to avoid receiving this deduction. Generally , you should: Make your code modular and reuse code wherever possible. Use functions. If you ﬁnd yourself copying and pasting your code, you are likely doing it wrong. Give reasonable and meaningful names to your variables and functions. Use indentation properly . Avoid "magic numbers" in your code. Give constants reasonable and meaningful names or write comments that explain what they are. Write comments for functions and code that is hard to understand if none was provided.
VG101 SU21 demo project 1 VG101 SU22 demo project 1= v′