here`s 2d sliding collision what i found on these forums and ported from db pro thanx to who ever wrote it.
#include <DarkGDK.h>
void sliding_2d_collision ( int moveable, int solid );
int x = 0;
int y = 0;
void DarkGDK ( )
{
dbBox( 0, 0, 65, 65 );
dbGetImage( 1, 0, 0, 65, 65 );
dbSprite( 1, 100, 100, 1 );
dbSprite( 2, 0, 0, 1 );
dbSprite( 3, 300, 300, 1 );
while ( LoopGDK ( ) )
{
x += dbRightKey() - dbLeftKey();
y += dbDownKey() - dbUpKey();
dbSprite( 2, x, y, 1 );
if ( dbSpriteCollision( 2, 1 ) ) {
sliding_2d_collision( 1, 2 );
}
if ( dbSpriteCollision( 2, 3 ) ) {
sliding_2d_collision( 2, 3 );
}
x = dbSpriteX( 2 );
y = dbSpriteY( 2 );
dbSync ( );
}
return;
}
void sliding_2d_collision ( int moveable, int solid )
{
int solid_x = 0;
int solid_y = 0;
int solid_width = 0;
int solid_height = 0;
int moveable_x = 0;
int moveable_y = 0;
int moveable_width = 0;
int moveable_height = 0;
int moveable_x_center = 0;
int moveable_y_center = 0;
int mx = 0;
//the heights, widths and coordinates of the sprites are recorded ahead of time for optiminization
//the moveable_center variables are the coordinates of the center of the sprite, not the corner
//mx is used in the algebra that calculates how to resolve the collision
solid_x = dbSpriteX( solid );
solid_y = dbSpriteY( solid );
solid_width = dbSpriteWidth( solid );
solid_height = dbSpriteHeight( solid );
moveable_x = dbSpriteX( moveable );
moveable_y = dbSpriteY( moveable );
moveable_width = dbSpriteWidth( moveable );
moveable_height = dbSpriteHeight( moveable );
// moveable center is relative to the soild coordinates
moveable_x_center = ( moveable_x + moveable_width / 2 ) - solid_x;
moveable_y_center = ( moveable_y + moveable_height / 2 ) - solid_y;
// this is part of the algebraic equations, (solid_height / solid_width) is m, the slope, and moveable_x_center is the x
mx = ( solid_height / solid_width ) * moveable_x_center;
// the algebra creates two imaginary lines through the soild sprite, this divides the sprite into four parts.
// Two inequalities, y > -mx + b (b is the height of the solid sprite) and y > mx are the equations for these two lines
// it detects which quadrant the center is in by creating a linear inequality. The equations of the two lines are calculated,
// and the x and y coordinates of the moveable sprite are plugged in to test if the sprite is below or above the line
// Then for example, if the center of the moveable sprite is in the lower quadrant, the sprite will be pushed downward to resolve the collision
if ( moveable_y_center > -mx + solid_height ) {
// sprite is in lower right half
if ( moveable_y_center > mx ) {
// sprite is in lower quarter
// the correct new y position for the sprite is simply the bottom of the solid sprite, sprite y + sprite height
dbSprite( moveable, moveable_x, solid_y + solid_height, dbSpriteImage( moveable ) );
}
else {
// sprite is in right quarter
// the correct new x position for the sprite is simply the right of the solid sprite, sprite x + sprite width
dbSprite( moveable, solid_x + solid_width, moveable_y, dbSpriteImage( moveable ) );
}
}
else {
// sprite is in upper left half
if ( moveable_y_center > mx ) {
// sprite is in left quarter
// the correct new x position is just enough away from the x position of the solid sprite
// if we subtract the width of the moveable sprite from the coordinate, the other side will barely touch, resloving the collision
dbSprite( moveable, solid_x - moveable_width, moveable_y, dbSpriteImage( moveable ) );
}
else {
// sprite is in upper quarter
// the correct new y position is just enough away from the y position of the solid sprite
// if we subtract the height of the moveable sprite from the coordinate, the other side will barely touch, resloving the collision
dbSprite( moveable, moveable_x, solid_y - moveable_height, dbSpriteImage( moveable ) );
}
}
}
toshiba satellite 1.6 core duo + nvidia geforce go 7300
windows vista ultimate.