[SDL] bugs with objects colliding

hey guys,

I created two rectangle objects, I wanted to achieve random acceleration in random directions at any given point in time, I think I have partially achieved the desired effect I was after but there seems to be quite a few bugs,

I will start with the most glaring of them, when the objects collide from the side the seem to get stuck inside one another and strangely both follow one direction( not that crappy band lol no one should follow them ), I thought my implementation looked pretty sound , another strange thing is when the rectangles collide from the top or bottom the collision (kind of )seems to work. I implemented AABB collision, when a collision is detected I will query which side the rectangle the collision happened, I can't spot where the bugs are in my code, everything looks ok to me.


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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#include <iostream>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <cmath>
#include <stdlib.h>
#include <ctime>

using namespace std;

SDL_Renderer* renderer;
SDL_Window* window;
SDL_Texture* texture;
SDL_Surface* surface;
SDL_Event event;
const int WIDTH = 800;
const int HEIGHT = 600;

class Vector2{

   public:
       double x;
       double y;

       Vector2(double x,double y): x(x),y(y){}
       Vector2(){x = 0;y = 0;}

       void add(Vector2& other){

          x += other.x;
          y += other.y;
       }

       void scale(double scalar){

          x*=scalar;
          y*=scalar;
       }

       double magnitude(){

         return sqrt( (x * x) +  (y * y) );
       }

       Vector2 normalize(){

           double mag = magnitude();
           return Vector2(x/mag,y/mag);
       }

       int dotProduct(Vector2& other){

          return (x * other.x) + (y * other.y);
       }

       friend Vector2 subtractVectors(const Vector2& one,const Vector2& two);

       bool limit(Vector2& position,double magSize){

         Vector2 futurePosition(0,0);
         futurePosition.x = position.x;
         futurePosition.y = position.y;
         futurePosition.add(*this);
         Vector2 distanceVector = subtractVectors(futurePosition,position);

         double mag = distanceVector.magnitude();
         cout << "mag == " << mag << endl;

         if(mag < magSize)
            return false;
         else
            return true;
       }
};

Vector2 subtractVectors(const Vector2& one,const Vector2& two){

   Vector2 difference;
   double x = -(two.x);
   double y = -(two.y);
   difference.x = one.x + x;
   difference.y = one.y + y;
   return difference;
}

class Rect{

  public:
      bool objectLeft = false;
      bool objectRight = false;
      bool objectUp = false;
      bool objectDown = false;
      Vector2 pos;
      Vector2 velocity;
      Vector2 acceleration;
      SDL_Rect spriteOneRect;
      SDL_Surface* spriteOneSurface;
      SDL_Texture* spriteOneTexture;


      Rect(int x,int y,string fileName){

         pos = Vector2(x,y);
         velocity = Vector2(0,0);
         acceleration = Vector2(-0.001,0.01);
         spriteOneSurface = IMG_Load(fileName.c_str());
         spriteOneTexture = SDL_CreateTextureFromSurface(renderer,spriteOneSurface);
         spriteOneRect.x = x;
         spriteOneRect.y = y;
         SDL_QueryTexture(spriteOneTexture,NULL,NULL,&spriteOneRect.w,&spriteOneRect.h);
      }
      Rect(){

         pos = Vector2(250,250);
         velocity = Vector2(0,0);
         acceleration = Vector2(0,0);
      }

      void update(){

        spriteOneRect.x = pos.x;
        spriteOneRect.y = pos.y;
      }
};

void renderRect(Rect& rect,Rect& rect2){


    SDL_RenderClear(renderer);
    SDL_RenderCopy(renderer,rect.spriteOneTexture,NULL,&rect.spriteOneRect);
    SDL_RenderCopy(renderer,rect.spriteOneTexture,NULL,&rect2.spriteOneRect);
    SDL_SetRenderDrawColor(renderer,255,255,255,255);
    SDL_RenderPresent(renderer);

}


