0
\$\begingroup\$

I'm currently working on a particle simulation program that makes pixels move towards a mouse point continually accelerating, but i'm running into some math issues. Currently my particle will accelerate in the direction of the mouse point, but will not actually accelerate towards a given point(only in it's direction).

To get an idea of what I'm trying to accomplish, my older program does just this but uses the fixed function pipeline. I want to recreate this program using the programmable pipeline and as a result changed around a lot of the program, there is really no similar code between the two at this point. Example of my problem:

enter image description here

As you can see the particle only accelerate in the direction of the cursor, my main equation I'm using is:

//if left mouse button is pressed
if(sf::Mouse::isButtonPressed(sf::Mouse::Left)){
        p.speed += glm::vec3(-pos.x * 30, -pos.y * 30 ,0.0f);   //increase speed in direction
}
//update position of particle with a little bit of spread -- speed(dt) * DRAG * Rand
p.pos += p.speed * (float)(delta) * DRAG * glm::vec3(rand()%5,rand()%5,0);

where my particle struct is:

struct Particle{
    glm::vec3 pos, speed;       //position, speed
    unsigned char r,g,b,a;      //color
    float size, angle, weight;  //?
    float life;                 //remaining life of a particle, if <0 it's super dead
    float cameradistance;       //squared distance to camera : -1.0f if dead

    //used for std::sort, needs an overloaded comparison operator
    bool operator<(const Particle& that) const {
        return this->cameradistance > that.cameradistance;
    }
};

how would I go about simulating the behavior of my first program with this second program I'm writing? I can't seem to wrap my head around doing this as the program structure is so drastically different.

\$\endgroup\$
  • \$\begingroup\$ Currently my particle will accelerate in the direction of the mouse point, but will not actually accelerate towards a given point(only in it's direction). I don't really understand your problem can you please clarify it? \$\endgroup\$ – concept3d May 20 '14 at 7:50
  • 1
    \$\begingroup\$ @concept3d I think he means that the particles don't stop when they reach the point where the mouse cursor is located, but keep moving (and accelerating) beyond that point. \$\endgroup\$ – user8363 May 20 '14 at 12:20
2
\$\begingroup\$

Well if I understand well as @user8363 explained in the comments, your problem is that you are making one direction for all the particles, which makes the particles move in that direction. If you want the particles to accelerate toward the point you need to make a direction vector for each particle. For instance:

foreach particle:
    acc = particle - mousePosition; // assuming mousePosition is actually a vec3.

Then updated each particle accordingly.

\$\endgroup\$
  • \$\begingroup\$ well the location of my equation is in a for loop which loops through and updates all particles positions according to the mouse point and if it was clicked. I'll update the post and make it a bounty(not eligible till tomorrow whoops). \$\endgroup\$ – Need4Sleep May 21 '14 at 0:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.