[SOLVED] One way collision

Is it possible to have a one way collision?

What do I mean by one way collision:

The object hits the collision from one side, but it can go through the collision from other sides.

In a racing game, I need to prevent the player from going in the wrong direction.

What exactly do you mean? Is this a 2D top down or 3D? Where are the objects? Is it like a checkpoint system or something else? What exactly will happen if it comes from the wrong side? Will it stop or turn around?

I want to implement the one-way collision in a 2D top-down perspective. When an object collides with the right side of the collision, for example, it won’t be able to pass. But when colliding with any side other than the right side of the collusion, it will be able to pass through. Just like there were no collision.

I’m clueless on this.

This formula would give you the angle difference between the car and an object I named wall. The value will be below zero for 1 side and negative for the other side.

AngleDifference(Wall.Angle(), Wall.AngleToObject(Car))

Modified, this would give the difference between their angles. So, you could tell if the car was going in the same direction as the wall object.

AngleDifference(Wall.Angle(), Car. Angle())

One of these may help you decide which side the car is on. You could pick the nearest wall or checkpoint object or you could use a collision. What you do from there is up to. It depends on how you’re moving your car.

The first implementation would only work if the car is at the same exact angle as the wall.
I didn’t understand the second implementation.
Any other solutions?

It’s been awhile. I think the angle difference idea would be used with say is less than a certain degree. IDK if the number could be a negative. If it could, you could use abs().

I still didnt understand

It’s been awhile and I’m not sure I recall myself and I don’t currently have the time to test things. If something comes to me. I’ll post it.

Maybe someone else has a better idea.

In most racing games this is simply made by checkpoints

EVEN if they are not visible in games
And they are spammed over tracks

Imagine you have checkpoints 1 2 3 4 5 and so gon up to let’s say 100

They are in as much distance from each other to make sense on curves of your track
More curved parts have smaller distances between checkpoints
Where more straight parts have only one at beginning and one end

Now you do not check all checkpoints
You check only distance from checkpoint that is next to front of player and one that is behind player
Where now check if distance between previous checkpoint and one in front of player is decreasing by comparing it against distance between player and next checkpoint
IF player is accelerating (so it is possible to drive in reverse and don’t see WRONG WAY message) then that value was lower and now it gets higher
It means player is going wrong way

You would only need to learn how to create such logic for 4 points
To be able to check only 2 at the time while also be able to pass on which is next and which is previous
And then you would simply duplicate same logic for how many more checkpoints you have

Where i am not into racing games and that is only method i know

Thanks for the reply related to my game, but I need a solution that doesn’t require placing checkpoints or objects to check if the player is going the wrong way or not, I simply need a one way collision.

How are the cars following the track or route?

What do you mean?
The cars race on a multi path track.

I think Keith asks how your AI cars are following track path in right direction

Using nodes or also known as breadcrumbs.
The bot follows these by order (stored in object variable called id), if there are multiple instances of a breadcrumb with the same ID (order), the bot will do a ray cast and pick the one with no obstacles.

How is this related to my question?

If they’re following nodes then what if you number them and keep track of the highest number. If a car interacts with a node that is less than the highest node then it’s going in the wrong direction. You could add a little tolerance by checking if the nearest node if a few numbers less than the highest reached node.

If you’re not using a lot of nodes then maybe use node ID plus distance or direction or whether the distance is increasing or decreasing. Compare the next and the previous node.

I have used this method, but what about other people looking for a solution to this problem for a non-racing game?

Bumping this topic, i want to see answers.

Do you have any ideas? Maybe someone can help you implement them.

I tried using multiple objects and tracking which object was collided with first. It sort of worked.

I tried using a sprite with a point on each side. I thought it could be used with a formula. I couldn’t find the right method.

I had two ideas one that would work if the player and the collision were on the same angle and the other one It had a fault that it would allow from 2 sides.
Currently I don’t have the implementation because the paper that I wrote these ideas has been thrown to the trash.

I give up. Tried brainstorming ideas but all of them fail.

Only got a solution by @ZeroX4,
which isn’t for collisions, but for detecting if the player is going in wrong direction.

@Keith_1357 suggested and have tried with me, yet we couldn’t figure it out.

Ill change this topic to “detecting wrong way in racing game”, so developers searching this forum can see this topic, as it has a nice solution for that problem.

1 Like