Hello! I'm working on this hobby little 2D engine in JavaScript because I want to learn how all this things are implemented and architectured in general. Internet is full of resources but most things are mathematical or theoretical stuff, there is very little about implementation or architecture for small 2D engines like the one I'm working in. So, this is my context and at the end are some questions I hope you can help me with:
I have a Body object that has different properties like (pseudo-code):
Body {
angle: float
scale: float
acceleration: 2d vector
velocity: 2d vector
position: 2d vector
update: function()
}
A Body object also has a Model object, which has (pseudo-code):
Model {
points: Array of [x, y] points
fillColor: string (hex)
transform: function(rotate, scale, translate)
}
The transform function receives Angle, Scale and Translate, and returns a new transformed instance of the model.
On every frame I will update the Body object to represent the current state: adding accelerator vector to velocity, velocity to location (and some similar stuff to update the angle and scale if required):
Body {
update() {
this.velocity.add(this.acceleration)
this.location.add(this.velocity)
this.acceleration.reset()
}
}
Once the Body object is updated I must do a world-transform of the model so that I can start doing physics related stuff (like collision detection/resolving, interaction forces between bodies, etc). After that I will do a view-transform, to transform the world-objects into the "perspective" of some other object.
At the moment this is my solution is based in keeing three instances of the Model object withing the Body object, like this:
Body {
model: the original *Model* with no transformations
model_world: result of *Model.transform(body.angle, body.scale, body.location)*
model_view: the result of transforming **model_world** instance with viewport properties
}
Questions:
- Do you think there is a better way to handle model transformations? Like maybe one the do not require the Body object to mantain the three instances of the three transformations itself, perhaps.
- Can you please give me some suggestions on how to better organize this functionality for a simple 2D engine?
Thank you very much in advance!