Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » 2D game has a little problem here

This thread is locked; no one can reply to it. rss feed Print
2D game has a little problem here
CodeOne
Member #15,442
December 2013

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

#SelectExpand
1 2//GLOBAL 3NUM_TRIANGLE=5; 4 5//Declaration 6void Inittriangle(Triangle triangle[],int size); 7void Drawtriangle(Triangle triangle[],int size); 8void Starttriangle(Triangle triangle[],float *Cameraposition,int size); 9void Updatetriangle(Triangle triangle[],float*Cameraposition,int size); 10 11void Cameraupdate(float *Cameraposition,Rball &ball,int BALL_WIDTH,int BALL_HEIGHT); 12 13//Somewhere in While loop. 14if(!isGameOver) // isGameOver not true 15 { 16 jump(ball,gnd); 17 Starttriangle(triangle,Cameraposition, NUM_TRIANGLE); 18 Updatetriangle(triangle,Cameraposition, NUM_TRIANGLE); 19 BallCollideTriangle(ball,triangle,NUM_TRIANGLE); 20 21 if(ball.lives <=0) 22 isGameOver = true; 23 } 24 25 26 27 28//Definition 29void Inittriangle(Triangle triangle[],int size) 30{ 31 for(int i = 0; i < size; i++) 32 { 33 triangle[i].ID = ENEMY; 34 triangle[i].live = false; 35 triangle[i].boundx = 15; 36 triangle[i].boundy = 15; 37 } 38} 39 40void Drawtriangle(Triangle triangle[],int size) 41{ 42 for(int i = 0; i < size; i++) 43 { 44 45 if(triangle[i].live) 46 { 47 // TRIED to work it up in many ways, Not looking good! Aint working 48 49 /*if( triangle[i-1].live && triangle[i].x -20 < triangle[i-1].x + 20 && 50 triangle[i].x + 20 > triangle[i-1].x - 20 ) 51 { 52 triangle[i].live =false; 53 //al_draw_filled_triangle(triangle[i].x , triangle[i].y , triangle[i].x + 40, triangle[i].y , triangle[i].x + 20 , triangle[i].y - 40, al_map_rgb(255,0,255)); 54 } 55 56 else 57 {*/ 58 al_draw_filled_triangle(triangle[i].x , triangle[i].y , triangle[i].x + 40, triangle[i].y , triangle[i].x + 20 , triangle[i].y - 40, al_map_rgb(255,0,255)); 59 60 } 61 } 62} 63 64 65void Starttriangle(Triangle triangle[],float *Cameraposition,int size) 66{ 67 for(int i = 0; i < size; i++) 68 { 69 if(!triangle[i].live) 70 { 71 if(rand() % 200 == 0) 72 { 73 triangle[i].live = true; 74 triangle[i].x = 800 +Cameraposition[0]; 75 triangle[i].y = 325; 76 77 } 78 } 79 } 80} 81 82void Updatetriangle(Triangle triangle[],float *Cameraposition,int size) 83{ 84 for(int i = 0; i < size; i++) 85 { 86 if(triangle[i].live) 87 { 88 89 if(triangle[i].x < Cameraposition[0]) 90 triangle[i].live = false; 91 count++; 92 93 } 94 95 96 } 97}

gezegond
Member #14,762
December 2012
avatar

Your triangles don't seem to move in the update function. If that's the case, just ensuring they don't spawn on top of each other will mean they won't overlap.

In your Starttriangle function, after you created your triangle check collisions with all the rest of the triangles on the screen, if collision occurs, retry spawn position until it doesn't.

for retrying spawn position you could simply randomize it again or you could write some algorithms that finds the closest place where it wouldn't collide.

l j
Member #10,584
January 2009
avatar

Only spawn a new triangle after the player has moved a large enough distance?

CodeOne
Member #15,442
December 2013

Solved it! used another triangle T to store the values of next triangle to be generated, and checked for overlapping. If not overlap, draw them!

Go to: