I have these ES components:
I have the ModelState, which works with Model and Position and adds the Spatial and the dyn4j body.
The Spatial has a control that updates its position in order to follow the body.
I have some methods for moving and updating the Position of the entity.
When the Position is changed, I update the Body position so that also the Spatial position is updated.
Question: all of this is a bit convoluted or is the standard way for doing this?
And why have a control on the spatial if you can get the position from the physical body ?
Sounds like you are mixing view with model. ModelState should only care about position and model, and update the spatial. The Dyn4j body lives in a PhysicsState that updates the Position.
Sounds like it’s reversed. The Physics should be feeding the Positions.
Normally you have ViewSytem which is solely interested in entities which have model and position/orientation, this system creates/removes/updates only the visuals.
Then you have PhysicSystem which cares for the physic stuff and updates the positions of your physical models.
User inputs are forces which the physicSystem has to apply to player whatever.
But as @asser_fahrenholz already mentioned above you seem to mix it somehow.
I create a Hitbox with a Position. The hitbox is added and then writes the position, so that the view can update the node and the visuals.
So that “Position” component is not really a “game information”, but simply a way to convey the information from Physics to the scenegraph.
But I might as well discard the “Position” component altogether and do with
I admit that my design has a bit of overlap and there’s not a clear separation of responsibilities…
Well, Dyn4j is, like other physics libaries, made to do all the heavy lifting on positions, collisions, rotations etc. Feed your input to Dyn4j, then have that do a world-update, extract positions/rotations from Dyn4j-bodies to your models and thus the view.
You mean to say your ‘Hitbox’ component also includes position information ? There’s no reason to have positional information twice. But I’m not sure what you use ‘hitbox’ for.
It is used on creation of the body, and contains width and height
Yes, but then Position is something entirely different.
As I see it you have the follow flow of data:
- Create Hitbox + Model + Position component
- Physics State creates Dyn4j body that is mapped to that entity, Model State creates model at said Position
- Input forces or logic to Physics State (as velocity or forces or what not)
- Physics State (Dyn4j) calculates new Position + Rotation in world.update()
- Extract new position + rotation onto entity
- Update view / model state with new position + rotation - go to #3
Yes, as stated above, physics state sets the Position component.
Anything wanting to deal with objects with Positions will then see updates to that component. This might be a model state that updates 3D visuals, it might be a map state that updates markers + labels on a map, etc…
This is also the way all of my examples work.