Scenario:
Implement a simplified 2D physics engine in Python using [Specify library, e.g., Pygame, Turtle, or a web-based canvas library like p5.js if applicable] to visually simulate the motion of multiple objects under the influence of gravity and collisions.
Requirements:
-
Object Representation:
- Create a class or data structure to represent 2D objects with properties such as position, velocity, mass, and size.
-
Gravity Simulation:
- Implement a function or method that updates the velocity of each object based on a constant downward gravitational force.
-
Collision Detection:
- Implement a function or method to detect collisions between objects (e.g., using simple circle collision detection or bounding box collision).
-
Collision Response:
- When a collision is detected, update the velocities of the colliding objects to simulate a simple elastic collision (e.g., using momentum conservation).
-
Visual Output:
- Use [Specify library] to visually represent the objects and their motion. Display the objects as circles or simple shapes.
-
Animation Loop:
- Create an animation loop that updates the object positions based on their velocities, applies gravity, detects collisions, and renders the objects on the screen.
-
Multiple Objects:
- Simulate the motion of at least three objects with different initial positions and velocities.
-
Boundary Conditions:
- Implement boundary conditions, such as bouncing off the edges of the screen or a simple floor.
-
Clear Comments:
- Add comments to explain the physics, collision detection, and rendering logic.
Evaluation:
The solution will be evaluated based on:
- The accuracy and realism of the physics simulation.
- The correctness of the collision detection and response.
- The smoothness and visual clarity of the animation.
- The organization and readability of the code.
- The inclusion of clear comments.
Optional Enhancements:
- Allow the user to interact with the simulation (e.g., adding or removing objects, changing gravity).
- Implement more advanced collision detection (e.g., polygon collisions).
- Add different types of forces (e.g., wind, friction).
- Implement a simple particle system.
Note:
- If you are using a library that requires specific setup or dependencies, please include the necessary installation instructions and example code for setting up the environment.