Skip to main content
gamedev.net
🔒 Locked

collision problem

Started by hello_there 4 replies 200+ views
Original Post
hello_there
hello_there
with my collision detection it check for keys presses if say left is presses it maves the player left then if it detects a collision it move the player back right. but sometimes the player ends up in the wall still and i don''t know why. the collision detection is alright. anyone know why or do you need to see the code?
____________How could hell be worse?
AeroBLASTER
AeroBLASTER
that''s not really specific enough, showing the code would be good, but also explain a little better. What are you using? Bounding boxes? what? If i''m understanding this right it might be that you''re just trying to have it collide w/ origin, which will obviously put it inside the thing you''re colliding with.

Just a thought, show us the code and explain better
---DirectX gives me a headache.
hello_there
hello_there
oh yeah i'm using bounding boxes. i had this collision stuff working before but my harddrive blew up so i had to make it up for a early copy i made. here's the code you mightn't understand my crazy code but anyway. tell me anything else you don't follow with the code.

void move(float time)
{
int numx=0;
int numy=0;
int numz=0;
int nx=0;
int ny=0;
int nz=0;
double R=3.141593/180.0f; //radians to degrees
float distance=0;
float xdist=0;
float ydist=0;

bool left=false;
bool right=false;
bool up=false;
bool down=false;
bool collision=false;

float oldposx=0;
float oldposy=0;
float oldposz=0;

oldposx = plone.playerposx;//plone.playerposx is the players x position an so on
oldposy = plone.playerposy;
oldposz = plone.playerposz;

if(keydown(VK_LEFT))
{
//it's only 2d but it's going to be 3d later and it's like gta 1 camera.

plone.playerposx=float(plone.playerposx+cos(-camerar*R)*-2.0*time);
plone.playerposy=float(plone.playerposy+sin(-camerar*R)*-2.0*time);
left=true;
plone.angle=-camerar+90;//rotates player so it look like they're going left
}
if(keydown(VK_RIGHT))
{
plone.playerposx=float(plone.playerposx-cos(-camerar*R)*-2.0*time);
plone.playerposy=float(plone.playerposy-sin(-camerar*R)*-2.0*time);
right=true;
plone.angle=-camerar-90;//rotates player so it look like they're going right
}
if(keydown(VK_UP))
{
plone.playerposx=float(plone.playerposx-sin(-camerar*R)*2.0*time);
plone.playerposy=float(plone.playerposy+cos(-camerar*R)*2.0*time);
up=true;
plone.angle=-camerar+0;//rotates player so it look like they're going up
}
if(keydown(VK_DOWN))
{
plone.playerposx=float(plone.playerposx-sin(-camerar*R)*-2.0*time);
plone.playerposy=float(plone.playerposy+cos(-camerar*R)*-2.0*time);
down=true;
plone.angle=-camerar+180;//rotates player so it look like they're going down
}

while(1)
{
numx=levelone.colx[nx];//colx[nx] is the position of the wall
numy=levelone.coly[ny];
numz=levelone.colz[nz];

xdist=poscoordx[numx]-plone.playerposx;//poscoordx[] is an array of positions. since colx[nx] is and int poscoordx[] gives it a screen position.
ydist=poscoordy[numy]-plone.playerposy;

distance = float(sqrt((xdist*xdist)+(ydist*ydist)));

if(distance < 5.0f)
{
if(numz == plone.height || numz+1 == plone.height)
{
if(
(plone.boundingplayer[0][0] <= poscoordx[numx]+0.2f &&
plone.boundingplayer[1][0] >= poscoordx[numx]-0.2f)&&
(plone.boundingplayer[0][1] <= poscoordy[numy]+0.2f &&
plone.boundingplayer[1][1] >= poscoordy[numy]-0.2f)&&
(plone.boundingplayer[0][2] <= poscoordz[numz]+0.2f &&
plone.boundingplayer[1][2] >= poscoordz[numz]-0.2f)
)
{
if(left)
{
plone.playerposx=float(plone.playerposx-cos(-camerar*R)*-2.0*time);
plone.playerposy=float(plone.playerposy-sin(-camerar*R)*-2.0*time);
}

if(right)
{
plone.playerposx=float(plone.playerposx+cos(-camerar*R)*-2.0*time);
plone.playerposy=float(plone.playerposy+sin(-camerar*R)*-2.0*time);
}

if(up)
{
plone.playerposx=float(plone.playerposx-sin(-camerar*R)*-2.0*time);
plone.playerposy=float(plone.playerposy+cos(-camerar*R)*-2.0*time);
}

if(down)
{
plone.playerposx=float(plone.playerposx-sin(-camerar*R)*2.0*time);
plone.playerposy=float(plone.playerposy+cos(-camerar*R)*2.0*time);

}
}

}

}

nx++;
ny++;
nz++;

if(nx >= 850)
{break;}
}


[edited by - hello_there on August 10, 2002 11:30:14 PM]
____________How could hell be worse?
AeroBLASTER
AeroBLASTER
well i did hear something about the speed of movement VS time to check for collisions screwing things up... ie by the time it checks for collision the object is inside the thing its supposed to collide with... not sure though, you''re right about you code being hard to follow (no offense :-P)
---DirectX gives me a headache.
hello_there
hello_there
yeah atleast i can follow it . i was going to tidy it up but i couldn''t be bothered. yeah i know it''s inside but then it moves it back the same distance it came forward so it should be out side again. i might try checking for collisions first then move.
____________How could hell be worse?
AeroBLASTER
AeroBLASTER
maybe set it to check collisions on a timer or something
---DirectX gives me a headache.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.