I’ve noticed that if my layers are at different angles, collision detection between objects does not work correctly. How do I make this work?
In the following example, I rotate the cameras on the base layer and RedLayer (which contains the RedSquare) by -90 degrees when the game starts. The Background and RedSquare then move from the right of the screen to the left and the RedSquare apparently then “collides” with the BlueSquare even though the BlueSquare is still on the bottom of the screen. Why? How do I get this to work properly?
If I disable the action that changes the camera angles, you can see why it would collide. But once the camera angles are changed, there should be no collision.
When you rotate the layer camera, you’re doing just that - rotating the layer camera - and not rotating the scene. It just rotates the view of the layers. In this case, you’re rotating what gets sent to the screen by 90 degrees in a clockwise direction. So when you’re moving the red square from right to left on the screen, it’s movement is really rotated by 90 degrees anti-clockwise, and it’s really moving it down the scene.
To understand this better, add a third object above the red one, and see it’s position relative to the red one on the screen. It’ll be to the right. You know it’s really above it, but it appears to the right because of the camera rotation. So when you move right to left on the screen, it’s moving away from the third object. And in the actual unrotated scene, this is down the scene, towards the blue square.
Interesting, from a logical X,Y perspective I can see why this is the case, but from a practical perspective it makes it a lot harder for me to detect real visual collisions.
I was considering plotting an invisible sprite on the layer where I want to detect collisions. That might help.
If I had an invisible BlueSquare on the RedLayer which I can use for collision detection with the RedSquare, what function or bit of maths do I need so I can plot it exactly underneath the BlueSquare on the BlueLayer when the RedLayer’s camera is rotated at -90 degrees?
Use variables for x and y to show up your RedSquare position when it collides your BlueSquare. It will show you the coordinates position, So you can put your invisible sprite on these positions on RedLayer.
Ok, I’ve figured this out in the end. I have to calculate the coordinates of the invisible sprite by applying a rotation to it very much like New coordinates by rotation of points Calculator - High accuracy calculation. Then I can plot the invisible sprite in the correct position on the rotated layer where I want to detect the collision.