C Board  

Go Back   C Board > General Programming Boards > Game Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 09-13-2003, 08:30 AM   #1
Registered User
 
pheer's Avatar
 
Join Date: Sep 2003
Posts: 25
bouncing bullets

IŽm developing a tilebased game and i have written some code to make bullets bounce, but it cant get i to work 100%. Do anyone f you guys have any experince of this kind of code? HereŽs the code and a picture that hopefully explains how i check when the bullet should bounce, i also included the game if you want to see how the bullet behaves.

Code:
void 
CBullets::Update()
{
	float tempX, tempY, factor;
	int tx, ty, lo, lo2;
	int xx[4], yy[4], type[4];
	bool deleted, left, right, upp, ner;
	bool bouncedX, bouncedY;

	// Loop through all bullets
	for (lo = bullets.size()-1; lo >= 0; lo--) {
		deleted = left = right = up = down = bouncedX = bouncedY = false;
		type[0] = type[1] = type[2] = type[3] = -1;
		
		// Save bullet position before moving it
		tempX = bullets[lo].x; 
		tempY = bullets[lo].y;

		// Move bullet
		bullets[lo].x += cos_360[ bullets[lo].direction ] * bulletProperties[ bullets[lo].typ ].speed * time;
		bullets[lo].y -= sin_360[ bullets[lo].direction ] * bulletProperties[ bullets[lo].typ ].speed * time;
		
		// Did it move to the left or to the right
		if ( bullets[lo].x < tempX ) {
			left = true;
			factor = -1.0;
		} else if ( bullets[lo].x > tempX ) {
			right = true;
			factor = 1.0;
		}
		// If it moved to the left or to the right caluclate control points
		if (left || right) {
			// Tile in the upper left/right corner, control point 1 (see picture)
			xx[0] = (bullets[lo].x + bulletProperties[ bullets[lo].type ].halfWidth * factor) / tilemap->TileWidth();
			yy[0] = (tempY - kulBesk[ bullets[lo].typ ].halfHeight) / tilemap->TileHeight();
			type[0] = tilemap->pTileArray()[ yy[0] ][ xx[0] ].tile;
			
			// Tile in the lower left/right corner, control point 2 (see picture)
			xx[1] = (bullets[lo].x + bulletProperties[ bullets[lo].type ].halfWidth * factor) / tilemap->TileWidth();
			yy[1] = (tempY + kulBesk[ bullets[lo].typ ].halfHeight) / tilemap->TileHeight();
			type[1] = tilemap->pTileArray()[ yy[1] ][ xx[1] ].tile;	
		}

		// Did it move up or down
		if (bullets[lo].y < tempY) {
			up = true;
			factor = -1.0;
		} else if (bullets[lo].y > tempY) {
			down = true;
			factor = 1.0;
		}
		if (up || down) {
			// Tile in the left upper/lower corner, control point 3 (see picture)
			xx[2] = (tempX - bulletProperties[ bullets[lo].typ ].halfWidth) / tilemap->TileWidth();
			yy[2] = (bullets[lo].y + bulletProperties[ bullets[lo].typ ].halfHeight * factor) / tilemap->TileHeight();
			type[2] = tilemap->pTileArray()[ yy[2] ][ xx[2] ].tile;
			
			// Tile in the right upper/lower corner, control point 4 (see picture)
			xx[3] = (tempX + bulletProperties[ bullets[lo].typ ].halfWidth) / tilemap->TileWidth();
			yy[3] = (bullets[lo].y + bulletProperties[ bullets[lo].typ ].halfHeight * factor) / tilemap->TileHeight();
			type[3] = tilemap->pTileArray()[ yy[3] ][ xx[3] ].tile;	
		}

		// Step through the tiles that the bullets collides/passes
		for ( lo2 = 0; lo2 < 3; lo2++ ) {
			if ( type[lo2] > -1 ) {
				// Obstacle, remove or bounce
				if ( tilemap->pTileProperties()[ type[lo2] ].obstacle ) {
					
					// The bullet doesnt bounce, delete it and exit loop
					if ( !bulletProperties[ bullets[lo].type ].bouncing ) {
						deleted = true;
						bullets.erase( &bullets.at(lo) );
						break;
					
					// A bouncing bullet
					} else {
						// Tiles before the bullet was moved
						tx = tempX / tilemap->TileWidth();
						ty = tempY / tilemap->TileHeight();
						
						// Y
						if ( ty != yy[lo2] ) {
							bullets[lo].direction = 359 - bullets[lo].direction;

							++bullets[lo].bounces;
						
						// X
						} else if ( tx != xx[lo2] ) {
							bullets[lo].direction = 180 - bullets[lo].direction;
							if ( bullets[lo].direction < 0 ) bullets[lo].direction += 360;

							++bullets[lo].bounces;
						}
												
						// "Reset" position so that i doesnt get stuck in the wall
						bullets[lo].x = tempX;
						bullets[lo].y = tempY;

						break;
					}
				}
			}
		}

		// The bullet has bounced maximum nr of times, delete it
		if ( bullets[lo].bounces > bulletProperties[ bullets[lo].typ ].maxBounces ) {
			deleted = true;
			bullets.erase( &bullets.at(lo) );

		// Draw bullet if it wans deleted
		} else if ( !deleted ) {
			....draw bullet...
		}
	}
}
http://student.itn.liu.se/~mikgu166/temp/bullet.gif
tegame.zip (346k)
pheer is offline   Reply With Quote
Old 09-13-2003, 11:18 AM   #2
Pursuing knowledge
 
