2D game problem here

Listen up guys,

I have created a side scrolling game in 800x400 display. Ball continuously moves in right direction, camera follows. Enemy triangles keep coming, ball jumps over them (NOTE: triangles are not moving,but the ball is moving towards right) . The game is fully functional if i want these triangles to overlap, but NO I don't want them to overlap.

What i tried so far: I checked for collisiondetection of a triangle relative to previous LIVE triangle. If they collide i make the current triangle[i].live = false. NOT WORKING

Long story short, HELP!! :P

Last edited on
I see 2 problems with your previous approach.

1) You aren't wrapping around the end of your array... so when you place triangle[0], you are checking to see if it overlaps triangle[-1] (of course, triangle -1 doesn't exist, as it's out of bounds). Instead, you should be checking triangle[size-1]

2) You should not be doing that check in Drawtriangle because it has nothing to do with drawing. Instead you should not be even generating the triangle if it would generate on top of another one. I would put the logic in Starttriangle.


It also seems a little risky that you would only check collision with the previous triangle. It's very possible it'll work due to the way the triangles are dying, but it still seems sketchy to me. I would check collision with all triangles rather than just the previous.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
void Starttriangle(Triangle triangle[],float *Cameraposition,int size)
{
  if(rand() % 200 != 0)
    return;  // <- do nothing (don't spawn) unless the random number comes up

  // prep the triangle we want to add
  Triangle t;
  t.live = true;
  t.x = 800 + Cameraposition[0];
  t.y = 325;

  // see if that triangle overlaps any other
  for(int i = 0; i < size; ++i)
  {
     if( Trianglesoverlap( t, triangle[i] ) )
       return;  // <- do nothing (don't spawn a triangle) if they overlap
  }

  // if we got here, the triangle should be added... so add it
  for(int i = 0; i < size; ++i)
  {
    if( !triangle[i].live )
    {
      triangle[i] = t;  // add it
      return;    // and exit this function
    }
  }
}



EDIT: you could combine those two for loops to record the index of the first available open slot which would give you a slight performance boost... but unless the array is extremely large I wouldn't worry about it.
Last edited on
Topic archived. No new replies allowed.