hello dear users..

i am working on a simple 2d game like DANGEROUS DAVE (if anybody played he will know better)... i was working fine although i am new to openGl and glut... now there is a problem that i want my character ( in fact a bmp image) when collide with a rectangle shape it falls back to ground under gravity.. i didn't get any helpful resource yet.. could anybody help me please as i have to submit my assignment really soon..
if anybody want more explanation or my code i am ready for that..
thanks in advance...!
waiting anxiously for your kind replies...!

I am not sure how detailed you want to do things and how organized your game is. But here is what I think.

Well, you need first something like an Collision Manager. A class that keeps track of all positions of textures within your visible range. All positions are stored here as references to the original positions. You can do it with arrays or if you want to use carefully pointers instead, you can use vectors.

The positions are looped through and checked if your characters position is near and if a collision occured. That is called a bounding box collision and basically you check if the dimensions of your bitmap overlapps with the dimensions of other objects.

Once a collision is detected, it creates an event your Event Management class. Something like

if (Collision(*textureSizeVector[i], *collisionVector[i], Character->Position(), Character->Size()) EventManager->CreateEvent(COLLISION_OCCURED);

now once you have the proper event flagged. Your event Manager starts all follow up events that are necessary when a collision occured. There are:

1. Take away controll of your character from player to computer
2. You need to deviate the cordinates of your character
3. You need to start a gravitydeviation in vetical direction
4. You need to flag when the event is finished

First you need to hand over controll of your character to the computer. That means your EventManager flags your MovementClass, that movement is OFF.

Secondly then witin your GLUT displayfunc, you need something like:

if (EventManager->CheckEvents()==NOEVENT)
        glTranslatef(this->itsPosition.X, this->itsPosition.Y,0); // move the ship to the set coordinates

if (EventManager->CheckEvent()==COLLISION_OCCURED)
glTranslatef(this->itsPosition.X=itsPosition.X, this->itsPosition.Y-gravitydeviation,0);

Thirdly, gravity was originally initialized to zero but your Event Manager starts to increment it. Increment is a formula from physics:

Character.gravitydeviation = 0.5*modifier*Frame*Frame

whereas a modifier depends on your game setting and units etc. and Frame is simply everytime the function is called within your gameloop. Since frame is squared it will accelerate your character to the ground.

Then if gravitydeviation is larger than the charactersdistance to the bottom screen, either scrolling starts or the character dies. Thus the collision event is flagged as off and you may start a new event depending on your preferences. I do not know the game, but I am pretty sure, that handling events this way are a good way to start. In order to move the character due to gravity is simple, and just a square function. Hope that helps you a little,



Thanks alot poliet it helps alot...but as m not very good with vectors so i have to do it in simple way..but still thanks alot for your kindness..