|
hello!I have a problem. |
keprast
Member #16,794
January 2018
|
This is my new code, 360 degrees can be used.(He's a circle.) 1 box1_x = x + wtW / 2.0;
2 box2_x = 400.0 + wtW1 / 2.0;
3
4 box1_y = y + wtH / 2.0;
5 box2_y = 400.0 + wtH1 / 2.0;
6
7 float l, lx, ly;
8 bool x1big, y1big, x2big, y2big;
9
10 if (!key_03) {
11 x1big = x2big = y1big = y2big = false;
12 if (box1_x >= box2_x) {
13 lx = box1_x - box2_x;
14 x1big = true;
15 }
16 else
17 {
18 lx = box2_x - box1_x;
19 x2big = true;
20 }
21 if (box1_y >= box2_y) {
22 ly = box1_y - box2_y;
23 y1big = true;
24 }
25 else
26 {
27 ly = box2_y - box1_y;
28 y2big = true;
29 }
30
31 l = lx*lx + ly*ly;
32 l = sqrt(l);
33
34 if (wtH / 2.0 + wtH1 / 2.0 <= l) {
35 /*无碰撞*/
36
37 }
38 else
39 {
40 /*有碰撞*/
41 if (x1big && y1big && !x2big && !y2big) {
42 x += 4.2;
43 y += 4.2;
44 }
45 if (!x1big && y1big && x2big && !y2big) {
46 x -= 4.2;
47 y += 4.2;
48 }
49 if (x1big && !y1big && !x2big && y2big) {
50 x += 4.2;
51 y -= 4.2;
52 }
53 if (!x1big && !y1big && x2big && y2big) {
54 x -= 4.2;
55 y -= 4.2;
56 }
57 }
58 key_03 = true;
59 }
Now, I have only two objects. If you have time, can you help me?:) |
Chris Katko
Member #1,881
January 2002
|
You want functions and objects, and arrays of objects! 1struct object
2{
3float x, y, w, h;
4bool is_alive; // if is_alive == 0, it's an empty/dead object. We set == 1 when we create an object.
5}
6
7object array[100]; //up to 100 possible objects. There are other ways to do this. You can use C++ vectors, or arrays of pointers where a NULL pointer is the lack of an object (so you don't need is_alive). This is just the simplest way.
8
9void setup_objects() //create some objects to play with
10 {
11 array[0].is_alive = 1;
12 array[0].x = 50;
13 array[0].y = 50;
14 array[0].w = 20;
15 array[0].h = 20;
16
17//this should be close enough to collide below with the first one.
18 array[1].is_alive = 1;
19 array[1].x = 55;
20 array[1].y = 50;
21 array[1].w = 20;
22 array[1].h = 20;
23
24
25//this should NOT be close enough
26 array[2].x = 255;
27 array[2].y = 50;
28 array[2].w = 20;
29 array[2].h = 20;
30 }
31
32//check whether two objects are colliding
33bool is_colliding(object *obj1, object *obj2)
34{
35if(obj1.x > obj2.x - obj.w/2 && obj1.x < obj2.x + obj.w/2
36 && obj1.y > obj2.y - obj.h/2 && obj1.y < obj2.y + obj.h/2) //check whether our object x/y centered point is within a centered rectangle around obj2 (you can change your collision criterea to whatever you want)
37 {
38 return true; //these two objects are colliding
39 }else{
40 return false; // we didn't find these two objects colliding
41 }
42 }
43
44//now to check whether ANY two objects are colliding using the generic "are THESE two objects colliding" function we just made:
45
46void find_collisions()
47 {
48 for(int i=0;i<100;i++) //search all possible objects from 0 to 100
49 for(int j=0;k<100;j++) //against all objects again from 0 to 100 (so it's 0 against 0,1,2...,99; then 1 against 0,1,2...99; and then 2 against 0,1,2,...99, etc)
50 {
51//(except where i == j, we don't check the same object against itself)
52 if(i != j)
53 if(is_colliding(array[i], array[j])
54 {
55 //these two objects are colliding! Do something with them.
56 // like this:
57 // array[i].is_alive = false; // we just killed the object at index i.
58 }
59 }
60 }
61
62
63}
So we have an array of similar objects. We have a function for checking whether ANY specified two objects are colliding (using a supplied collision criteria--like distance from the center point which is the easiest). And we have a function that checks ALL objects against ALL OTHER objects using that same collide function. That's basically it. -----sig: |
keprast
Member #16,794
January 2018
|
@Chris Katko I don't think he is efficient, though. |
Chris Katko
Member #1,881
January 2002
|
That's the most basic, simplified version I can make to illustrate what's going on. There are plenty of intermediate and advanced tricks for speeding that up. - For example, you can keep a SORTED array, so that all alive ones come first and dead ones come second. Then, the second you encounter a "dead" one, you just stop parsing the array. But then I'd have to explain how and why sorting works, and how to add additional entries, and edge cases like pointers to objects get invalidated if you move them around in the array. - You can also use a linked list which supports removing elements in the middle. However a linked list is actually slower than a vector. - If you have C++, simply use a std::vector. It's an array that automatically resizes itself if it needs to grow. https://stackoverflow.com/questions/4442477/remove-ith-item-from-a-c-stdvector Are you using C or C++? Those are also data structure optimizations. You can use quadtrees to keep far away objects from ever being checked against each other. https://www.wikiwand.com/en/Quadtree But that's a more advanced structure and muddles the core aspects: - You have a array/list/etc of objects You can always google "basic collision detection tutorial" for tons of articles out there. -----sig: |
|