Before the workshop you should have watched the lectures. Some workshops have pre-workshop tasks which are optional, but useful. Completing labs before workshops is also optional (but may be useful)!
Your tutor will quickly explain over the starter code, and explain how to set every second line to be black.
If you have any questions, now is a great time to ask them!
This week's workshop will explore Rust's trait system in more detail. You have been provided with code that does not use traits at all, which simulates the motion of bodies under gravity. Your job will be to refactor the code, and possibly to expand on it.
In the library you have been provided, there are two types of object defined in an enum.
Planets do not move, but apply gravity to other
Asteroids move with a certain initial velocity,
and are affected by gravity.
Since the enum is defined by the library, it is not possible to extend the library's behaviour to include different object types. In this task, you will modify the code so that it can support user-defined objects.
Pre Workshop Work
The workshop code mostly re-uses the starter code from last week's workshop. We encourage you to try and copy your improved code into this week's starter code at the start of the workshop, or before.
Using the Simulator
In your starter code, you have been provided an HTML file called "phys_simulator.html". Open this file in your web browser to see a simulation of the planets on your screen.
Your tasks during the workshop are as follows:
- Refactor the code so that rather than taking a vec of enums, it takes a vec of
Planets, and a vec of Asteroids. Once you are done, you should be able to entirely remove
- Refactor the code so that Planets and Asteroids share a trait which defines their position, and allows conversion into a Circle.
- Refactor the code so that any object which is a "gravity source" can be passed instead of only Planets.
- Implement a different type of gravity source which pulses (i.e. has high gravity, then low gravity, then high again).
- Refactor the code so that any object which is a "gravity receiver" can be passed instead of only Asteroids.
- Implement an Asteroid which is only affected by gravity when it's further away than 100 units from a gravity source.
- (extension) If you have time, try writing a trait which returns
Option<&dyn GravityReceiver>. This will allow you to put your planets and asteroids back into a single Vec, and have objects which both are gravity sources and sinks.