1
\$\begingroup\$

I am wanting to write small Particle and ParticleEmitter Classes for a HTML5 Canvas Game (Though, just discussing it in pseudo code is fine)

I am not great, at all, at Physics, and I don't want to use any 3rd party scripts/libraries to do this for me. Is there a simple way to do this? The Game is a top-down 2D Game and here is the sort of thing I'm wanting to achieve:

-

The Particle is the black ball, and I want it to accelerate up and move to the left until it reaches a specified amount of pixels above the origin, stop, and then accelerate down until it stops and disappears completely

Any help would be appreciated, I've been searching for a while now and cannot find anything on this.

Thanks

EDIT: I have implemented a quick mock-up in my game and it works well, but I was just wondering if there was any better, more efficient way to do it (Just because I don't know if this is the proper way, it does not affect performance)

if(_update) {
    _yV += _gravity;
    _y -= _yV;
    _x -= 0.8;

    _diff++;

    if(_diff >= 10 && _loop) {
        _yV *= -0.8;
        _loop = false;
        _diff = 0;
    }else if(_diff >= 20 && !_loop) {
        _update = false;
    }
}
\$\endgroup\$
4
  • 2
    \$\begingroup\$ Are you wanting gravity? or deflection off of a plane? Your image implies deflection which can be as simple as switching the vertical velocity to as advanced as using the plane normal to determine the new direction. I think your best bet would be to study up on some physics for game and simulation programming. \$\endgroup\$ Apr 24, 2013 at 19:44
  • \$\begingroup\$ @UnderscoreZero Gravity would help in the velocity sense, but I do not want deflection. Once a Particle has run it's course as shown in the picture, it shall just be removed from the emitter. \$\endgroup\$
    – oyed
    Apr 24, 2013 at 19:56
  • \$\begingroup\$ By way of inspiration, you could consider Googling "Feynman diagrams". Not strictly relevant to your current proposal, but possibly of interest in terms of seeing how physicists have learned to visualize quantum-mechanical interactions. \$\endgroup\$ Apr 24, 2013 at 22:09
  • \$\begingroup\$ @PieterGeerkens Thank you for directing me to this. I will read up on it over the next few days and see if it could help me out in any way. \$\endgroup\$
    – oyed
    Apr 24, 2013 at 22:28

1 Answer 1

0
\$\begingroup\$

It seems from your picture that you want the path to be straight, which is not what would happen if the ball was subject to gravity. Also from your description, it sounds like you want the ball to come to a sudden stop before accelerating downwards again.

Anyway, from what I understand of your problem, here is how I would solve it. If I have misunderstood something, I apologise.

I would store position (vector), direction (normalised vector) and speed (scalar).

Vector2 position = Vector(0,0);
Vector2 direction = Vector(1,3).unit();
float speed = 0.0;
const float acceleration = 0.1;
const float y_limit = 100.0;

Then, every frame do something like this:

position = position + direction * speed;
speed += acceleration;
if( position.y > y_limit )
{
    position.y = y_limit;
    direction.y = -direction.y;
    speed = 0.0;
}

You will have to insert your own 2D vector class (or one from a library). If you don't understand vectors, just find a good tutorial and learn the very basics, it will only take 5 minutes.

Hope it helps.

\$\endgroup\$

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