confuted's Avatar
 
Join Date: Jun 2002
Posts: 1,916
Hmmm... I didn't read all the code, and the diagram was a bit confusing. Are you trying to have a bullet "bounce" like a ball would off the wall in a Pong game, or are you trying to do something more "kid-ish" where the bullet will bounce off in some sort of smooth curve?
__________________
Away.
confuted is offline   Reply With Quote
Old 09-13-2003, 11:44 AM   #3
Registered User
 
pheer's Avatar
 
Join Date: Sep 2003
Posts: 25
The balls are bouncing as a ball in a pong-game, but when the ball hits a wall to its left/right it "sometimes" doesnt bounce, try the game and youŽll see what i mean
pheer is offline   Reply With Quote
Old 09-13-2003, 11:48 AM   #4
Pursuing knowledge
 
confuted's Avatar
 
Join Date: Jun 2002
Posts: 1,916
Quote:
Originally posted by pheer
The balls are bouncing as a ball in a pong-game, but when the ball hits a wall to its left/right it "sometimes" doesnt bounce, try the game and youŽll see what i mean
Can you post a link to the game?

Until then... is the problem in the collision detection (it doesn't detect a collision with the wall), or is the problem in the math to make it bounce?
__________________
Away.
confuted is offline   Reply With Quote
Old 09-13-2003, 11:56 AM   #5
Registered User
 
pheer's Avatar
 
Join Date: Sep 2003
Posts: 25
the link is up therem, under the link to the confusing diagram
its the colission detection that doesnt work, i think its only when the middle of the ball (y) hits where one tile meets another
pheer is offline   Reply With Quote
Old 09-17-2003, 05:47 AM   #6
Super Moderator
 
Bubba's Avatar
 
Join Date: Aug 2001
Posts: 7,814
Bouncing can be done easily by:


if (ball.x<=left) ball.xvel=-ball.xvel;
if (ball.x>=right) ball.xvel=-ball.xvel;
if (ball.y<=top) ball.yvel=-ball.yvel;
if (ball.y>=bottom) ball.yvel=-ball.yvel;


This does not take into account angles and will not always produce the desired results. But its a lot less code and produces decent results.

For angles, create a triangle out of the ball bouncing. The base of the tri is the distance the ball traveled in X, and the height of the tri is the distance in Y. Unfortunately the hypotenuse requires the use of the oh so slow sqrt(), but you can get around this using a Taylor-series expansion - which may or may not be faster in certain cases. You must store the initial x,y of your ball and the final x,y of your ball to create each tri. When the ball bounces - store the initial again and wait for the ball to hit another side to get the final.
__________________
If you aim at everything you will hit something but you won't know what it is.
Bubba is offline   Reply With Quote
Old 09-17-2003, 05:59 AM   #7
Registered User
 
pheer's Avatar
 
Join Date: Sep 2003
Posts: 25
I know how to bounce a ball on the screen, but bouncing it in a "tiled environment" is a little more complex than that, atleast i think so
pheer is offline   Reply With Quote
Old 09-17-2003, 09:58 PM   #8
Super Moderator
 
Bubba's Avatar
 
Join Date: Aug 2001
Posts: 7,814
Why would that be harder??

I'm lost.
__________________
If you aim at everything you will hit something but you won't know what it is.
Bubba is offline   Reply With Quote
Old 09-18-2003, 08:31 PM   #9
Banned
 
frenchfry164's Avatar
 
Join Date: Oct 2001
Posts: 1,552
The logic for projectiles should be exactly identical for all 2D games. If you make the projectiles work only within tiles, it won't be that flexible.

Actually, it could be a little simpler in a tiled environment for the collision detection, if you don't care about getting everything precisely accurate.
frenchfry164 is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
bullets [2D] St0rmTroop3er Game Programming 3 10-04-2004 11:26 PM
bullets lambs4 Game Programming 10 08-13-2003 06:32 AM
pic movement pode Game Programming 31 08-21-2002 09:30 PM
Allegro help-o Vicious Game Programming 109 06-11-2002 08:38 PM
Bouncing ball - help ?? Gugge C Programming 7 04-13-2002 02:34 PM


All times are GMT -6. The time now is 05:30 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22