views:

295

answers:

2

I think the title is rather self explanatory but just to clarify I am trying to figure out how to tell which side the collision has occured on.

For a bit more detail, I'm trying to make a maze-like game so I can't simply stop all movement upon a collision. Instead I need to be able to tell which side the collision has happened on so I can block that direction.

Any help is appreciated and if there is a better approach to this issue, I'm all for trying it out.

I hope this is enough details but if you need anymore, ask and I'll edit. Thanks in advance.

[edit] @viggity - No, I'm not using any specific game engine and I would post the current "detection" code but it's a little, absurdly, robust.

@Streklin - I'm using the this.Paint event to draw onto the form itself as it was recommended I start by doing that to get better at drawing real time. I'm also using a location that's updated each time the timer ticks based on what I press (left, right, up, down). Yes the maze is tile based. Currently it only consists of 3 colors even. I'm not a very advanced programmer.

@Eric - Definately a one-d game. Again, I only have 3 colors, the lines are black, the background is white and the square (the user) is green. I'm using the DrawImage() with Bitmaps to draw onto the screen.

[edit psuedo-code summary]

foreach(Wall _wall in walls) if(player.intersectsWith(_wall)) stop movement;

@JeffH - I'm not really sure what you're asking as that's pretty much all there is besides testing code that I was using to try and get it working. The only thing I left out was the if statement to check if it was the x axis or not so that x and y could move indepedently from each other. So instead of getting "stuck" because you touched the wall, you could slide against it. I didn't see the point in including that though since the problem occurs before that.

+4  A: 

Assuming you're talking about a 3D game here.

The normal of the face you can see points towards you, so the dot product of your direction vector with the face normal will be negative. If it's positive then you are coming at the face from the back.

If it's zero you're travelling at right angles to the face.

|              <---------- your direction of travel
|
|----------> <- face normal
|
| <- face

If you're not in 3D then you could store the direction the wall is facing (as a 2D vector) and do the same dot product with your 2D direction of movement.

ChrisF
A: 

Based on your edit you can only go one direction at a time? Or can you go in diagonal directions? If it's the later, ChrisF has provided you the answer in 3D and the corresponding information for 2D. If not, you should just have to stop travel in the direction of travel - since there are only four possibilities it should be easy enough to check them all for simple starter game.

Streklin