Public Group

# Motion on a grid and collision

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

## Recommended Posts

I am working on a grid-based game, can you tell about the concept of motion and collision on a grid.

I used to a 2d array to hold info of the grid. I have a concept on collision checking but I am really lost about the concept and motion and animation on grid.

My concept of collision checking invokes a 2d array keeping record all the objects on the grid, while an object moves the array would be updated accordingly and if an object tries to move to a position which already holds an object, it would detect a collision. Wouldn't this type of collision checking work? Is there an easier and better way to do it?

##### Share on other sites
Collision doesn't need to be done using a grid. It can though, and will work, but it doesn't need to. If you DO do it this way, you'll be limiting what types of collision you can have in your game if you don't specify any others anywhere else. You can check collision by just using a 'radius' variable or something similar, then checking to see if the distance between the player and a collisional object <= player's radius + collisional object radius.

Pathfinding needs some sort of grid to create a path.

In a 2d game, motion and animation are pretty simple. Animations are typically done using sprite sheets. Motion is simple, just make your Player object have a starting position, then update that position every frame when the user presses the appropriate input.

##### Share on other sites
If you want strict grid movement, as in a tile moviment, its pretty easy. If you want free movement with the grid just serving as reference for pathfinding and mapping its a little more work but still not terribly hard. First we have to know what kind of movement you want. But anyways, animation will work the same way.

Motion is simple both ways, only collision avoidance and pathfinding will be a little harder. If you're using a tile engine, I would keep current position and future position, as in, your character is moving from one tile to the next one, so both are unwalkable. But I would like to hear a little more about what kind of movement are you looking for. Fully tile based or free movement.

fully tile based

##### Share on other sites

I am working on a grid-based game, can you tell about the concept of motion and collision on a grid.

I used to a 2d array to hold info of the grid. I have a concept on collision checking but I am really lost about the concept and motion and animation on grid.

My concept of collision checking invokes a 2d array keeping record all the objects on the grid, while an object moves the array would be updated accordingly and if an object tries to move to a position which already holds an object, it would detect a collision. Wouldn't this type of collision checking work? Is there an easier and better way to do it?

Ok, as far as motion goes, its pretty simple. This is the way I do it. My movement and animation work based on directions, N S E W NE SW NW SE. These directions are direction vectors E (1, 0), W (-1, 0), etc...

When my character is moving to the right, the movement function receives a movement vector, multiplied by a fixed movement rate (something like the character speed), and checks if distance to the next tile is smaller than the total distance to the center of the next tile. If it is, it moves to the center instead of the whole amount.

Simply put, you will move based on a vector, the same way you would freely move your character, except that you now will keep stored an array of tiles, as waypoints (or if youre moving a single tile, exemple, you press the right arrow key, you can get the tile at your character's right, and just keep this position as your current waypoint, and at every movement update you check:

Are we there yet?
No
So in which direction do I have to go?
Right. So keep moving right until you are close enough to the center of the next tile.

As far as animating goes, I call my animation method (which updates my spritesheet based on a timer, in the direction the character is facing) everytime I call the move method.

This is a little example, (Typed it right here, so sorry for any mistakes)

 public void UpdateMovement() { if(Destination != null) { Vector2 movementDirection = GetDirectionToPosition(Destination) Move(movementDirection); } } public void Move(Vector2 direction) { CharSprite.Animate(Facing); if (movementTimer >= movementInterval) { if (direction != Vector2.Zero) { Vector2 movement = direction * Speed * stepSize; if(movement > Destination - Position) Position = Destination; else Position += movement; } movementTimer = 0f; } } 

If you have any specific question I would love to help, just have to be sure of what exactly do you need help with.

1. 1
2. 2
Rutin
19
3. 3
4. 4
5. 5

• 9
• 9
• 9
• 14
• 12
• ### Forum Statistics

• Total Topics
633291
• Total Posts
3011234
• ### Who's Online (See full list)

There are no registered users currently online

×