Particle Toy

Here is Paper library toy (not a game!) that I managed to put together in my ambled creation of a 2D-game engine. The latest development includes a MotionState class that allows you to simulate physics and add forces and torques to different bodies, a ParticleSystem class that automates the spawning of Paper.Paths, and a VectorField class that can be used to influence the motion of physics-bodies and particles. You can edit the source code and run your own version by clicking the "Source" button at the top right (don't reload the page without saving!). The code is a bit verbose at the moment, but the purpose of the functions and variables should be clear enough. I've moved most of the editable stuff to the top. Feel free to request any features in the comments.

Source


  • Up - ship boost
  • Left/Right - ship rotate
  • Down - ship toggle linear damping
  • w / a / s / d - vector field move
  • [Shift - a/d] - vector field rotate
  • q / e - vector field resize
  • Spacebar - pause the simulation
  • - / = - change the simulation speed
  • [ / ] - change canvas width
  • ; / ' - change canvas height


You can fly the yellow spaceship around with the arrow keys. The motion is driven by forces and torques applied to a MotionState object. When you boost, a stream of particles shoots out the back. Each particle is spawned with a position and velocity (randomly chosen within a specified rectangle) relative to its parent system's motion state (in this case linked to the ship). Notice how the particles inherit the angular velocity of the ship.

The dashed-box in the center represents a vector field. Each vector field is specified by a function of time, position, velocity, rotation, and angular velocity. The function is applied in the field's local-space, which allows us to move and rotate the field around as we please. Indeed in the toy you can move the field around with the 'wasd' keys and spin it with 'shift-ad'. The field above has an attractive $\mathtt{y}$-force, an oscillating attractive $\mathtt{x}$-force (green $\rightarrow$ red), and an oscillating torque. When you fly the ship inside the field a white line emanates in the direction of the force. Explicitly,

$$
F(t,p,v,\theta,\omega) \;\; \left\{ \;
\begin{array}{r c l}
     \texttt{force } & = & -10 \; [ \, p_x \, (1 - \sin t) / 2,  \; p_y \, ]' \\
     \texttt{torque } & = & 10 \, \sin(t/3)  \\
\end{array}
\right.
$$

The code is lengthy because it contains the full-source of the MotionState, VectorField, and ParticleSystem objects. The MotionState class is convenient for simulating separate physics bodies, but it's not tailored for systems. For example, in the previous Paper Gravity Toy, the bodies need to be stepped in unison for accurate motion (not supported). The upcoming collision-toy will also require a more convenient and robust way to apply impulses (rather than just changing the velocities directly). I'm also probably going to change the dependency between the three classes, as well as make them slightly more consistent (esp. for overriding the user-defined functions).

For now I will hold off on supplying instructions for use until it is a bit more polished, at which point I'll write up a full-documentation. Until then, have fun!



No comments:

Post a Comment