First of all, you shouldn't be updating things in your draw function. That's bad programming practice.
Second, I see absolutely no collision code in there.
You're defining rectangles ('rec'). You have to see if they intersect each other. If they intersect each other, than you disallow whatever movement you don't want.
First of all, you shouldn't be updating things in your draw function. That's bad programming practice.
Second, I see absolutely no collision code in there.
You're defining rectangles ('rec'). You have to see if they intersect each other. If they intersect each other, than you disallow whatever movement you don't want.
Sorry
I have tried the intersection code, however my index for obstacles in another position.
Code:
public void CalculateCollisions(GameTime gameTime, Grid grid, Sprite sprite)
{
//sprite.MoveChar(gameTime, grid);
//sprite.AllowMove = false;
//int idx = GetIndex(pt, grid, sprite);
int x = 0;
int y = 0;
int index = 0;
int idx = GetIndex(grid, sprite);
sprite.Y = grid.rec.Top;
for (int row = 0; row < grid.Rows; row++)
{
y = row * Grid.CellHeight;
for(int col = 0; col < grid.Columns; col++)
{
x = col * Grid.CellWidth;
index = row * grid.Columns + col;
if (grid[index] == 1)
{
}
if (grid[index] == 2)
{
if ((grid.rec.Intersects(sprite.rec) && sprite.rec.Left >= grid.rec.Right))
{
sprite.AllowMoveRight = false;
//sprite.X = grid.rec.Right;
//sprite.X = grid.rec.Right;
sprite.X = 0;
sprite.Y = 0;
}
//if (sprite.rec.X >= x)
//{
// sprite.rec.X = grid.rec.X - sprite.Width - Grid.CellWidth;
// sprite.AllowMoveRight = false;
//}
if (sprite.rec.Bottom >= grid.rec.Top - sprite.rec.Height - Grid.CellHeight)
{
sprite.AllowMove = false;
sprite.Y = grid.rec.Top;
sprite.rec.Y = grid.rec.Top;
if (sprite.rec.Bottom < grid.rec.Top)
{
sprite.AllowMove = true;
}
//grid.Y = sprite.rec.Bottom;
}
//sprite.MoveChar(gameTime, grid);
//sprite.Position.Y = (int)grid.location.Y - Grid.CellHeight * 2 - sprite.Height;
}
}
}
}
Here is the collisions I tried to do, they do not function.
I have not had a real chance to look at your code thoroughly, but from the looks of it, you are trying to test collisions from several directions in the same method. Now, I am very new to collision programming, as well, but i would separate this method into several smaller methods that check specific directions of collision, like checking to see if it can move to the right, or if it can move down. That way, if you have a function that just needed to know if it can move right, then you can call the method to check if it can move right. Also, when programming for collision, there are two main things that need to be programmed, which is to check if anything is colliding, and to correct the collision. It would be best if you separated this method into two methods, one that calculates the collisions, and one that corrects the sprite's movements. I think then you will be able to see and visualize the code a lot easier, and then you can change one method without having to rewrite everything. By the way, what game is this for?
I have not had a real chance to look at your code thoroughly, but from the looks of it, you are trying to test collisions from several directions in the same method. Now, I am very new to collision programming, as well, but i would separate this method into several smaller methods that check specific directions of collision, like checking to see if it can move to the right, or if it can move down. That way, if you have a function that just needed to know if it can move right, then you can call the method to check if it can move right. Also, when programming for collision, there are two main things that need to be programmed, which is to check if anything is colliding, and to correct the collision. It would be best if you separated this method into two methods, one that calculates the collisions, and one that corrects the sprite's movements. I think then you will be able to see and visualize the code a lot easier, and then you can change one method without having to rewrite everything. By the way, what game is this for?
Thanks for your tips, or help. It is on a game I am working on albeit incredibly slowly, a platformer. Now that summer is starting, perhaps I will make more progress.
I have yet to make any platformer games, but it sounds like it would be fun. Mostly I deal with arcade games and top down shooter games, so there is limited collision in dealing with gravity and advanced physics, but I am glad I was able to help. I learned all of that just from experience, and if you keep working at it, you will continue to grow as a good programmer.