Detecting where collision happened

Hello,

What I want for my game is to have cars that detect which ‘part’ of them was hit, so that if a pedestrian hits them from the front, the cars stop, but if the pedestrian collides with them from the sides, the cars carry on.

How can I do that? I looked into complex body construction and sensors, but I;m not sure how to use either, and I get the feeling there is an easier way to do this. [import]uid: 106739 topic_id: 19891 reply_id: 319891[/import]

Make sure the collision is set as an event, eg;

[lua]local function mycollision (event)[/lua]

Then use event.x and event.y.

Peach :slight_smile: [import]uid: 52491 topic_id: 19891 reply_id: 77322[/import]

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]

The quick and dirty is something like this, which I’m using
you have the body of an object1 with a listener on it (the whole body)

then you have object2 with a listener on it. You would weld this piece onto the front of the car, and put a separate listener on that.

basically, you just weld additional bodies on whatever it is and apply listeners to those, rather than trying to mess around with angles and math. The trade off is more physics objects, but I found it to be pretty speedy, I’m nailing 60 fps in my projects.

If you need an example, I could cook one up but its like do a rect, and then weld another one on the front and apply listeners separately to each body.

So 2 bodies, 2 listeners done deal. works great for me :slight_smile:

ng [import]uid: 61600 topic_id: 19891 reply_id: 77622[/import]

That’s what I used to have, until I decided I would end up with too many physics objects ( I will have something like 20 cars on screen, and each on of them needs at least 2 extra sensors of the kind you described ) [import]uid: 106739 topic_id: 19891 reply_id: 77738[/import]

@PerturbedMollusk

Ahh, ok I understand.

Well, I can tell you that I have 1000’s of vertices and very complex curves etc, with many weld points and still get 55+fps, and 50ish on 3gs, 55 on ipod4g and pretty close to 60 on ipad2.
I remember there was a post on if you had a display.newRect , you could detect what side a collision is on. It exists, I just can’t remember where it is, I tried searching on it but still not coming up with anything.
I’ll keep an eye out :slight_smile:

ng [import]uid: 61600 topic_id: 19891 reply_id: 77762[/import]