bool willObjectsCollide(Rect& rect,Rect& rectTwo,Vector2 futurePos){

          int rightA = rectTwo.pos.x + rectTwo.spriteOneRect.w;
          int rightB = futurePos.x + rect.spriteOneRect.w;
          int leftA = rectTwo.pos.x;
          int leftB = futurePos.x;
          int topA = rectTwo.pos.y;
          int topB = futurePos.y;
          int bottomA = rectTwo.pos.y + rectTwo.spriteOneRect.h;
          int bottomB = futurePos.y + rect.spriteOneRect.h;

          return (rightB >= leftA && leftB <= rightA && topB <= bottomA && bottomB >= topA);
}

void moveObject(Rect& rect,Rect& rectTwo,int magSize){

        Vector2 futurePos(0,0);
        futurePos.x = rect.pos.x;
        futurePos.y = rect.pos.y;
        futurePos.add(rect.velocity);
        bool collisionDected = willObjectsCollide(rect,rectTwo,futurePos);

        if(collisionDected){

          int rightA = rectTwo.pos.x + rectTwo.spriteOneRect.w;
          int rightB = futurePos.x + rect.spriteOneRect.w;
          int leftA = rectTwo.pos.x;
          int leftB = futurePos.x;
          int topA = rectTwo.pos.y;
          int topB = futurePos.y;
          int bottomA = rectTwo.pos.y + rectTwo.spriteOneRect.h;
          int bottomB = futurePos.y + rect.spriteOneRect.h;

          if(topB <= bottomA || bottomB >= topA)
            rect.velocity.y = rect.velocity.y * -1;
          else
            rect.velocity.x = rect.velocity.x * -1;
        }

        rect.pos.add(rect.velocity);
        rect.update();

        if(rect.pos.x + rect.spriteOneRect.w > WIDTH || rect.pos.x < 0)
            rect.velocity.x = rect.velocity.x * -1;
        if(rect.pos.y + rect.spriteOneRect.h > HEIGHT || rect.pos.y < 0)
            rect.velocity.y = rect.velocity.y * -1;


        double randomX = (double)(1+(rand()%40));
        double randomY = (double)(1+(rand()%40));
        int randomScale = 2+rand()%4;
        randomX -= 20;
        randomY -= 20;
        randomX *= 0.00025;
        randomY *= 0.0025;

        rect.acceleration.x = randomX;
        rect.acceleration.y = randomY;

        if(!rect.velocity.limit(rect.pos,magSize))
            rect.velocity.add(rect.acceleration);

        rect.acceleration.scale(randomScale);
        Vector2 oldPos(0,0);
        oldPos.x = rect.pos.x;
        oldPos.y = rect.pos.y;
        //rect.pos.add(rect.velocity);
        rect.update();
}

int SDL_main(int argc,char* argv[]){

   init();
   bool quit = false;
   srand(time(NULL));

   Rect br(50,100,"blockone.png");
   Rect br2(400,400,"blockone.png");


   //Vector2 acceleration(randomX,randomY);


   while(!quit){

      SDL_PollEvent(&event);

      if(event.type == SDL_QUIT)
        break;

       if(event.type == SDL_KEYDOWN){

          int key = event.key.keysym.sym;
          if(key == SDLK_b)
            cout << "yeah" << endl;

       }
      moveObject(br,br2,8);
      moveObject(br2,br,8);
      renderRect(br,br2);
      SDL_Delay(2);

   }
}



video of what is happening - https://youtu.be/VOEFSiaby5I

also seems to get stuck at the bottom edge sometimes - https://youtu.be/TZWlGWJY7TY
Last edited on
It's because the objects are overlapping too far into each other before the collision is detected, so once it is detected they "tangle" until they can drift apart.

There's multiple ways of solving this. One way would be to make the collision detection stateful. Once two objects are overlapping and the collision has been detected, the collision detection function should not return true for those two objects until they have stopped overlapping.
oh ok I think I understand so when a collision is detected they are already overlapping so the colliding rectangle changes direction but when it changes direction it doesn't change move far enough and the collision detection again is set off, so again the rectangle changes direction and the process repeats until they separate.

first fix seemed to have failed but I'll try another fix

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
bool alreadyColliding = false;

