|
|
This thread is locked; no one can reply to it.
|
1
2
|
| Allegro collision help! |
|
Edgar Reynaldo
Major Reynaldo
May 2007
|
Okay , but how are charx and chary updated? My Website! | EAGLE GUI Library Demos | My Deviant Art Gallery | Spiraloid Preview | A4 FontMaker | Skyline! (Missile Defense) Eagle and Allegro 5 binaries | Older Allegro 4 and 5 binaries | Allegro 5 compile guide |
|
mrukki
Member #9,433
January 2008
|
|
Edgar Reynaldo
Major Reynaldo
May 2007
|
I don't know what's going on. Let's just see all of the code entirely. Upload it as an attachment if you need to and then we can skim through it and see if anything sticks out. Maybe I see something. My Website! | EAGLE GUI Library Demos | My Deviant Art Gallery | Spiraloid Preview | A4 FontMaker | Skyline! (Missile Defense) Eagle and Allegro 5 binaries | Older Allegro 4 and 5 binaries | Allegro 5 compile guide |
|
mrukki
Member #9,433
January 2008
|
Code! |
|
Edgar Reynaldo
Major Reynaldo
May 2007
|
I put your code up here because it's easier to refer to and look at with some formatting. There is an attachments button in the post editing area you can upload pictures / code with and then include a link to.
If you look at the code right around your call to the box_check function : if (box_check(character, tree) == collision_none) { return 1; } Why is return 1 there? If there's no collision quit? But that's besides the point , right around the call to box_check , you never set all your walk_direction variables to true each time through the loop and check to see if they should be false by looking at the value box_check returns. You should set all four walk_ variables to true each time through the while loop and then use the return value of box_check to decide whether any of those variables should be set to false to prevent movement in that direction. My Website! | EAGLE GUI Library Demos | My Deviant Art Gallery | Spiraloid Preview | A4 FontMaker | Skyline! (Missile Defense) Eagle and Allegro 5 binaries | Older Allegro 4 and 5 binaries | Allegro 5 compile guide |
|
mrukki
Member #9,433
January 2008
|
Oh, I just used that just to test if (box_check(character, tree) == collision_none) { walk_u = true; walk_d = true; walk_r = true; walk_l = true; } Do you mean that I should do like this: while (!key[KEY_ESC]) { while (speed > 0) { walk_u = true; walk_d = true; walk_r = true; walk_l = true;
And then the rest of the loop? |
|
Edgar Reynaldo
Major Reynaldo
May 2007
|
You have to use the result of box_check to set the variables that allow your character to walk in a certain direction :
Like I said though , this method will let your character overlap other objects by one pixel because it checks whether the character is currently overlapping another object , not whether it would after it moved one in that direction. My Website! | EAGLE GUI Library Demos | My Deviant Art Gallery | Spiraloid Preview | A4 FontMaker | Skyline! (Missile Defense) Eagle and Allegro 5 binaries | Older Allegro 4 and 5 binaries | Allegro 5 compile guide |
|
mrukki
Member #9,433
January 2008
|
Wow, Thanks man! EDITED: Oh, It didn't work |
|
someone972
Member #7,719
August 2006
|
Warning: this is a very long post. The box_check function isn't set up right. int box_check(object &obj1, object &obj2) { if (obj1.bottom == obj2.top) { return collision_bottom; } if (obj1.top == obj2.bottom) { return collision_top; } if (obj1.right == obj2.left) { return collision_right; } if (obj1.left == obj2.right) { return collision_left; } else collision_none; } The last line should return collision_none, otherwise it won't tell you there wasn't a collision. Also, in the if statements it checks only for the x or y position. This will cause problems. For example:
The new object structure struct object { int x; int y; int w; int h; BITMAP *image; }; This allows you to easily move and setup the object. This combined with the new collision function make it so you can do this to set them up and move them. The new collision detection function
This may seem complicated at first, but it is actually pretty simple. First is: if(obj1.y+obj1.h >= obj2.y && obj1.y <= obj2.y+obj2.h && obj1.x+obj1.w >= obj2.x && obj1.x <= obj2.x+obj2.w) This checks if there was a collision and works even if the object is not exactly on the edge of the other. It is basically if(obj1.bottom >= obj2.top && obj1.top <= obj2.bottom && obj1.rgiht >= obj2.left && obj1.left <= obj2.right) Here is a picture explaining it more. int side = collision_bottom; int overlap = abs((obj1.y+obj1.h) - obj2.y); The side is the side of collision which is returned, just like in the other function. Overlap is how far one shape is inside the other and should be used to move the shape out. Finally these lines:
This check the next side. If the overlap is less than that is the side closest to the edge and is where it should be removed from. This is continued for all the sides. Then it just sets the overlap variable and returns the side of collision. int overlap; collision_state = box_check(character , tree,overlap); switch (collision_state) { case collision_none : overlap = 0; break; case collision_left : character.x += overlap;walk_l = false;break; case collision_right : character.x -= overlap;walk_r = false;break; case collision_top : character.y += overlap;walk_u = false;break; case collision_bottom : character.y -= overlap;walk_d = false; default : break; } With this you can move the objects larger amounts and it will still collide correctly. Conclusion ______________________________________ |
|
Audric
Member #907
January 2001
|
(bump) |
|
mrukki
Member #9,433
January 2008
|
Oh yeah!! I't working! |
|
|
1
2
|