I am in need of assistance and I'm not sure if /prog/ does this sort of thing, but I'm asking anyway.
I'm currently writing my Final Project for my Java course and it is a game, a relatively simple game. The issue I'm having is collision. The point of the game is to keep your square from being hit by circles, and in the early stages of writing it, I had a detection system based on the coordinates of the start of the rectangle, and the start of the circle, plus the width and height of each, using the g2d.fillRect(x,y,w,h) and the g2d.fillOval(x,y,w,h)m however, during the course of editing, this stopped working for whatever reason.
My question basically boils down to this:
Is there a way to using the pixel colors to detect collision, something like,
if(bluePixel touches greenPixel)
collision = true;
If this is not possible, then shit.
If so, please assist me.
>>1
Just a reminder, bro - you're talking about programming, everything is possible. Now the question is whether you actually should do it the way you described (answer: no, it's a stupid idea).
There's a lot of literature around the Internet about collision detection, I suggest that you look around and read some. Just a hint: comparing stuff per-pixel is generally a bad idea. You're testing whether two simple geometrical shapes intersect or not - you most definitely don't need to render them first, and if you can't think of a different way to do it then maybe programming isn't for you.
Name:
Student!HXEkJ/9fP62010-05-20 20:56
>>8
I figured it out, I merely rewrote the original collision detection system I had, and it worked.
Name:
Am I on /prog/ or /sci/?2010-05-20 20:57
>>1
Start by making sure the objects are within each others's bounding regions.
Proving that, construct the equation of the ellipse and then get the vector between the center of the ellipse to the rectangle.
Turn that vector into a y = x line equation. Also, render the equation for the ellipse into y = x form.
Make the two equations equal to each other - y = y - and solve for an x. There can be one or more x values depending on how much the shapes can embed themselves in each other before being collision checked.
From x, get (all) y values. If you have more than one set of x,y pairs, and you need or want one, average them out or re-use the vector between the two shapes's centers.
Name:
Student!HXEkJ/9fP62010-05-20 21:04
>>10
I'm not quite sure what that means, but this is what I did, it seems to work:
if(
(circle[i].x <= character.x +character.w) &&
(circle[i].x + circle[i].w >= character.x)&&
(circle[i].y <= character.y +character.h) &&
(circle[i].y + circle[i].h >= character.y)
)
gameEnd();
>>11
You wrote the step one >>10 is talking about. The rest of >>10 is about the specifics of the collision and making sure (where) the shapes definitely touch, something you don't have to worry about since, apparently, the game ends.
Name:
Anonymous2010-05-20 21:31
>>11
Have you noticed that you actually made your circle a square?