# How do I check if collision is clockwise or counterclockwise?

## How do I…

I’m making a chain reaction game using a grid of sprockets. When the player clicks a sprocket, the sprocket should rotate 90 degrees clockwise. If that sprocket comes in contact with another sprocket, the two sprockets should collide, and the second sprocket should rotate 90 degrees, and so on.

## What is the expected result

If a sprocket is rotating clockwise, and it contacts another sprocket, it should spin the second sprocket counterclockwise. If a sprocket is rotating counterclockwise, and it contacts another sprocket, it should spin the second sprocket clockwise.

## What is the actual result

Currently, when a sprocket is contacted by another sprocket, it rotates clockwise, regardless of the direction of rotation in which it was hit.

## Related screenshots

Here are my current conditions and action:

I’m pretty sure I need an If/Then in my action to decide whether the sprocket should rotate +90 or -90, but I can’t figure out how to do that.

If someone can tell me how to write the If/Then I need or explain a better way of accomplishing my goal, I would greatly appreciate it.

Thanks.

What about using an object variable to identify the rotation direction? Then when they collide read the variable and rotate accordingly? The variable could be set by the original rotation direction based on + or - angle?

It’s not as simple to do as you are hoping. You want to do this recursively, and I would use a second collision checking sprite to help. Here’s a working example:

Which looks like:

2 Likes

Wow, man, thanks for taking the time to put that together. If I understand correctly, I need to make three variables. I make those global, right?

I’ve only been using GDevelop for a few days now, so I’m still getting the hang of it.

No worries, it only took 15 ins to put together, and it was a nice little exercise to try out

You’ll need only 2 variables - processingSprocket, which is a boolean, and roatation, which is a number. Make them scene variables. There’s no need to carry them over to other scenes.

State is an object variable. And you don’t need to define the variables in the editor. If it doesn’t exist, then GDevelop creates in at run time for you.

1 Like

Awesome. I’ll hit it tonight and see what I can do.

Thanks again.

So, I’ve got all the events in, but I’m doing something wrong because it’s not working. When I click a sprocket, it rotates as expected, but there is no collision when it contacts another sprocket. I have a feeling one of these is the issue:

1. “I would use a second collision checking sprite to help.” ← I’m not sure what you mean here. What image should I use for the sprite, and does it need a collision mask?
2. You have the Checker object being created on the Base Layer. My sprockets are on their own “Sprockets” layer. Should I stick with the creating the Checker on the Base Layer or do it on the Sprockets layer?

Thanks.

1. The second collision checking sprite, the Checker object, is best as a duplicate of the sprocket object. Currently it’s undefined, and probably doesn’t have a collision box.

2.Yes, put your Checker on the same layer as the sprockets. If there’s any camera movement, then putting the Checkers objects anywhere else can become a nightmare.

1 Like

I created a duplicate of my sprocket and set that as the checker to be created on the sprockets layer, but the collisions aren’t being detected.

Here are my events:

And here is what my objects, layers, and game board look like:

Here are my collision masks. I’ve had the collisions working before, so I’m pretty sure the masks aren’t the issue.

(I would have put all of this in a single reply, but I can only upload one image at a time)

I’m not sure about strategy but using 2 different objects would help. I’m thinking every other one in a checkerboard setup. You could use the Create multiple copies of an object extension or a couple of nested repeats

That way no 2 instances of the same object could collide with each other. That would make picking objects a lot easier. You’d need some object variables. One for direction and one for has moved or is moving plus you can check the status of the tweens.

That’s all I got. It sounds interesting. Easy but also complex. If I think of anything more. I’ll share it.

Edit : I don’t fully understand the checker process. That could be easier. It could also be used in combination with alternating objects.

1 Like

Ah, and the collision masks are where the problem will be. Collisions occur if there’s an overlap between collision masks - so they won’t occur between the Checker and Sprocket objects. How you got it working before is mystery, unless the collision masks were different.

Anyway, here’s why the collisions won’t work- the green being your collision masks. There’s no overlap of any of the green areas:

Here is an approximation of my example, the green being the collision mask, and the red being the overlap of checker and sprocket collision masks:

So the upshot is that you’ll need to either modify your sprocket and collision masks, or make the checker larger (say 1.5 times) than the sprocket and have its collision mask circular in shape.

Yeah, that was my understanding too. But we are wrong on this. Checking for collision between 2 instances of the same object results in both those instances being picked. We can’t differentiate which between them for explicit actions on just one of them. So something like:

will result in both sprockets changing angle by 90 degrees. We just can’t do things like separate them, or delete just one of them without further conditions.

It’s something I came up with many moons ago. It’s a recursive process that works like a chain reaction, one part of the chain at a time. Effectively it checks for neighbouring sprockets to the one that’s just turned, and then turns those and checks for their neighbouring sprockets that haven’t been turned and so on.

1 Like

Interesting. It doesn’t matter that the collision masks overlap when a sprocket rotates?

The checking process reminded me of a pipe game I made. I had to search for the logic. It used an array as a buffer. You put the neighboring tiles into the array and then pulled them back to check the tiles around it and put them into the array. You keep putting them in and taking them out until there’s nothing left in the array.

1 Like

No, because all the work determining which sprockets turn and in which direction is done in one frame, when the click is detected. Once the rotation starts, there is no further collision detection done until the next mouse click on a sprocket.

I had assumed sprockets were circular, with teeth like a cog wheel. If you want the collision to be detected mid rotation, then the events I posted will need a rewrite.

That’s interesting way of doing it, and not one I’d considered. I might give that a go next time I use it in a game.

I’ve tried redoing my sprocket and masks so that the masks overlap when at rest, and I end up spinning a dozen sprockets instead of one when I click one.

I tried that, too, with the same results. I’ve been trying to make this game for weeks, starting with Unreal, then Godot, then GDevelop. I’m beginning to think it’s just not meant to be.

When I first posted this thread, I could click a sprocket, it would rotate and collide with other sprockets, and they would turn as well. It was basically working. The only problem was all the sprockets turned clockwise, regardless of the direction they were hit. Is there not a way to use that method and just add some kind of checker to see which direction the sprocket was hit? Like, comparing Xs and Ys to see if the first sprocket was hitting from above or below, or from left or right?