200 likes | 220 Views
Developing An Educational Rigid Body Dynamics Physics Engine. By Neal Milstein. Abstract. Goal of Project: To create a rigid-body dynamics physics engine in order to allow physics students to visualize and solve physics models.
E N D
Developing An Educational Rigid Body Dynamics Physics Engine • By Neal Milstein
Abstract • Goal of Project: To create a rigid-body dynamics physics engine in order to allow physics students to visualize and solve physics models. • Rigid-body dynamics: The study of the motion of non- deformable, free-moving bodies through space.
Abstract (continued) • Engine • Focus on accuracy and speed • Algorithms Used: Runge-Kutta 4, Separate Axis Theorem, collision response via grids of ellipses • Interface • Focus on education, straightforwardness, and free flow
Introduction • Proper visualization of physical models required to properly understand physics concepts. • Computers are used because they replace real-life simulations and have less error margin • My project: simulates rigid bodies and their interactions in two-dimensional space
Micro vs. Macro • Physics engines – Present the unique problem of combining small-scale physical rigid-body oriented interactions with large-scale physics engine design • Micro – Small-scale interactions, algorithms, and designs. e.g. Separate Axis Theorem, Hooke’s Law • Macro – Large-scale design paradigms, architectural patterns. e.g. model-view-controller, separation of interactions from graphics.
Background - Separating Axis Theorem • A line can always be drawn between two concave non-intersecting bodies • What this means: objects are colliding if and only if all separating axes are colliding
Background – Runge-Kutta 4 Integration • Uses weighted average of past slope measurements to estimate h, the best slope estimate for the differential equation at the given time interval. • Much more accurate then Euler’s method, especially over large time step. • Can be used in combination with optimization techniques to calculate differential over many objects.
Development – Model-View-Controller • Separated into Model, View, and Controller (MVC design) • Model - Storage structure for physical components. Includes ways components are grouped and their connections with each other. • View - GUI to which components are displayed. Reads component models and outputs them to screen. Includes User Interface • Controller - Manipulates Models and prepares them for the view. Includes physics engine and physical interactions between models.
Development – First step: Model • Model, underlying structure of physical objects, was developed first. • Made up of classes that store information, with not too many functional methods (by design)
Example of a Model Class package model; publicabstractclass Body extends Element implements Referenceable, Massable, Anchorable, Collideable { protected Reference reference; protecteddoublemass; protectedbooleanisAnchored; privatebooleancolliding = false; public Body(Reference reference, double mass) { this.reference = reference; this.mass = mass; isAnchored = false; } public Body(Reference reference, double mass, boolean isAnchored) { this.reference = reference; this.mass = mass; this.isAnchored = isAnchored; } publicvoid addForce(double forceMag, Vector direction) { double acceleration = forceMag / mass; this.getReference().addAcceleration(new Vector(acceleration, direction)); }
Development – Controller Implementation • Increases time step • Cycles through Interacter objects and calls their interact() method • Passes the model as an argument to the Graphical User Interface • Some current Interacters: • CollisionInteracter (Separating Axis Theorem) • SpringInteracter (Hooke’s Law) • ReferenceableInteracter (Runge-Kutta 4)
Example of an Interacter Class publicclass SpringInteracter extends Interacter { public SpringInteracter(Model model) { super(model); } publicvoid interact() { for (Element e : model) { if (e instanceof Spring) { Spring s = (Spring)e; double forceMag = s.getForce(); Vector direction = s.getBodyA().getReference().getPosition().minus(s.getBodyB().getReference().getPosition()); s.getBodyA().addForce(forceMag, direction); s.getBodyB().addForce(forceMag, direction.getNegative()); } } } }
Collision Response • Generally involve calculations such as rotational momentum, moment of inertia, and conservation of energy • My physics engine uses “grids” of elliptical bodies to simulate the responses to these collisions
Collision Response (Continued) An example of the ellipses grids used to detect collisions. Here, a collision is simulated (programmatically) with the light blue ellipse, which then drags the rest of the shape along with it.
Collision Response (Continued) • Drag forces • Use drag formula to prevent objects from continuously moving • When coupled with a high spring constant, collision response is quite accurate
Development - View • Graphical User Interface • View.render(Model model) method called every time step • View draws the model to the screen • Also contains interface buttons that feed commands back to the controller
Results • Easily create new simulations just by drawing the objects to the GUI or programmatically defining them • Demonstrated Conservation of Energy – Center of Mass always in center of screen.
Functionality • Detect Collisions via the Separate Axis Theorem – collision response (dynamics) on the way • Simulate tension interactions with Hooke’s Law • 2-D motion accurate due to Runge-Kutta 4 • Draw Bodies on screen, have them connected with springs • Simulate any number of bodies in any configuration
Testing Results • Online models of predefined physics problems • Physics textbook problems that can be solved by hand • Testing Conservation of Energy or Momentum
Conclusions • If properly modularized, physics engines can be expandable and scalable. • Key is in separation of programming areas (model from view from controller) • Simplification of interactions