How to make sure a player remains on a path.. ?

Started by
12 comments, last by dantheman1337 14 years ago
Hi, I wanna know if there is a better way to make sure a player remains on the path and does not go astray... one way to go about it would be use vectors conditions, after the x and y coordinates increase, we can check whether the new point lies within the path or not, if it doesnt, we wont increase the particular coordinate, if it lies within, well n good.. but this method will go awfully difficult when there maybe thousands of paths and curves, coding vector conditions for thousand paths might turn out to b a programming hell... the other way would be to check the color of the next pixel.. supposing the path is of white color and the so-called-not-the-path-excluding-the-wall-like-obstacles are of black color, we will check if the next pixel is black, if it is, then we wont allow the point increment, if it isnt, then increment allowed.. but this method will / might b a failure when using many colors, for black mayb the outline of any object n hence the restrictions wont allow the character to cross it especially when the character needs to be over it.. so what is d best way of making sure that a player / character remains on the path specified?? SimpleD
Advertisement
Is this for NPCs?
no this isnt for NPCs..

its like, how / or what type of physics keeps the cars in need for speed from going into the stands n killing people.. what method do they use so that the car remains on d track.. they dont write code for every stand there is to avoid the car going into the stand, do they?

like in real life, we dont bang against the wall, we avoid it.. so in games too, we want the player to avoid every wall there is.. but coding for every wall mayb huge because there mayb thousands of walls, thousands of different codes mayb written, thousands of coordinates, thousands of "if" conditions, making program a thousand times slower..

whats a better n a simpler n a shorter way to go about it.. ?
What you are looking for is collision detection. There are some resources available on the subject on this site, they will get you a long way.
i've been r&ding on collision detection, but isnt collision detection d same deal.. like every time the coordinate of the player changes, we'll have to call a collision_check() function which checks whether there is a collision or not.. to check whether there is a collision, it'll has to check the collision with "all" obstacles..

is this really how every game is made? m really confused.. because its difficult to think that the developers of need for speed wrote pages actually checking whether the car rode off the rode by collision detection method..

is collision detection the answer to my query?
Quote:Original post by SimpleD
to check whether there is a collision, it'll has to check the collision with "all" obstacles..
...All obstacles that are spatially close. Consider using some sort of spatial partitioning system, for example: Imagine overlaying your world with a grid, if you know which grid cell(s) the car is currently occupying then you only need run the collision check with obstacles that are also in that same cell; obstacles in other cells can be safely discarded. You could divide cells into sub-cells too.

Each obstacle would probably also have its own collision volume, such as a box or sphere which is easy and cheap to check. You can very quickly and cheaply eliminate most of the obstacles in the world and only put the effort into the ones that are really close by.
++ to what dmatter said.

Also for the case of something like a racing game, if you have a spline (bezier curve or something like it) representing the center line of the road, and you know how wide the road is, you can just check if the player is less than 1/2 the roads with from the spline. If they are, you know they are on the path, else they are off (and you can handle that case however you want to... push them back on, start ticking down a timer that makes them lose if they get to 0, or whatever you want)
Quote:Original post by dmatter
...All obstacles that are spatially close. Consider using some sort of spatial partitioning system, for example: Imagine overlaying your world with a grid, if you know which grid cell(s) the car is currently occupying then you only need run the collision check with obstacles that are also in that same cell; obstacles in other cells can be safely discarded. You could divide cells into sub-cells too.

Each obstacle would probably also have its own collision volume, such as a box or sphere which is easy and cheap to check. You can very quickly and cheaply eliminate most of the obstacles in the world and only put the effort into the ones that are really close by.


i'll dig into this more.. looks like this might be "the thing" thanks

Quote:Original post by Atrix256
++ to what dmatter said.

Also for the case of something like a racing game, if you have a spline (bezier curve or something like it) representing the center line of the road, and you know how wide the road is, you can just check if the player is less than 1/2 the roads with from the spline. If they are, you know they are on the path, else they are off (and you can handle that case however you want to... push them back on, start ticking down a timer that makes them lose if they get to 0, or whatever you want)


this is indeed a very smart idea.. i'll opt for this idea till i do all the theory behind spatial partitioning system
This is more or less a "Solved Problem". Find and integrate a 3rd party physics engine to do the collision detection for you. Trying to implement it yourself is a nightmare even if you know what you're doing; there's a reason they hire physics PHDs to make all the good physics engines.[smile]

But yes, every frame of the game the new position of all objects in the world are tested for collision and collision is handled.

plus or minus a few thousand other function calls:
while (1) //the game loop. each iteration is a frame{    updateInput();    moveStuff();    handleCollision();    render();}


If you really care about this topic, buy the book "Real-Time Collision Detection". It's amazing.

-me
Yeah that book 'real time collision detection' is seriously the collision detection bible... it's amazing.

The concepts behind things is there if you are curious
The math behind things is there if you are curious
If not, the source code is there for you to copy haha

it's an amazing book

This topic is closed to new replies.

Advertisement