0
\$\begingroup\$

I'm developing a top-down shooter and I currently have a PhysicsSystem which operates on TransformComponents. Basically, it update each component's position based on the component's velocity, etc.

However, this only allows for one type of movement. I would like to extend this by, for example, having a projectile shoot out of a weapon and then arc in a semi-circle.

My initial solution to this was to just have a SemiCircleProjectile component along with a SemiCircleMovementSystem, but this did not feel generic enough. For one, it updated positions` directly rather than velocities, and also, I would have to create a new system and new component (and duplicate a lot of code) for every single different "equation" of motion.

Rather, I was wondering if there was a way to have a generic component such as ParametricMovement, and be able to supply an equation to it, and then have a ParametricMovementSystem be able to update that entity's position based on the equation of motion supplied.

That is, the only thing I would have to do would be to supply the equation to the component and then the generic system would process it all the same. In addition, (and I'm not sure if this is possible), I would be able to have an array of motion-modifying functions, and they would all interact and add in some way, so as to have the ability to have multiple movement-modifiers at once (for example: squiggle shot and arc-shot to make a projectile move in an arc and squiggle along the way).

My question is two-fold. One, does this sound like a good idea? And two, how might something like this be implemented? That is, should the functions set the entity's position directly? Or maybe only add to the velocity? And what sorts of parameters should this "movement equation function" take? Math is not really my strong suit. Additionally, with the "arc" example, would I be using the equation of a semi-circle: (something like x^2 + y^2 = r^2; x >= 0) or should I be manually calculating the angles based on sin and cos? Should the component save the previous iteration's velocity to interpolate upon it, or recalculate it every time?

Basically, I'm not the greatest at math and I'm not exactly sure how to design a system like this. An example of how to develop something like this would be much appreciated.

\$\endgroup\$
1
\$\begingroup\$

You don't really need physics. Here is a top down shooter example that you can learn from with code that you can also re-use. https://www.tabageos.com/examples/topDown360Shooter/ It's tile based. For arcs one would utilize geometry.

In that example the enemies are somewhat set up to be doing what you describe, for example at the bottom of the code, in the code for each enemy there is:

en.separate(enemies);
en.chase(this.player);
en.update();

You could change .chase to .circle and they would circle the player, you could change it to .wander() and they would wander. They also have the .followPath method which takes a path of points to follow. In the tabageos library the GeometricMath Class has the methods you would want for making paths, for example getHermiteCurvePath, getArcCurvePath etc.. You could use those methods and give the resulting path to the followPath method of the enemies, and have them follow whatever path.

The update method is updating velocity and the position is updated based on velocity.

The core library Classes you would want to see are GeometricMath and TravelerSkeleton, they are the base of how the enemies are moving in that example.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .