Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Dark GDK / collision

Author
Message
death metal black metal
13
Years of Service
User Offline
Joined: 26th Oct 2010
Location:
Posted: 27th Oct 2010 04:05
Sorry for the long post.

I am working on a 2d game where you land a rocket ship on a landing pad. I am having trouble getting the ship to look as though it has landed on the pad. Once the ship has landed, a message will display a congrats, to let you know that you have landed. As of right now, when the ship is supposed to land on the pad, the ship slides right through it. I have the dbSpriteCollision set to have both my spaceship and landing pad to detect the collision. I just can't figure out how to get ship to land on the pad. I had set the dbSprite to reset the ship position to the landing pad, however, when the two sprites collide, it jumps to the pad and of course, looks really crappy.

Not sure if you need to know this, but I have the controls set up, so that the spacebar raises the ship. You also have to keep the spacebar held and press either the right or left key, to move the ship right/left. When you let go of the spacebar, the ship automatically descends back to the ground.

If you need the code, please let me know, I will post it asap.
Mireben
16
Years of Service
User Offline
Joined: 5th Aug 2008
Location:
Posted: 27th Oct 2010 22:40 Edited at: 27th Oct 2010 22:41
Yes, I think we'll need the code because without it, it's not possible to figure out where the problem is. Based only on your description, I have trouble even to imagine what exactly is happening on the screen.
death metal black metal
13
Years of Service
User Offline
Joined: 26th Oct 2010
Location:
Posted: 28th Oct 2010 03:36

// This program allows the user to move a sprite
// with the arrow keys.
#include "DarkGDK.h"

// Constant for the refresh rate.
const int REFRESH_RATE = 60;

// The following constants are for the image numbers
// and sprite numbers used in this program.
const int PLANET_IMAGE_NUMBER = 1;
const int PLANET_SPRITE_NUMBER = 1;
const int SPACESHIP_IMAGE_NUMBER = 2;
const int SPACESHIP_SPRITE_NUMBER = 2;
const int LANDING_PLATFORM_IMAGE_NUMBER = 3;
const int LANDING_PLATFORM_SPRITE_NUMBER = 3;

// Constants for the UFO's starting
// XY coordinates
const int SPACESHIP_STARTING_X = 400;
const int SPACESHIP_STARTING_Y = 350;

// Constants for the landing platform.
const int PLATFORM_POSITION_X = 50;
const int PLATFORM_POSITION_Y = 320;

// Function prototypes
void createSprites();
void getSPACESHIPcoordinates(int &, int &);
void detectCollision(int, int);

//**********************************************
// The DarkGDK function *
//**********************************************
void DarkGDK()
{
// Variables for the SPACESHIP's x and y coordinates.
int spaceshipX = SPACESHIP_STARTING_X;
int spaceshipY = SPACESHIP_STARTING_Y;

// Variables for the Landing Platform x and y coordinates.
int platformX = PLATFORM_POSITION_X;
int platformY = PLATFORM_POSITION_Y;

dbSyncOn(); // Disable auto refresh
dbSyncRate(REFRESH_RATE); // Set the refresh rate

// Create the sprites.
createSprites();

// Game loop
while ( LoopGDK() )
{
// Get the coordinates for the SPACESHIP.
// Note that spaceshipX and spaceshipY are passed
// by reference. After the function call
// they will contain the new coordinates
// for the SPACESHIP.
getSPACESHIPcoordinates(spaceshipX, spaceshipY);

// Display the SPACESHIP at its new location.
dbSprite(SPACESHIP_SPRITE_NUMBER, spaceshipX, spaceshipY, SPACESHIP_IMAGE_NUMBER);

// Check for collision.
detectCollision(spaceshipX, spaceshipY);

// Refresh the screen.
dbSync();
}
}

//***********************************************
// The createSprites function creates the PLANET*
// and SPACESHIP sprites. *
//***********************************************
void createSprites()
{
// Set the key color to white.
dbSetImageColorKey(255, 255, 255);

// Load the planet.bmp, alienspaceship.bmp, landingarea.bmp images.
dbLoadImage("planet.bmp", PLANET_IMAGE_NUMBER);
dbLoadImage("alienspaceship.bmp", SPACESHIP_IMAGE_NUMBER);
dbLoadImage("landingarea.bmp", LANDING_PLATFORM_IMAGE_NUMBER);

// Create the PLANET sprite. Because this is used
// as the background, we will display it at (0, 0).
dbSprite(PLANET_SPRITE_NUMBER, 0, 0, PLANET_IMAGE_NUMBER);

// Create the SPACESHIP sprite and display it at its
// starting coordinates.
dbSprite(SPACESHIP_SPRITE_NUMBER, SPACESHIP_STARTING_X,
SPACESHIP_STARTING_Y, SPACESHIP_IMAGE_NUMBER);

// Create the Landing Zone sprite and display it at its coordinates.
dbSprite(LANDING_PLATFORM_SPRITE_NUMBER, PLATFORM_POSITION_X,
PLATFORM_POSITION_Y, LANDING_PLATFORM_IMAGE_NUMBER);
}

//****************************************************
// The getSPACESHIPcoordinates accepts two arguments *
// by reference. These arguments are for the *
// SPACESHIP's X and Y coordinates. The function *
// determines whether the user is pressing an *
// arrow key and updates these values *
// accordingly. *
//****************************************************
void getSPACESHIPcoordinates(int &x, int &y)
{
if ( dbSpaceKey() && (dbRightKey()))
{
y--;
x++;
}
if (dbSpaceKey() && (!dbRightKey()))
{
y--;
}
if (dbSpaceKey() && (dbLeftKey()))
{
y--;
x--;
}
if (dbSpaceKey() && (!dbLeftKey()))
{
y--;
}
else
{
if (y < 350)
y++;
}
}
//***************************************************
// The detectCollision function determines whether *
// the spaceship sprite collides with the landing *
// area. If so, then a message will congratulate *
// the user. *
//***************************************************
void detectCollision(int x, int y)
{
if (dbSpriteCollision(SPACESHIP_SPRITE_NUMBER, LANDING_PLATFORM_SPRITE_NUMBER))
{
// Congratulate the user for landing.
if (x == 50 && x == 255 && y > 200)
dbPrint("Congratulations!");
}
}
Mireben
16
Years of Service
User Offline
Joined: 5th Aug 2008
Location:
Posted: 28th Oct 2010 21:18
Next time please use the code tags (press the "code" button before and after pasting the program code) to collapse your code under a nice pushbutton. It shortens the post and also preserves the format of the program text.

I have a suggestion how to land your spaceship. The first problem is that, when the collision is detected, you have a condition which can never be true. The x value will never be equal to 50 and 255 at the same time. If you want to check if X is between certain values, then do this:



The second problem is that you don't stop moving the spaceship when a collision happens. When this "if" condition is corrected, the "congratulations" text will scroll over the screen but the spaceship can still move and, as you said, slide through the platform. I suggest to introduce a status variable which shows whether the spaceship landed or not. If it landed, then don't move it any more. You can modify the collision detection function to return a boolean value which means whether the two sprites collided or not (with or without an extra coordinate check). Example:



Then, in the main loop, use the status variable to decide whether the spaceship can still move or not. If it has not landed yet, then move it. If it has landed, then don't move but display the congratulations text. I have also changed dbPrint to dbText because dbPrint changes the cursor position so the message will be printed on the next line every loop. With dbText you can specify the position where the text should be. Example:



This will solve the problem. I have one more advice: the function which sets the coordinates of the moving spaceship is a little overcomplicated. It can be written shorter:

death metal black metal
13
Years of Service
User Offline
Joined: 26th Oct 2010
Location:
Posted: 29th Oct 2010 04:44
Yeah, sorry for the ugly post, I accidently deleted the closing code tag.

Thanks, that makes a lot of sense. I'm kinda new to all this, but I think I am getting the hang of it. Again, thank you for your help, and sorry for posting in the wrong format.
Timidon
19
Years of Service
User Offline
Joined: 26th Jun 2005
Location: Bakersfield, Ca. USA.
Posted: 29th Oct 2010 18:02
Mireben, Question for you.

What is the advantage of this function header?

"void getSPACESHIPcoordinates(int &x, int &y)"

vs

"void getSPACESHIPcoordinates(int x, int y)"

What is the "&" symbol used for here?

There are many answers but just one question" ~ Jerilith the Mad
Matty H
15
Years of Service
User Offline
Joined: 7th Oct 2008
Location: England
Posted: 29th Oct 2010 18:52
Quote: ""void getSPACESHIPcoordinates(int &x, int &y)"

vs

"void getSPACESHIPcoordinates(int x, int y)""



The second makes copies of x and y and uses the copies inside the function, meaning you can't change the original values.
The first passes in the actual variables(addresses), meaning the function can alter x and y.

The second wont actually work if it is intended to get the spaceship coordinates.

Login to post a reply

Server time is: 2024-09-28 16:28:11
Your offset time is: 2024-09-28 16:28:11