bool willObjectsCollide(Rect& rect,Rect& rectTwo,Vector2 futurePos){

          int rightA = rectTwo.pos.x + rectTwo.spriteOneRect.w;
          int rightB = futurePos.x + rect.spriteOneRect.w;
          int leftA = rectTwo.pos.x;
          int leftB = futurePos.x;
          int topA = rectTwo.pos.y;
          int topB = futurePos.y;
          int bottomA = rectTwo.pos.y + rectTwo.spriteOneRect.h;
          int bottomB = futurePos.y + rect.spriteOneRect.h;

          if(rightB >= leftA && leftB <= rightA && topB <= bottomA && bottomB >= topA && !alreadyColliding){

             alreadyColliding = true;
             return true;
          }else
            return false;
}

bool objectsColliding(Rect& one, Rect& two){

    if(one.spriteOneRect.y >= two.spriteOneRect.y + two.spriteOneRect.h)
        return false;
    if(one.spriteOneRect.x >= two.spriteOneRect.x + two.spriteOneRect.w)
        return false;
    if(one.spriteOneRect.y + one.spriteOneRect.h <= two.spriteOneRect.y)
        return false;
    if(one.spriteOneRect.x + one.spriteOneRect.w <= two.spriteOneRect.x)
        return false;

    cout << "returning true :: collision" << endl;
    return true;
}

void setRandomAcceleration(Rect& rect){

        double randomX = (double)(1+(rand()%40));
        double randomY = (double)(1+(rand()%40));
        randomX -= 20;
        randomY -= 20;
        randomX *= 0.00025;
        randomY *= 0.0025;

        rect.acceleration.x = randomX;
        rect.acceleration.y = randomY;
}

void moveObject(Rect& rect,Rect& rectTwo,int magSize){

        Vector2 futurePos(0,0);
        futurePos.x = rect.pos.x;
        futurePos.y = rect.pos.y;
        futurePos.add(rect.velocity);
        bool collisionDected = willObjectsCollide(rect,rectTwo,futurePos);

        if(collisionDected && !alreadyColliding){

          int rightA = rectTwo.pos.x + rectTwo.spriteOneRect.w;
          int rightB = futurePos.x + rect.spriteOneRect.w;
          int leftA = rectTwo.pos.x;
          int leftB = futurePos.x;
          int topA = rectTwo.pos.y;
          int topB = futurePos.y;
          int bottomA = rectTwo.pos.y + rectTwo.spriteOneRect.h;
          int bottomB = futurePos.y + rect.spriteOneRect.h;

          if(topB <= bottomA || bottomB >= topA)
            rect.velocity.y = rect.velocity.y * -1;
          else
            rect.velocity.x = rect.velocity.x * -1;
        }

        if(!objectsColliding(rect,rectTwo))
            alreadyColliding = false;

        rect.pos.add(rect.velocity);
        rect.update();

        if(rect.pos.x + rect.spriteOneRect.w > WIDTH || rect.pos.x < 0)
            rect.velocity.x = rect.velocity.x * -1;
        if(rect.pos.y + rect.spriteOneRect.h > HEIGHT || rect.pos.y < 0)
            rect.velocity.y = rect.velocity.y * -1;

         setRandomAcceleration(rect);

        if(!rect.velocity.limit(rect.pos,magSize))
            rect.velocity.add(rect.acceleration);

        int randomScale = 2+rand()%4;
        rect.acceleration.scale(randomScale);
        //rect.pos.add(rect.velocity);
        rect.update();
}
still seems to have the same problem, I decided to give each rect a boolean variable to indicate if the rects are already colliding instead of sharing a global variable,

the positive - the collision detection seems to be solid for collisions on the top and bottom of each rect BUT still when the objects collide from the sides they still meet with one another

and I also changed willObjectsCollide to objectsCollision this will at least change both rects directions instead of one

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

class Rect{

  public:
      bool objectLeft = false;
      bool objectRight = false;
      bool objectUp = false;
      bool objectDown = false;
      bool alreadyColliding = false;
      Vector2 pos;
      Vector2 velocity;
      Vector2 acceleration;
      SDL_Rect spriteOneRect;
      SDL_Surface* spriteOneSurface;
      SDL_Texture* spriteOneTexture;


