SDL, understanding how to push back ar rect object to a vector outside class

So Im trying to get a rect to constantly update its coordinates in the right index of a vector, this way I hope to check the vector for collisions, in the mean time, how can I push back the data?

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
 std::vector<SDL_Rect > collisionboxes;

class bunny{

public:


int start_pos_x,start_pos_y,health,stillclips = 12,upclips=0,downclips=3,leftclips=9,rightclips=6,roamtime=0;
bool up,down,left,right,yellow,red,blue,purple,ill,still;

SDL_Rect obscolbox;



void update_rect()
{
obscolbox.x=start_pos_x;
obscolbox.y=start_pos_y;
obscolbox.w=50;
obscolbox.h=50;
}

bunny()
{
start_pos_x = (40+rand()%540);
start_pos_y = (40+rand()%300);
int dir = (rand()%3);
if(dir==0){up=true;}
if(dir==1){down = true;}
if(dir==2){left = true;}
if(dir==3){right = true;}
update_rect();
collisionboxes.push_back(obscolbox);
}
Last edited on
You want the vector to hold a reference to the SDL_Rect so

std::vector<SDL_Rect*> collisionboxes ;

or

std::vector<std::reference_wrapper<SDL_Rect>> collisionboxes ;

Although, this could be problematic if, for example, a bunny is stored in a vector that grows and the location of the bunny (and its SDL_Rect changes.)
Last edited on
I cant work out why the collision box isn't working now? is it something to do with not using a reference to bunnies in the vector not updating the collision boxes?

they are just happily roaming around passing straight through each other, but they get stuck at the beginning if I dont set collided to false...but only right at the beginning

heres my 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
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

//////~///////

class bunny{

public:

//ints and bools etc///


SDL_Rect obscolbox;



void update_rect(int x,int y)
{
obscolbox.x=x;
obscolbox.y=y;
obscolbox.w=50;
obscolbox.h=50;
}

bunny()
{
int sex = (1+rand()%2);
if (sex == 2){female = true;}
start_pos_x = (40+rand()%540);
start_pos_y = (40+rand()%300);
int dir = (rand()%3);
if(dir==0){up=true;}
if(dir==1){down = true;}
if(dir==2){left = true;}
if(dir==3){right = true;}
update_rect(start_pos_x,start_pos_y);

}

bool bunny_collision(SDL_Rect B)
{
    int leftA, leftB;
    int rightA, rightB;
    int topA, topB;
    int bottomA, bottomB;

    leftA = obscolbox.x;
    rightA = obscolbox.x + obscolbox.w;
    topA = obscolbox.y;
    bottomA = obscolbox.y + obscolbox.h;

    leftB = B.x;
    rightB = B.x + B.w;
    topB = B.y;
    bottomB = B.y + B.h;

    if( bottomA <= topB )
    {
        return false;
    }

    if( topA >= bottomB )
    {
        return false;
    }

    if( rightA <= leftB )
    {
        return false;
    }

    if( leftA >= rightB )
    {
        return false;
    }

    return true;
}




void new_direction()
{
int dir = 0;
dir = (1+rand()%5);
if(dir==1){up=true;}
if(dir==2){down = true;}
if(dir==3){left = true;}
if(dir==4){right = true;}
if(dir==5){still = true;}
}

bool check_pos()
{
if ((start_pos_x < 5)||(start_pos_x > 640-51)||(start_pos_y<5)||(start_pos_y>475-47)||(collided==true))
{

return true;
}
else
    return false;
}

void roam_about()
{
    
     //lots of directional if statements like the one below (if up is true...etc)
   
    else if (right == true)
    {

    apply_surface(start_pos_x = start_pos_x +5,start_pos_y = start_pos_y,bunnysheet,screen,&clips[rightclips]);
    rightclips++;
    roamtime++;
    if(rightclips == 9){rightclips = 6;}
    if(check_pos()==true){right = false; roamtime = 0; left = true;}
    else if(roamtime == 9){right = false; roamtime = 0; new_direction();}
    }

    update_rect(start_pos_x,start_pos_y);//<-updates rect position for object
    collided = false;//<-most bunnies are rooted to the spot if i dont do this, but not all

}

};

///main///

//initialize stuff

bunny fatso;
bunny bugs;

vector<bunny>bunny_list;

bunny_list.push_back(fatso);
bunny_list.push_back(bugs);


for(int q = 0;q < 1000;q++)
{

bugs.roam_about();
fatso.roam_about();

//So the idea below is to go through each bunny and see whos colliding
 //with the collision function, I was also hoping to use the numbers to
//check things against the two coliding bunny, one could be male and the 
//other could be female (am gonna make if alpha bunny = true then beat 
//up omega bunny, im gonna simulate their whole complex social structure 
//:D) 

for (int n =0;n<bunny_list.size();n++ )
    for (int z =0;z<bunny_list.size();z++ )
       {
           bunny_list[n].bunny_collision(bunny_list[z].obscolbox);
       }


//flipscreen stuff


bucking fump
The vector bunny_list is storing bunny objects. When you add a bunny like so, bunny_list.push_back(fatso);, bunny_list will store a copy of fatso. Changes made to fatso will not affect the object in bunny_list and vice versa.
Could have each cell of the board store a stack of bunnies. Each turn just randomly move all your bunnies by popping each stack and pushing them onto a new one. After this move, check the size of each stack. If any stack has size greater than 1, then you had a collision. By popping off all bunnies except one, you get rid of the colliding bunnies. From there you can handle the collision how you wish, be it backtracking and trying a new move, killing the bunny that was there, or by bunny orgy.

Just one way to do it. Has the benefit of being simple and segregating actions nicely.
How can I get a vector of references to bunny objects, where I can still call a function on the reference to the bunny to get results :/

its a sdl screen this time, not a hacky board, I have to update the bunnehs in the vector some how :/
Last edited on
funk
Instead of using fatso and bugs, use the ones in the list.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//initialize stuff

vector<bunny>bunny_list;

bunny_list.push_back(bunny());
bunny_list.push_back(bunny());

for(int q = 0;q < 1000;q++)
{

    for ( auto& b : bunny_list )
        b.roam_about() ;

    // ...

    for (int n =0;n<bunny_list.size()-1;n++ )
        for (int z =n+1;z<bunny_list.size();z++ )
        {
            bunny_list[n].bunny_collision(bunny_list[z].obscolbox);
        }

    // ... 


[edit: Oops. auto b should've been auto& b]
Last edited on
oh yeah :D,
Is it possible to keep the data in the vector up to date with the object?
If you store all bunny objects in the vector, like cire showed you, you will not have keep anything "up to date" because there will only be one object for each bunny.
Last edited on
hurr, Im not familiar with my problem then, I have made head way thanks guys, now to spend a few more hours solving the problem again
I liked my stack solution /shrug
@biscuit, it was good I wish I thought of that at when I was doing the early hack board version, I would probaly lose controls of the bunnies
Is it possible to keep the data in the vector up to date with the object?


If you tried the code I posted in the earlier post (which has since been corrected,) there was a bug, which meant the roam_about function was being called on copies of the stored bunnies. You can see the correction in the [edit:] note.
Last edited on
I got sound to work on a test program, you know why it wont work now?

I think the devil is in my computer, im going to smash it ALL up for sure

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

Mix_Chunk *note = NULL;


~
main ()
~

    SDL_Init( SDL_INIT_EVERYTHING );
    screen=SDL_SetVideoMode(SCREEN_WIDTH,SCREEN_HEIGHT,SCREEN_BPP,SDL_SWSURFACE);
    Mix_OpenAudio( 22050, MIX_DEFAULT_FORMAT, 2, 4096);


    note = Mix_LoadWAV("DustyF10-lowPianoTomEndHit.wav");

/
       Mix_PlayChannel(-1,note,0);

    ~


1
2
3
4
5
6
7
class="quote">
class="qd"> FUNK
Last edited on
Topic archived. No new replies allowed.