Problem with that is, the roads will not all be going in the same direction, so the collision detection needs to be modular. For example, if a collision happened ‘below’ a car (so, at the same x but bigger y), for one car that might mean something is in front of it, but for another it might mean something is to the left of it, depending on the direction the cars are travelling at.
I ended up solving it using geometry.
I find a random point in front of the car to draw an imaginary line that shows the axis (direction) the car is travelling on
pointX = carX + (randomNumber * math.sin(carAngle))
pointY = carY + (randomNumber * math.cos(carAngle))
randomNumber can be anything, it is basically the distance of the point from the car’s reference point. As long as the two randomNumber are the same, it works whatever number you input.
carAngle is the direction the car is looking towards ( 0 for straight down)
Then I use the line drawn by the car’s reference point and the point I just found to find the distance of the pedestrian’s reference point from the axis the car is travelling on.
distance = math.abs(((car.x - pointX)*(pointY - ped.y)) - ((pointX - ped.x)*(car.y - pointY)))/math.sqrt(math.pow((car.x - pointX), 2) + (math.pow((car.y - pointY), 2)))
Then if that distance is bigger than the sum of half the ped’s width and half the car’s width, the ped is in front (or behind) the car
source: http://mathworld.wolfram.com/Point-LineDistance2-Dimensional.html
: ) [import]uid: 106739 topic_id: 19891 reply_id: 77528[/import]