Hello Guest, please login or register.
Did you miss your activation email?
Login with username, password and session length.

Pages: [1] 2   Go Down

Author Topic: [C++] Collision Checker Bug  (Read 5463 times)

0 Members and 1 Guest are viewing this topic.

King Tetiro

Leader of Phoenix Heart
[C++] Collision Checker Bug
« on: November 30, 2010, 08:25:47 pm »
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 3549
I constructed a collision checker for it basing it off another person's code. Now here's the thing, it should technically work. Below is the code

Code: [Select]
bool checkCollision(BITMAP* spra, BITMAP* sprb, int ax, int ay, int bx, int by)
{
int trns=makecol(255,0,255);
int x,y;
if (spra == NULL || sprb == NULL)
return false;

if (min( ax+(spra->w % 2), bx+(sprb->w % 2) )<=max(ax-(spra->w % 2),bx-(sprb->w % 2) )
||min( ay+(spra->h % 2), by+(sprb->h % 2) )<=max(ay-(spra->h % 2),by-(sprb->h % 2) ) )
return false;

for(x=max(ax-(spra->w % 2),bx-(sprb->w % 2) );x<min( ax+(spra->w % 2), bx+(sprb->w % 2) );x++)
{

for(y=max(ay-(spra->h % 2),by-(sprb->h % 2) );y<min( ay+(spra->h % 2), by+(sprb->h % 2) );y++)
{

if (getpixel(spra,x-ax,y-ay) != trns && getpixel(sprb,x-bx,y-by) != trns)
return true;

}

}

return false;
}


Now I've looked over the code and I can't work it out. I know that the sprites exist. If anyone can help me fix this I'd appreciate it.

EDIT: Almost forgot

ax,ay = Object A's co-ordinates
bx,by = Object B's co-ordinates
trns = Magenta (Transparency)

This is meant to be perfect pixel collision (box bounding doesnt suit me)

The % keeps the division integers
« Last Edit: November 30, 2010, 08:30:36 pm by King Tetiro »
Logged
  • Phoenix Heart

Xiphirx

wat
Re: [C++] Collision Checker
« Reply #1 on: November 30, 2010, 08:27:37 pm »
  • Xiphirx
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 3007
...

We need more details. Looking at it right now, I'm guessing it is pixel perfect?
Logged
  • For The Swarm

King Tetiro

Leader of Phoenix Heart
Re: [C++] Collision Checker
« Reply #2 on: November 30, 2010, 08:28:42 pm »
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 3549
...

We need more details. Looking at it right now, I'm guessing it is pixel perfect?

Yes it is. And I noticed I haven't given enough. So I'll try to.
Logged
  • Phoenix Heart

Xiphirx

wat
Re: [C++] Collision Checker Bug
« Reply #3 on: November 30, 2010, 08:44:47 pm »
  • Xiphirx
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 3007
You might want to try something like

http://wiki.allegro.cc/index.php?title=Pixel_Perfect_Collision

Here's a quick approach
Code: [Select]
int trns=makecol(255,0,255);
if (    (ax >= bx) &&
(ax <= (bx + sprb->w) ) &&
(ay >= by) &&
(ay <= (by + sprb->h) )
   )
{
//There is a possibility for collision, so check for pixel perfect (this will give you bounding box collision)
for (int i=0; i < spra->h; i++)
{
for (int k=0; k < sprb->w; k++)
{
if ( getpixel(spra, k, i) == trns)
{
//No collision here
}
else
{
//check other bitmap
}
}
}
}
else
{
return false;
}
Logged
  • For The Swarm

King Tetiro

Leader of Phoenix Heart
Re: [C++] Collision Checker Bug
« Reply #4 on: November 30, 2010, 08:50:03 pm »
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 3549
How would I check the other bitmap?

Would it be similar to the loop for the first bitmap?

EDIT: Now I can't move at all.
« Last Edit: November 30, 2010, 08:53:39 pm by King Tetiro »
Logged
  • Phoenix Heart

Xiphirx