      Rect(int x,int y,string fileName){

         pos = Vector2(x,y);
         velocity = Vector2(0,0);
         acceleration = Vector2(-0.001,0.01);
         spriteOneSurface = IMG_Load(fileName.c_str());
         spriteOneTexture = SDL_CreateTextureFromSurface(renderer,spriteOneSurface);
         spriteOneRect.x = x;
         spriteOneRect.y = y;
         SDL_QueryTexture(spriteOneTexture,NULL,NULL,&spriteOneRect.w,&spriteOneRect.h);
      }
      Rect(){

         pos = Vector2(250,250);
         velocity = Vector2(0,0);
         acceleration = Vector2(0,0);
      }

      void update(){

        spriteOneRect.x = pos.x;
        spriteOneRect.y = pos.y;
      }
};


void renderRect(Rect& rect,Rect& rect2){


    SDL_RenderClear(renderer);
    SDL_RenderCopy(renderer,rect.spriteOneTexture,NULL,&rect.spriteOneRect);
    SDL_RenderCopy(renderer,rect.spriteOneTexture,NULL,&rect2.spriteOneRect);
    SDL_SetRenderDrawColor(renderer,255,255,255,255);
    SDL_RenderPresent(renderer);

}


bool objectsCollision(Rect& rect,Rect& rectTwo){

          int rightA = rectTwo.pos.x + rectTwo.spriteOneRect.w;
          int rightB = rect.pos.x + rect.spriteOneRect.w;
          int leftA = rectTwo.pos.x;
          int leftB = rect.pos.x;
          int topA = rectTwo.pos.y;
          int topB = rect.pos.y;
          int bottomA = rectTwo.pos.y + rectTwo.spriteOneRect.h;
          int bottomB = rect.pos.y + rect.spriteOneRect.h;

          return (rightB >= leftA && leftB <= rightA && topB <= bottomA && bottomB >= topA);
}



bool objectsColliding(Rect& one, Rect& two){

    if(one.spriteOneRect.y >= two.spriteOneRect.y + two.spriteOneRect.h)
        return false;
    if(one.spriteOneRect.x >= two.spriteOneRect.x + two.spriteOneRect.w)
        return false;
    if(one.spriteOneRect.y + one.spriteOneRect.h <= two.spriteOneRect.y)
        return false;
    if(one.spriteOneRect.x + one.spriteOneRect.w <= two.spriteOneRect.x)
        return false;

    cout << "returning true :: collision" << endl;
    return true;
}

void setRandomAcceleration(Rect& rect){

        double randomX = (double)(1+(rand()%40));
        double randomY = (double)(1+(rand()%40));
        randomX -= 20;
        randomY -= 20;
        randomX *= 0.00025;
        randomY *= 0.0025;

        rect.acceleration.x = randomX;
        rect.acceleration.y = randomY;
}

void moveObject(Rect& rect,Rect& rectTwo,int magSize){

        Vector2 futurePos(0,0);
        futurePos.x = rect.pos.x;
        futurePos.y = rect.pos.y;
        futurePos.add(rect.velocity);
        bool collisionDetected = objectsCollision(rect,rectTwo);

        if(collisionDetected && !rect.alreadyColliding){

          cout << "COLLLLLLLLLIDE" << endl;
          rect.alreadyColliding = true;

          int rightA = rectTwo.pos.x + rectTwo.spriteOneRect.w;
          int rightB = rect.pos.x + rect.spriteOneRect.w;
          int leftA = rectTwo.pos.x;
          int leftB = rect.pos.x;
          int topA = rectTwo.pos.y;
          int topB = rect.pos.y;
          int bottomA = rectTwo.pos.y + rectTwo.spriteOneRect.h;
          int bottomB = rect.pos.y + rect.spriteOneRect.h;

          if(topB <= bottomA || bottomB >= topA)
            rect.velocity.y = rect.velocity.y * -1;
          else
            rect.velocity.x = rect.velocity.x * -1;
        }

        if(!objectsColliding(rectTwo,rect)){
            rect.alreadyColliding = false;
            cout << "not colliding" << endl;
        }

        rect.pos.add(rect.velocity);
        rect.update();

        if(rect.pos.x + rect.spriteOneRect.w > WIDTH || rect.pos.x < 0)
            rect.velocity.x = rect.velocity.x * -1;
        if(rect.pos.y + rect.spriteOneRect.h > HEIGHT || rect.pos.y < 0)
            rect.velocity.y = rect.velocity.y * -1;

         setRandomAcceleration(rect);

        if(!rect.velocity.limit(rect.pos,magSize))
            rect.velocity.add(rect.acceleration);

        int randomScale = 2+rand()%4;
        rect.acceleration.scale(randomScale);
        //rect.pos.add(rect.velocity);
        rect.update();
}


**edit - after debugging I've also come to a hypothesis -

1
2
3
4
5
6

if(topB <= bottomA || bottomB >= topA)
            rect.velocity.y = rect.velocity.y * -1;
          else
            rect.velocity.x = rect.velocity.x * -1;


the else statement no matter what never seems to get executed, after careful review I believe that if statement is always going to get executed no matter what, so I need to find a better way of determining which side a collision is detected.

it's strange because I followed - https://gamedev.stackexchange.com/questions/153763/detecting-on-which-side-of-the-box-a-collision-occurred

the top answer on that post...
Last edited on
Another possibility is, once the collision has been detected, to instantly move the objects in the new direction until they're no longer colliding.
very good idea,

I did manage to fix the problem ( kind of, it's still a little glitchy) but I added this snippet of code in move object when collisions are detected

1
2
3
4
5
6
7
8
9
10
11
12
13

if(rect.pos.y <= rectTwo.pos.y - (rectTwo.spriteOneRect.h/2))
            rect.velocity.y = rect.velocity.y * -1;

         if(rect.pos.y >= rectTwo.pos.y + (rectTwo.spriteOneRect.h/2))
            rect.velocity.y = rect.velocity.y * -1;

          if(rect.pos.x < rectTwo.pos.x)
            rect.velocity.x = rect.velocity.x * -1;

           if(rect.pos.x > rectTwo.pos.x)
              rect.velocity.x = rect.velocity.x * -1;


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

void moveObject(Rect& rect,Rect& rectTwo,int magSize){

        Vector2 futurePos(0,0);
        futurePos.x = rect.pos.x;
        futurePos.y = rect.pos.y;
        futurePos.add(rect.velocity);
        bool collisionDetected = objectsCollision(rect,rectTwo);

        if(collisionDetected && !rect.alreadyColliding){

          cout << "COLLLLLLLLLIDE" << endl;
          rect.alreadyColliding = true;

          int rightA = rectTwo.pos.x + rectTwo.spriteOneRect.w;
          int rightB = rect.pos.x + rect.spriteOneRect.w;
          int leftA = rectTwo.pos.x;
          int leftB = rect.pos.x;
          int topA = rectTwo.pos.y;
          int topB = rect.pos.y;
          int bottomA = rectTwo.pos.y + rectTwo.spriteOneRect.h;
          int bottomB = rect.pos.y + rect.spriteOneRect.h;

          if(rect.pos.y <= rectTwo.pos.y - (rectTwo.spriteOneRect.h/2))
            rect.velocity.y = rect.velocity.y * -1;

         if(rect.pos.y >= rectTwo.pos.y + (rectTwo.spriteOneRect.h/2))
            rect.velocity.y = rect.velocity.y * -1;

          if(rect.pos.x < rectTwo.pos.x)
            rect.velocity.x = rect.velocity.x * -1;

           if(rect.pos.x > rectTwo.pos.x)
              rect.velocity.x = rect.velocity.x * -1;

        }

        if(!objectsColliding(rectTwo,rect)){
            rect.alreadyColliding = false;
            cout << "not colliding" << endl;
        }

        rect.pos.add(rect.velocity);
        rect.update();

        if(rect.pos.x + rect.spriteOneRect.w > WIDTH || rect.pos.x < 0)
            rect.velocity.x = rect.velocity.x * -1;
        if(rect.pos.y + rect.spriteOneRect.h > HEIGHT || rect.pos.y < 0)
            rect.velocity.y = rect.velocity.y * -1;

         setRandomAcceleration(rect);

        if(!rect.velocity.limit(rect.pos,magSize))
            rect.velocity.add(rect.acceleration);

        int randomScale = 2+rand()%4;
        rect.acceleration.scale(randomScale);
        //rect.pos.add(rect.velocity);
        rect.update();
}


I got it from the first answer on this post - https://gamedev.stackexchange.com/questions/22609/breakout-collision-detecting-the-side-of-collision

but I honestly don't know how the math even works and how it detects if it hots the right,left,top or bottom sides :/ I don't like to just copy and paste code and hope for the best (glue code), I like to understand how it actually works, if you could explain would be awesome :)

