attraction between objects

i am relatively new to c++, and am trying to make it so particles will gravitationally attract to the mouse pointer. i have gotten all the bugs that the compiler found, but there must be another because the program crashes the second i open it. my code could be way off, so just tell me if it is and ill do more research and learning before trying something like this again. here is the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include <SDL/SDL.h>
#include <iostream>
#include <vector>
#include <math.h>
#include <stdio.h>

using namespace std;

float mouseX;
float mouseY;

class focusObject
{
public:
    int x, y;
    float mass;
    focusObject(int X, int Y, float Mass);
    void move();
};

focusObject::focusObject(int X, int Y, float Mass)
{
    x = X;
    y = Y;
    mass = Mass;
}

void focusObject::move()
{
    x = mouseX;
    y = mouseY;
}

class particle
{
public:
    Uint32 color;
    int x, y, xvel, yvel;
    float mass, xacc, yacc, distance, theta;
    particle(int X, int Y, int Xvel, int Yvel, float Mass, Uint32 Color);
    void force(focusObject a);
    void move();
    void show();
};

particle::particle(int X, int Y, int Xvel, int Yvel, float Mass, Uint32 Color)
{
    x = X;
    y = Y;
    xvel = Xvel;
    yvel = Yvel;
    mass = Mass;
    color = Color;
}

void particle::force(focusObject a)
{
    theta = atan((a.x - x)/(a.y-y));
    distance = sqrt((a.x - x)*(a.x - x) + (a.y - y)*(a.y - y));
    xacc = ((a.mass * mass)/(distance*distance))*sin(theta);
    yacc = ((a.mass * mass)/(distance*distance))*cos(theta);
}

void particle::move()
{
    xvel += xacc;
    yvel += yacc;
    x += xvel;
    y += yvel;
}

void particle::show()
{
    Uint8* pixels=(Uint8*)SDL_GetVideoSurface()->pixels;
    Uint8* pixel=pixels+(int)y*SDL_GetVideoSurface()->pitch+(int)x;
    *pixel=color;
}

void clearScreen()
{
    SDL_FillRect(SDL_GetVideoSurface(), &SDL_GetVideoSurface()->clip_rect, SDL_MapRGB(SDL_GetVideoSurface()->format, 0, 0, 0));
}

int main(int argc, char** argv)
{
    int screenHeight = 480;
    int screenWidth = 640;
    SDL_Init(SDL_INIT_EVERYTHING);
    SDL_Surface* screen;
    screen = SDL_SetVideoMode(screenWidth, screenHeight, 8, SDL_SWSURFACE);
    Uint32 start;
    const int FPS = 30;
    bool running = true;
    int maxparticles = 5000;
    focusObject Luna(320, 240, 20);
    vector<particle*> particles;
    for(int i = 0; i < maxparticles; i++)
        particles.push_back(new particle(rand()%screenWidth, rand()%screenHeight, 0, 0, 1, SDL_MapRGB(screen->format, rand()%255, rand()%255, rand()%255)));




    while(running)
    {
        start = SDL_GetTicks();
        SDL_Event event;
        while(SDL_PollEvent(&event))
        {
            switch(event.type)
            {
            case SDL_QUIT:
                running = false;
                break;
            case SDL_MOUSEMOTION:
                mouseX = event.motion.x;
                mouseY = event.motion.y;
            }
        }
        Luna.move();
        clearScreen();
        for(int i = 0; i < maxparticles; i++)
        {
            particles[i]->force(Luna);
            particles[i]->move();
            particles[i]->show();
        }

        SDL_Flip(screen);

        if(1000/FPS>SDL_GetTicks()-start)
                SDL_Delay(1000/FPS-(SDL_GetTicks()-start));
    }
    SDL_Quit();
    return 0;
}
Last edited on
Shouldn't line 58 be using atan?
I'm not particularly experienced in SDL but it seems to be crashing in the particle::show() method.
Can't believe I didn't see it earlier, line 59 distance = sqrt((a.x - x)^2 + (a.y - y)^2); the ^ is an XOR operation in c++ not an exponent.
i fixed that, but it still crashes within .05 seconds of it running.
I've been playing with it a little, this still crashes if the mouse cursor gets too close to the window edge.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include <SDL.h>
#include <iostream>
#include <vector>
#include <math.h>
#include <stdio.h>