wat
Re: [C++] Collision Checker Bug
« Reply #5 on: November 30, 2010, 09:04:30 pm »
  • Xiphirx
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 3007
How would I check the other bitmap?

Would it be similar to the loop for the first bitmap?

You will need to map the other bitmap onto the first bitmap. Look at the attached image.

The red dot shows where the collision should return true. Notice that its at different coordinates for each image. For image A its at (10,4), but for image B its at (0,0)

You need to figure out the overlap and compare pixels from there. Which can be something like ax+spra->w - bx for x and ay+spra->h - by

Then do a for loop on that portion of the image and check if both pixels are transparent.

It also seems like this is all confusing you... I'd seriously recommend not getting ahead of yourself.

Logged
  • For The Swarm

King Tetiro

Leader of Phoenix Heart
Re: [C++] Collision Checker Bug
« Reply #6 on: November 30, 2010, 09:09:54 pm »
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 3549
How would I check the other bitmap?

Would it be similar to the loop for the first bitmap?

You will need to map the other bitmap onto the first bitmap. Look at the attached image.

The red dot shows where the collision should return true. Notice that its at different coordinates for each image. For image A its at (10,4), but for image B its at (0,0)

You need to figure out the overlap and compare pixels from there. Which can be something like ax+spra->w - bx for x and ay+spra->h - by

Then do a for loop on that portion of the image and check if both pixels are transparent.

It also seems like this is all confusing you... I'd seriously recommend not getting ahead of yourself.



Of course it's confusing me but it's tough !@#$%. I have to learn in next week anyway as it's part of the unit. And now it's the only thing left to do. I don't understand your post though

EDIT: Btw, the code I based it off is the allegro wiki one
« Last Edit: November 30, 2010, 09:12:23 pm by King Tetiro »
Logged
  • Phoenix Heart

Xiphirx

wat
Re: [C++] Collision Checker Bug
« Reply #7 on: November 30, 2010, 09:14:29 pm »
  • Xiphirx
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 3007
I'd rather have your teacher teach you then. I'm a horrible teacher, it's no surprise you didn't understand me :P

Logged
  • For The Swarm

King Tetiro

Leader of Phoenix Heart
Re: [C++] Collision Checker Bug
« Reply #8 on: November 30, 2010, 09:26:41 pm »
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 3549
Okay Im getting really pissed off at this now. Can someone please post pixel perfect collision that will work so I can continue work!

This is getting incredibly stressful
Logged
  • Phoenix Heart
Re: [C++] Collision Checker Bug
« Reply #9 on: November 30, 2010, 09:35:46 pm »
  • Doesn't afraid of anything
  • *
  • Reputation: +42/-0
  • Offline Offline
  • Gender: Male
  • Posts: 7002
Are you doing this for like..school?  Because it seems like you're getting way ahead of yourself.

And no, we're not going to just post working code for you.  Programming doesn't work that way, especially not in real languages like C++.
Logged



i love big weenies and i cannot lie

Xiphirx

wat
Re: [C++] Collision Checker Bug
« Reply #10 on: November 30, 2010, 09:36:56 pm »
  • Xiphirx
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 3007
Yes, this is not GM. There are many different things that vary from program to program.
Logged
  • For The Swarm

King Tetiro

Leader of Phoenix Heart
Re: [C++] Collision Checker Bug
« Reply #11 on: November 30, 2010, 09:42:49 pm »
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 3549
Sorry I shouldnt have demanded it. Im just so stressed right now. I've been working on it for a week with no luck. In fact I've given up now to cool off. I think Im going to have to leave it.

And of course it's not GM. C++ is much more powerful.

Though here's the thing, if I don't have a clue how to do it, the rest of the class are screwed. Im one of the smartest for programming in the class.

And can you all stop saying "u're getting ahead of yourself"? It keeps putting me down.

Though can we discuss how to solve this?

EDIT: Okay not even NOT pixel perfect collision won't work. Im thinking go to the lecturer tomorrow
« Last Edit: November 30, 2010, 09:52:01 pm by King Tetiro »
Logged
  • Phoenix Heart

