This tutorial is continuation of "Matrix 2d" where i will show simple 2d solar system simulations using matrices
We will define 4 Astral objects:
All astral object will have only one point that define their position in their local space. For this simple example all astral object will be located in their own center of local space, this mean that all astral object will hold only one point (0.0, 0.0, 1.0) except star that will be defined by 6 points.
Orbit will be wrapper for one Astral object, and will move child local space inside orbit space, this will actually move it's child without them knowing about this operation. This works just like in real live, planet is rotating around sun but people on earth do not know about this movement.
Abstract object holds information about :
- size and object color
- model matrix - this is local matrix it will translate/rotate current astral abject and all child with it.
- modelViewModel - this is buffer for merged model matrix and parent matrix
Astral object will have 4 methods:
- draw - this method will merge model matrix with it parent matrix, after that it will invoke abstract drawImpl with merged matrix.
- update - this method will be executed by timer with time difference between current call and previous call
- calculateModelViewMatrix this method do actual matrix merging
First we will look at Natural satellite model, this model will rotate little of center using modelMatrix.
We will rotate two point, center and shifted. First point will only be used to draw expected rotation, second point will be our actual satellite local position.
Update method will modify total rotation angle, and setup our local model matrix with this rotation.
Our abstract draw method will merge this local matrix to global vie model matrix
Drawing method will calculate center point in global position and draw magenta circle that will represent expected shifted rotation. Radius of this circle will br equal to our shift vector length.
Next step draw actual satellite :
This object will wrap any AstralObject and append view model matrix with it "orbit matrix" before passing it to wrapped astral object.
This is additional data used by Orbit object:
- object - wrapped astral object
- angle / angleSpeed - this is current orbit angle and its angular speed
- shift matrix - this is orbit radius vector (object position for angle 0)
- rotation matrix - matrix created from "angle" it will represent orbit rotation
Shift and Rotation matrix merged together will produce Model Matrix, shift matrix is constant and equal to initial object shift( 0, radius)
First we update angle and then refresh rotation matrix, then we merge rotation with shift and create modelMatrix. Next we will draw orbit, to do this we have to use only parent matrix, to do this we override "draw" method and before executing old implementation that will merge modelMatrix with parentView Matrix we will draw orbit.
Now we will merge matrix and pass it to wrapped astral object.
DrawImpl have merged model and view matrix as a second parameter, we have to only pass this value to wrapped object.
This object have list of Orbits, all it doest is draw itself and pass method drawImpl and update method to orbits.
This object works like planet it holds list of orbits and pass methods to them but it also show "complex" model rotation. We will draw and rotate whole solar system and star model.
First we define our model when rotation angle is 0.
Drawing method is simple
first we apply modelViewMatrix to star model, output is in startPointOut, after that we draw two overlapped triangles using fillPolygon method. This method use two separate array, firs have only "x" values, second only "y". Next we pass drawing to our orbits with modelViewMatrix so orbits will rotate with solar system. If orbit drawing was moved to draw(...) only star would rotate.
update methods is the same as in planets:
Last part is to define our solar system :