also the second answer mentions you could so some vector math using angles which would accomplish the same result, which would be more efficient in your opinion?
Last edited on
That attempted fix appeared to work on the surface but when the program runs for long enough many bugs still persist,

such as, the movement still looks quite unrealistic when collisions occur. Sometimes a rectangle will get "stuck" at the bottom or top of the window and even sometimes the rectangles will still go through one another.

short demonstration - https://youtu.be/wLVJX01-dF8
I love it. Background music of choice for that footage: https://www.youtube.com/watch?v=ZnHmskwqCCQ

Keep working at it. You'll figure it out.
yeah that would fit rather perfectly at the moment

so my "failed" solution, although I think I'm on the right path... please tell me if I'm way off.

The problem : when rectangle A collides with one of the edges of the window it rebounds, but when this happens and rectangle B is too close to A they will keep colliding in rapid succession, A will collide with the wall and change direction, B will collide with A and change direction, A will collide with B and change direction.

The solution is when A is within a certain magnitude/distance from the edge of the wall and if B collides with A, B should only hit A once and immediately change direction no matter the circumstance. To do this when A hits the edge I make a check to see if the magnitude/distance between A and B is a certain fixed length and if it is, B should only hit A once.

below is what I changed it too, but with mixed results, although the collisions have improved somewhat, the rectangles will still go through one another from time to time. Which leaves me wondering.. is the magnitude too big or small?

thanks

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
double distanceFromEachOther(Rect& rect,Rect& rectTwo){

    Vector2 distance = subtractVectors(rect.pos,rectTwo.pos);

    return distance.magnitude();
}

void moveObject(Rect& rect,Rect& rectTwo,int magSize){

        rect.update();
        rect.pos.add(rect.velocity);
        bool collisionDetected = objectsCollision(rect,rectTwo);

        if(collisionDetected && !rect.alreadyColliding && !rectTwo.hitOnce){

          cout << "debug :: collide  " << endl;
          rect.alreadyColliding = true;

          int rightA = rectTwo.pos.x + rectTwo.spriteOneRect.w;
          int rightB = rect.pos.x + rect.spriteOneRect.w;
          int leftA = rectTwo.pos.x;
          int leftB = rect.pos.x;
          int topA = rectTwo.pos.y;
          int topB = rect.pos.y;
          int bottomA = rectTwo.pos.y + rectTwo.spriteOneRect.h;
          int bottomB = rect.pos.y + rect.spriteOneRect.h;

          if(rectTwo.pos.y <= rect.pos.y - (rect.spriteOneRect.h/2))
            rect.velocity.y = rect.velocity.y * -1;

         if(rectTwo.pos.y >= rect.pos.y + (rect.spriteOneRect.h/2))
            rect.velocity.y = rect.velocity.y * -1;

          if(rectTwo.pos.x < rect.pos.x)
            rect.velocity.x = rect.velocity.x * -1;

           if(rectTwo.pos.x > rect.pos.x)
              rect.velocity.x = rect.velocity.x * -1;
        }

        if(!objectsColliding(rectTwo,rect)){
            rect.alreadyColliding = false;

            if(rectTwo.hitOnce && distanceFromEachOther(rect,rectTwo) >= 4)
                rectTwo.hitOnce = false;
        }

        if(rect.pos.x + rect.spriteOneRect.w > WIDTH || rect.pos.x < 0)
            rect.velocity.x = rect.velocity.x * -1;

        if(rect.pos.y + rect.spriteOneRect.h > HEIGHT || rect.pos.y < 0){

            rect.velocity.y = rect.velocity.y * -1;
            if(distanceFromEachOther(rect,rectTwo) < 4)
                rectTwo.hitOnce = true;
        }

         setRandomAcceleration(rect);

        if(!rect.velocity.limit(rect.pos,magSize))
            rect.velocity.add(rect.acceleration);

        int randomScale = 2+rand()%4;
        rect.acceleration.scale(randomScale);
}


*note I will need to modularise moveObject,it's beyond too long and messy
Last edited on
do you think this is a viable solution?
Topic archived. No new replies allowed.