Sign in to follow this  

(C#) Minor collision glich question??

This topic is 4591 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

When doing my player Vs envirotment collision. Sometimes the player sprite runs into colliding tiles and lodges into them slighlty(my the sane amount as its movment increment)... i assumed this was due to handling input before chacking collision.... but i found that handling input after checking collision resulted in no collision detenction. Here is a snippet of my game loop
if (levelInitialised == true)
{
	CurrentTick = System.Environment.TickCount;
	if(CurrentTick > LastUpdate + UpdateFreq)
		{
		LastUpdate = CurrentTick;
		HandleInput();
		if(LastPlayPos != PlayerPos)
		     CollideTest(PlayerPos,ScreenPos);	

		PlayerUpdate(ref PlayerPos);
		TileUpdate(ref ScreenPos);
		LastPlayPos = PlayerPos;
		Draw();	
		}
}


The CollideTest sets bool's like CanGoUp, CanGoDown, etc. HandleInput SHOULD only increment PlayerPos if the directions "CanGo" bool = true TileUpdate makes sure the screen is showing the correct tiles relevant to where the level is scrolled to and makes sure it stays within the bounds of the level. PlayerUpdate makes sure the player sprite is showing the correct animation fram and not out of the level or scren bounds. Ive implimented a work around where the player just bounsed back one movement increment when it collides... but that doesnt seem like a very neat way of doing it. Any help would be great thanks.

Share this post


Link to post
Share on other sites
The way the checking of CollideRange works is that all the collidable tiles are bunched up at the start of the tile file. So each levels XML config file tell the program how many tiles from the front of the file are collidable and this becomes the CollideRange.
So each array element contins an int, "/"ing or "%"ing that int by the tileset width and then "*"ing that by TileSize gives the X,Y co-ord of the needed tile and that tiles position in the TileSet depicts its collision value as described above.


private void CollideTest(Point PlayerPos, Point ScreenPos)
{
int PositionX = PlayerPos.X + ScreenPos.X;
int PositionY = PlayerPos.Y + ScreenPos.Y;
if(PlayerPos.Y % TileSize == 0)
{
if(Level[(PositionX / TileSize)+1, PositionY / TileSize] <= CollideRange)
CanGoRight = false;
else
CanGoRight = true;
if(Level[PositionX / TileSize, PositionY / TileSize] <= CollideRange)
CanGoLeft = false;
else
CanGoLeft = true;
}
else
{
if(Level[(PositionX / TileSize)+1, PositionY / TileSize] <= CollideRange)
CanGoRight = false;
else if(Level[(PositionX / TileSize)+1, (PositionY / TileSize)+1] <= CollideRange)
CanGoRight = false;
else
CanGoRight = true;
if(Level[PositionX / TileSize, PositionY / TileSize] <= CollideRange)
CanGoLeft = false;
else if(Level[PositionX / TileSize, (PositionY / TileSize)+1] <= CollideRange)
CanGoLeft = false;
else
CanGoLeft = true;
}

if(PlayerPos.X % TileSize == 0)
{
if(Level[PositionX / TileSize, (PositionY / TileSize)+1] <= CollideRange)
CanGoDown = false;
else
CanGoDown = true;
if(Level[PositionX / TileSize, (PositionY / TileSize)] <= CollideRange)
CanGoUp = false;
else
CanGoUp = true;
}
else
{
if(Level[PositionX / TileSize, (PositionY / TileSize)+1] <= CollideRange)
CanGoDown = false;
else if(Level[(PositionX / TileSize)+1, (PositionY / TileSize)+1] <= CollideRange)
CanGoDown = false;
else
CanGoDown = true;

if(Level[PositionX / TileSize, (PositionY / TileSize)] <= CollideRange)
CanGoUp = false;
else if(Level[(PositionX / TileSize)+1, (PositionY / TileSize)] <= CollideRange)
CanGoUp = false;
else
CanGoUp = true;
}

}

Share this post


Link to post
Share on other sites
I may have been doing this wrong as i have just had a burst of inspiration... rather than changing the position if it can move..

Maybe i should not change the position but request a change in position and then check that you can move in that direction and if both RequestMoveRight & CanGoRight = true move 1 increment to the right?

Does this sound right? or should i just check then move if CanGo in that direction.

Share this post


Link to post
Share on other sites
Ok ive made the adjustments i mentioned and i can now pin dow the sticking to happening on 2 occasions.

1) When the players left side collides with a tiles right side.
&
2) When the playes top side collides with a tiles bottom side.

I can fix this in the CollisionTest by just minusing 1 from the position checked.... but im hoping to use this in a CV when i look for work after getting my MCAD and dont want a program with makeshift fixes in it.

Share this post


Link to post
Share on other sites

This topic is 4591 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this