Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Horizontal collision

Credits go to Edgar Reynaldo for helping out!
This thread is locked; no one can reply to it. rss feed Print
Horizontal collision
wolf_wolf17
Member #13,811
December 2011
avatar

|
| = Collision line.
|

I have been able to create these two collision lines, using

int x2 = -10;
int y2 = 465;

int x3 = 625;
int y3 = 10;

this comes out.

|           |
|           |
|           |
|           |

when I try to add collision on the top with:

int x4 = 515;
int y4 = -3;

this comes out:

|       |   |
|       |   |
|       |   |
|       |   |

So I was wondering how to make it look like this:

||||||||||
|        |   
|        |   
|        |

Hope that makes sense! ???
Thanks in advance.

torhu
Member #2,727
September 2002
avatar

Are you trying to draw lines? Or are the lines just imaginary and used for collision testing? Allegro 4 or 5?

wolf_wolf17
Member #13,811
December 2011
avatar

They are imaginary and are setting a border(Invisible), for collision.
Pretty sure its 5.0.5

torhu
Member #2,727
September 2002
avatar

Could you post your code, at least the relevant parts of it? I'm still not sure exactly what you're doing.

wolf_wolf17
Member #13,811
December 2011
avatar

#SelectExpand
1 int x2 = -10; 2 int y2 = 465; 3 int dir1 = 360; 4 5 int x3 = 625; 6 int y3 = 10 ; 7 int dir2 = 180; 8 9 int x4 = 515; 10 int y4 = -3; 11 12 13 Collision (x, y, x2, y2); 14 if (Collision(x, y, x2, y2) == true) { 15 if (dir1 == 360) 16 x -= 5; 17 18 if (dir1 == 180) 19 x += 5; 20 } 21 22 Collision (x, y, x3, y3); 23 if (Collision(x, y, x3, y3) == true) { 24 if (dir1 == 360) 25 x -= 5; 26 27 if (dir1 == 180) 28 x += 5; 29 } 30 31 Collision (x, y, x4, y4); 32 if (Collision(x, y, x4, y4) == true) { 33 if (dir1 == 360) 34 x -= 5; 35 36 if (dir1 == 180) 37 x += 5; 38 }

I'm not sure if I missed anything important, but that gives you the idea.
x, y = Sprite

All the x2 and others are invisible rectangles that I'm using as walls.

x4, y4 is the one I want to go horizontally.

torhu
Member #2,727
September 2002
avatar

Sorry man, I'm not a psychic. What does the Collision function look like? Just post the lot.

wolf_wolf17
Member #13,811
December 2011
avatar

Basically I wanna rotate which way my collision line is facing.
Basically I wanna rotate which way my bounding box is facing.

torhu
Member #2,727
September 2002
avatar

Well, here is a generic reply. If this doesn't answer your question, please post more of your code.

If you have a sprite moving inside a box (imaginary or not) where the upper left and lower right corners are defined by (box_x1, box_y1) and (box_x2, box_y2), you can do collision testing like this:

if (sprite_x <= box_x1 || (sprite_x + sprite_width) > box_x2) {
  /* reverse horizontal direction here */
}

if (sprite_y <= box_y1 || (sprite_y + sprite_height) > box_y2) {
  /* reverse vertical direction here */
}

If you want the box to be open at the bottom, just remove the comparison with box_y2.

wolf_wolf17
Member #13,811
December 2011
avatar

Thats the thing! I'm trying to make the top of the box. ::)

torhu
Member #2,727
September 2002
avatar

So, did you get it working?

wolf_wolf17
Member #13,811
December 2011
avatar

I got it... Kinda. Check this out for me? (Walk to top) The collision is messed up for some reason that I don't understand.

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

wolf_wolf17
Member #13,811
December 2011
avatar

where did I put that?

verthex
Member #11,340
September 2009
avatar

Just do the same thing you would for a vertical collision.

wolf_wolf17
Member #13,811
December 2011
avatar

I did but there is something wrong with how I did it and Im not sure what it is, I posted the code and stuff as attachment. Up a few posts.
No matter what I try I can't get a collision stretching across the top, no idea what i'm dong wrong.

torhu
Member #2,727
September 2002
avatar

if (Collision2(x, y, x4, y4) == true) {
          if (dir1 == 360)
           x -= 5;
           
          if (dir1 == 180)
          x += 5;
          }

It's because you are changing x instead of y here.

EDIT: And you are also using dir1 instead of dir2.

You could use an enum for the direction, like this:

enum Direction { North, East, South, West };

Direction dir = North;

Then use that instead of dir1 and dir2. And you could simplifiy your collision testing a lot, but let's take one step at a time. ;)

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

Edgar said:

if (sprite_y < top_boundary) {sprite_y = top_boundary;}

where did I put that?

You didn't, but if you had it would have solved all of your problems. If you don't want the top of a sprite to go above a certain y level, that is all you would have to do.....

wolf_wolf17
Member #13,811
December 2011
avatar

Oh my god, that's so beautifully easily!!! Thank-you very much.

Go to: