7 Replies - 680 Views - Last Post: 17 December 2012 - 02:31 PM Rate Topic: -----

#1 jserete  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 16-December 12

C++ Collision Checking in DarkGDK

Posted 16 December 2012 - 02:45 PM

Is there a way I could check collision between the player sprite and a path formed by a lot of rectangles (around 50) generated randomly at the start of each run, so that the player cannot go off the path?
I have a lot (around 50) rectangles that I am currently using as Sprites. These rectangles are overlapping. I trying to check for collision to see if the player's move is legal (the player has to stick on the path formed by these rectangles, and cannot go off), and I'm currently doing this on individual rectangles. However, the collision checking is not accurate at all. Right now, I have no idea what to do. Could somebody point me in the right direction?
 // Dark GDK - The Game Creators - www.thegamecreators.com

// the wizard has created a very simple 2D project that uses Dark GDK
// it can be used as a starting point in making your own 2D games

// whenever using Dark GDK you must ensure you include the header file
#include "DarkGDK.h"

// the main entry point for the application is this function
	int delay = 0;
	int offsetX = 0;
	int offsetY = 0;
	int blockLimit = 50;
	int startX = 250;
	int startY = 300;
	int setX = startX;
	int setY = startY;
	int placementX [201];
	int placementY [201];
	int chance = 100;
	bool movableUp = true;
	bool movableDown = true;
	bool movableLeft = true;
	bool movableRight = true;
int createBlock(int placeX, int placeY, int index){
	placementX[index] = placeX;
	placementY[index] = placeY;
	return true;
}
int drawBlock(int placeX, int placeY, int index){
	dbLoadImage("block.bmp", index);
	dbDeleteSprite(index);
	dbSprite(index, placeX + offsetX, placeY + offsetY, index);
    return true;
}
int checkBlock(int index1, int index2){
	
		if(dbSpriteX(index1) + 150 < dbSpriteX(index2) + 25){
			movableRight = false;
		}else{
			movableRight = true;
		}
		if(dbSpriteX(index1) > dbSpriteX(index2) + 7){
			movableLeft = false;
		}else{
			movableLeft = true;
		}
		if(dbSpriteY(index1) + 150 < dbSpriteY(index2) + 34){
			movableDown = false;
		}else{
			movableDown = true;
		}
		if(dbSpriteY(index1) > dbSpriteY(index2) + 2){
			movableUp = false;
		}else{
			movableUp = true;
		}
	
    return true;
}
void DarkGDK ( void )
{

	for(int i=1; i<blockLimit+1; i++){
		chance = dbRnd(dbTimer());
		if(chance % 100 <= 25){
			setX += 90;
		}
		if(chance % 100 > 25 && chance % 100 <= 50){
			setY -= 90;
		}
		if(chance % 100 > 50 && chance % 100 <= 75){
			setX -= 90;
		}
		if(chance % 100 > 75 && chance % 100 <= 50){
			setY += 90;
		}
		createBlock(setX, setY, i);
	}
	for(int i=101; i<blockLimit+101; i++){
		chance = dbRnd(dbTimer());
		if(chance % 100 <= 25){
			setX += 100;
		}
		if(chance % 100 > 25 && chance % 100 <= 50){
			setY -= 100;
		}
		if(chance % 100 > 50 && chance % 100 <= 75){
			setX -= 100;
		}
		if(chance % 100 > 75 && chance % 100 <= 50){
			setY += 100;
		}
		createBlock(setX, setY, i);
	}
	// in this application a backdrop is loaded and then several
	// animated sprites are displayed on screen

	// when starting a Dark GDK program it is useful to set global
	// application properties, we begin by turning the sync rate on,
	// this means we control when the screen is updated, we also set
	// the maximum rate to 60 which means the maximum frame rate will
	// be set at 60 frames per second
	dbSyncOn   ( );
	dbSyncRate ( 10 );

	// a call is made to this function so we can stop the GDK from
	// responding to the escape key, we can then add in some code in our
	// main loop so we can control what happens when the escape key is pressed
	dbDisableEscapeKey ( );

	// now we will set the random seed value to the timer, this will
	// help us to get more random values each time we run the program
	dbRandomize ( dbTimer ( ) );

	// we are going to display a backdrop for the scene, to do this
	// we load our image and give it an ID number of 1, this particular
	// image is of a sky at night with stars
	dbLoadImage ( "dungeon.jpg", 1 );

	// the next step is to create a sprite that uses this image, this
	// is achieved by calling dbSprite and passing in a value of 1 for the
	// sprites ID, 0 for the X coordinate, 0 for the Y coordinates and a
	// value of 1 for the image
	dbSprite ( 1, 0, 0, 1 );

	// next we will load in some animated sprites, before doing this
	// we need to adjust the image color key, by using this function we
	// can make a specific color be transparent, in our case we want this
	// to be bright pink
	dbSetImageColorKey ( 254, 254, 254 );

	// in this loop we're going to create some animated sprites, the image
	// we load contains frames of animation for an asteroid
	

	// now we come to our main loop, we call LoopGDK so some internal
	// work can be carried out by the GDK
	while ( LoopGDK ( ) )
	{
		for(int i=0; i<blockLimit; i++){
			drawBlock(placementX[i], placementY[i], i + 4);
		}
		for(int i=0; i<blockLimit; i++){
			if(dbSpriteCollision(i+4, 2)){
				checkBlock(i+4, 2);
			}
		}
		// run a loop through all our sprites
		for ( int i = 2; i < 3; i++ )
		{
			// move the sprite down and play its animation
			// moving from frame 1 to 16 with a delay of 60 ms
			if(delay>0){
				delay--;
			}
			if(delay == 0){
				if (dbUpKey() == 1){
					if(movableUp){
						offsetY += 10;
						delay = 0;
					}
				}
				if(dbLeftKey() == 1){
					if(movableLeft){
						offsetX += 10;
						delay = 0;
					}				}
				if(dbDownKey() == 1){
					if(movableDown){
						offsetY -= 10;
						delay = 0;
					}
				}
				if(dbRightKey() == 1){
					if(movableRight){
						offsetX -= 10;
						delay = 0;
					}
				}
			}
			//dbPlaySprite ( i, 1, 16, 60 );
			for ( int i = 2; i < 3; i++ )
			{
				dbDeleteSprite(i);
				// create an animated sprite and give it the ID number from the
				// variable i, next is the filename, now we come to how many frames
				// across and down, in our case this is 4, finally we come to the image
				// ID that the sprite will use, again we use i
				dbCreateAnimatedSprite ( i, "Image3.bmp", 1, 1, i );

				// position our sprite at a random location
				dbSprite ( i, 300, 300, i );
			}
			// check the position of the sprite, if it has gone off scren
			// then reposition it back to the top
			//if ( dbSpriteY ( i ) > 500 )
			//	dbSprite ( i, dbRnd ( 640 ), -dbRnd ( 1500 ), i );
		}

		// here we check if the escape key has been pressed, when it has
		// we will break out of the loop
		if ( dbEscapeKey ( ) )
			break;

		// here we make a call to update the contents of the screen
		dbSync ( );
	}

	// when the user presses escape the code will break out to this location
	// and we can free up any previously allocated resources
	
	// delete all the sprites
	for ( int i = 1; i < 30; i++ )
		dbDeleteSprite ( i );

	// delete the backdrop image
	dbDeleteImage ( 1 );

	// and now everything is ready to return back to Windows
	return;
}



Is This A Good Question/Topic? 0
  • +

Replies To: C++ Collision Checking in DarkGDK

#2 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 1
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: C++ Collision Checking in DarkGDK

Posted 16 December 2012 - 03:29 PM

No, you do not use collisions to determine if a gameplay rule has been broken. You have seriously violated an unwritten rule of game programming.
Was This Post Helpful? 0
  • +
  • -

#3 jserete  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 16-December 12

Re: C++ Collision Checking in DarkGDK

Posted 16 December 2012 - 07:31 PM

I'm not following... What do you mean? Is there another way?

I guess I'm not really trying to determine whether or not a player's move was legal... I mean that I'm trying to detect whether the player has gone off the path, because everything else is supposed to be a wall...
Sorry for any confusion.
Was This Post Helpful? 0
  • +
  • -

#4 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 1
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: C++ Collision Checking in DarkGDK

Posted 16 December 2012 - 09:15 PM

Okay, there is a subtle difference here. Collisions are used as a way for an object to interact with the game world, it should not in any way be hooked into the gameplay mechanic - in that a gameplay rule should not be broken if a collision is detected.

What I think you are looking to do is that if a region is occupied then you can apply the rules of gameplay as you see fit.

Generally, collision detection is implemented as part of the physics engine and not the gameplay mechanic.

This post has been edited by ButchDean: 16 December 2012 - 09:16 PM

Was This Post Helpful? 0
  • +
  • -

#5 jserete  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 16-December 12

Re: C++ Collision Checking in DarkGDK

Posted 17 December 2012 - 01:29 PM

Yeah, I think I may have confused the two.

But anyway, could you point me in the right direction?
I'm pretty lost in terms of how I should perform the collision detection.
Was This Post Helpful? 0
  • +
  • -

#6 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 1
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: C++ Collision Checking in DarkGDK

Posted 17 December 2012 - 01:59 PM

Have you googled it?
Was This Post Helpful? 0
  • +
  • -

#7 jserete  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 16-December 12

Re: C++ Collision Checking in DarkGDK

Posted 17 December 2012 - 02:19 PM

I mean, how should I detect whether or not the player is still in contact with the rectangles?
Was This Post Helpful? 0
  • +
  • -

#8 jserete  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 16-December 12

Re: C++ Collision Checking in DarkGDK

Posted 17 December 2012 - 02:31 PM

How would I implement it so that it is efficient and accurate?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1