using namespace std;

float mouseX;
float mouseY;

class focusObject
{
public:
    int x, y;
    float mass;
    focusObject(int X, int Y, float Mass);
    void move();
};

focusObject::focusObject(int X, int Y, float Mass)
{
    x = X;
    y = Y;
    mass = Mass;
}

void focusObject::move()
{
    x = mouseX;
    y = mouseY;
}

class particle
{
public:
    Uint32 color;
    int x, y, xvel, yvel;
    float mass, xacc, yacc, distance, theta;
    particle(int X, int Y, int Xvel, int Yvel, float Mass, Uint32 Color);
    void force(focusObject a);
    void move();
    void show(SDL_Surface* s);
    Uint8* pixels;
};

particle::particle(int X, int Y, int Xvel, int Yvel, float Mass, Uint32 Color)
{
    x = X;
    y = Y;
    xacc = yacc = 0;
    xvel = Xvel;
    yvel = Yvel;
    mass = Mass;
    color = Color;
    pixels=(Uint8*)SDL_GetVideoSurface()->pixels;
  //  pitch = SDL_GetVideoSurface()->pitch;
}

void particle::force(focusObject a)
{
    if((a.y - y) != 0 && (a.x-x) != 0)
    {
        theta = atan2((a.y - y),(a.x-x));
        distance = hypot((a.x - x), (a.y - y));
        if(distance)
        {
            yacc = ((a.mass * mass)/(distance*distance))*sin(theta);// ((a.mass + mass)/(distance))*sin(theta);
            xacc = ((a.mass * mass)/(distance*distance))*cos(theta);//((a.mass + mass)/(distance))*cos(theta);
        }
    }
}

void particle::move()
{
    xvel += xacc;
    yvel += yacc;
    x += xvel;
    y += yvel;
}

void particle::show(SDL_Surface* s)
{
    //Uint8* pixels=(Uint8*)SDL_GetVideoSurface()->pixels;
    Uint8* pixel = nullptr;
    if(x >= 0 && y >= 0)
    {
        pixel = (Uint8*)s->pixels + y*s->pitch + x;
        if(pixel)
            *pixel=color;
    }
}

void clearScreen()
{
    SDL_FillRect(SDL_GetVideoSurface(), &SDL_GetVideoSurface()->clip_rect, SDL_MapRGB(SDL_GetVideoSurface()->format, 0, 0, 0));
}

int main(int argc, char** argv)
{
    int screenHeight = 480;
    int screenWidth = 640;
    SDL_Init(SDL_INIT_EVERYTHING);
    SDL_Surface* screen;
    screen = SDL_SetVideoMode(screenWidth, screenHeight, 8, SDL_SWSURFACE);
    Uint32 start;
    const int FPS = 30;
    bool running = true;
    int maxparticles = 5000;
    focusObject Luna(320, 240, 20);
    vector<particle*> particles;
    for(int i = 0; i < maxparticles; i++)
        particles.push_back(new particle((rand()%screenWidth) - 5, (rand()%screenHeight) - 5,
            0, 0, 1, SDL_MapRGB(screen->format, rand()%255, rand()%255, rand()%255)));




    while(running)
    {
        start = SDL_GetTicks();
        SDL_Event event;
        while(SDL_PollEvent(&event))
        {
            switch(event.type)
            {
            case SDL_QUIT:
                running = false;
                break;
            case SDL_MOUSEMOTION:
                mouseX = event.motion.x;
                mouseY = event.motion.y;
            }
        }
        Luna.move();
        clearScreen();
        
        for(int i = 0; i < maxparticles; ++i)
        {
            particles[i]->force(Luna);
            particles[i]->move();
            SDL_LockSurface(screen);
            particles[i]->show(screen);
            SDL_UnlockSurface(screen);
        }
       
        SDL_Flip(screen);

        if(1000/FPS>SDL_GetTicks()-start)
                SDL_Delay(1000/FPS-(SDL_GetTicks()-start));
    }
    SDL_Quit();
    return 0;
}
i changed the xvel and yvel variables to floats as well, the particles move now but it crashes soon after again. i need to check to make sure my math goes into the equations correctly.
Topic archived. No new replies allowed.