Xiphirx

wat
Re: [C++] Collision Checker Bug
« Reply #12 on: November 30, 2010, 09:54:45 pm »
  • Xiphirx
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 3007
Sorry I shouldnt have demanded it. Im just so stressed right now. I've been working on it for a week with no luck. In fact I've given up now to cool off. I think Im going to have to leave it.

And of course it's not GM. C++ is much more powerful.

Though here's the thing, if I don't have a clue how to do it, the rest of the class are screwed. Im one of the smartest for programming in the class.

And can you all stop saying "u're getting ahead of yourself"? It keeps putting me down.

Though can we discuss how to solve this?

EDIT: Okay not even NOT pixel perfect collision won't work. Im thinking go to the lecturer tomorrow

See, we don't know how you are structuring your game, how you handle movement, etc. We're only saying that you're getting ahead of yourself so you won't be disappointed. It's wisdom, not a way to put you down.

I believe I gave enough information to formulate a working algorithm... The wiki link I posted already has a copypasta function for your use...
Logged
  • For The Swarm

Mamoruanime

@Mamoruanime
Re: [C++] Collision Checker Bug
« Reply #13 on: November 30, 2010, 10:00:41 pm »
  • ^Not actually me.
  • *
  • Reputation: +9/-0
  • Offline Offline
  • Gender: Male
  • Posts: 9786
I think I said this a few times in your other threads; but seriously- learn in your class, not here. You're not going to get any help that's really going to benefit you at all when learning this language. It's just going to confuse you.
Logged

King Tetiro

Leader of Phoenix Heart
Re: [C++] Collision Checker Bug
« Reply #14 on: November 30, 2010, 10:04:46 pm »
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 3549
Okay guys now there is a problem. I've done a full copy and paste of the wiki version, and it won't work.

In regards to movement, it's like this

loops from 0 to <speed
if collision check (x-1,y) //Assume its for left here
move left 1 pixel
end loop

That's pretty much it
Logged
  • Phoenix Heart
Re: [C++] Collision Checker Bug
« Reply #15 on: November 30, 2010, 10:21:54 pm »
  • Doesn't afraid of anything
  • *
  • Reputation: +42/-0
  • Offline Offline
  • Gender: Male
  • Posts: 7002
Bit of an odd way of doing things...

You should look into the separating axis theorem.
Logged



i love big weenies and i cannot lie

King Tetiro

Leader of Phoenix Heart
Re: [C++] Collision Checker Bug
« Reply #16 on: November 30, 2010, 10:35:06 pm »
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 3549
Ok guys, Im closing this topic. This isnt an obvious glitch.

I've boiled it down to the most simplistic collision check and it doesnt work so Im taking it to the lecturer. I've asked you, old friends and even other good programmers. Nothing. I will question the lecturer on this.
Logged
  • Phoenix Heart

Xiphirx

wat
Re: [C++] Collision Checker Bug
« Reply #17 on: November 30, 2010, 10:36:50 pm »
  • Xiphirx
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 3007
uh... You should be able to just move, check for collisions, then handle the collision ...
Logged
  • For The Swarm

King Tetiro

Leader of Phoenix Heart
Re: [C++] Collision Checker Bug
« Reply #18 on: November 30, 2010, 10:42:13 pm »
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 3549
Doesnt even want to do that. I literally have no clue why its not working
Logged
  • Phoenix Heart

Mamoruanime

@Mamoruanime
Re: [C++] Collision Checker Bug
« Reply #19 on: November 30, 2010, 11:03:43 pm »
  • ^Not actually me.
  • *
  • Reputation: +9/-0
  • Offline Offline
  • Gender: Male
  • Posts: 9786
Doesnt even want to do that. I literally have no clue why its not working

I think he means you need to structure it in a way that it works the way he's mentioning...
Logged
Pages: [1] 2   Go Up

 


Contact Us | Legal | Advertise Here
2013 © ZFGC, All Rights Reserved



Page created in 0.027 seconds with 75 queries.

anything