Discuss Scratch
- Discussion Forums
- » Show and Tell
- » 3D collisions
- ggenije2
-
100+ posts
3D collisions
3D collisions
The main thing to pay attention to is the fact that collision detection in general is very performance expensive operation.
For that reason you should look to find the most optimal solution for collision detection even if it the solution is at cost of precise collisions.
There are every various types of collision detection, and every algorithm have two key objects which are colliding. That key objects could be
primitives (cubes, spheres, capsules ,triangles etc.) or even whole mesh (although mesh is actually a group of trianlges).
Now the every collision algorithm is combination of of those two key objects, this means there are collisions like cube vs sphere collision, triangle vs sphere , capsule vs triangle etc.
But for scratch there are only a few one which are worth to be made since the scratch is very performance limiting.
Those collisions are:
Of course there a lot more like OBB vs OBB (OBB - Oriented Bounding Box) and triangle vs triangle,
but those are simply not worth it to have in scratch because they're very performance demanding.
So let's explain how to do previous collisions:
Sphere vs Sphere
The easiest one to do, to test collision just check if the distance between centres of two spheres is less than their radiuses combined.
AABB vs AABB
Also very easy it is done by checking each axis seperately (X, Y and Z) to check are they colliding on that axis.
The formula is:
minX1<maxX2 and minX2<maxX1 and minY1<maxY2 and minY2<maxY1 and minZ1<maxZ2 and minZ2<maxZ1
The Sphere vs Triangle and Sphere vs Capsule I won't explain here because they're too complex to be explained especially vs Triangle one (~500 blocks).
If you want you can find them here: https://scratch.mit.edu/projects/560988776/editor/
Nevertheless for beginners it's best to use only the first two here. This mean your projects must be designed so you don't need the complex collision algorithms.
Still again, the more complex algorithm doesn't mean it's better.
In the video game industry , mesh vs mesh collisions are very expensive, for that reason one mesh is divided into multiply primitive objects (like sphere for head, OBB vs for arms and lengs, capsule for torso etc.)
This means work smarter not harder.
In many scenarios you can combine multiple objects to get complex one, instead of needing to have special algorithm for that complex object.
For example if you have wall and player as sphere, instead of needing to have Quad collision which is very expensive operation,
you can instead check for both Sphere vs AABB and Sphere vs Plane (Where intersection between that Plane and AABB must be the desired quad)
This is much faster solution only fallback is that your quads must be aligned on at least one axis.
Also goes for the floor , you can have multiply triangles, but even better solution is to have multiply aabb , and for slopes combinations of AABB and planes,
but for start you don't need to have slopes just have AABB as floor and as player, that is the easier solution for begginers.
Now for the actual thing which needs to happen when collision occurs for the collisions you wan't to be “solid”, it's the same thing as you would in 2D platformers.
You have separated Y and XZ movement, just you need to do X movement and then Z movement.
For XZ when you check collision you return to previous value of that axis.
For Y collision, after hitting ground move up until you don't.
The main thing to pay attention to is the fact that collision detection in general is very performance expensive operation.
For that reason you should look to find the most optimal solution for collision detection even if it the solution is at cost of precise collisions.
There are every various types of collision detection, and every algorithm have two key objects which are colliding. That key objects could be
primitives (cubes, spheres, capsules ,triangles etc.) or even whole mesh (although mesh is actually a group of trianlges).
Now the every collision algorithm is combination of of those two key objects, this means there are collisions like cube vs sphere collision, triangle vs sphere , capsule vs triangle etc.
But for scratch there are only a few one which are worth to be made since the scratch is very performance limiting.
Those collisions are:
- Sphere vs Sphere
- AABB vs AABB (AABB stands for Axis-Aligned Bounding Box, this is a cube which can't be rotated).
- Sphere vs Plane (Plane is infinitely long surface)
- Sphere vs Triangle (or Quad)
- Sphere vs Capsule (Capsule is like 3D line with thickness)
Of course there a lot more like OBB vs OBB (OBB - Oriented Bounding Box) and triangle vs triangle,
but those are simply not worth it to have in scratch because they're very performance demanding.
So let's explain how to do previous collisions:
Sphere vs Sphere
The easiest one to do, to test collision just check if the distance between centres of two spheres is less than their radiuses combined.
AABB vs AABB
Also very easy it is done by checking each axis seperately (X, Y and Z) to check are they colliding on that axis.
The formula is:
minX1<maxX2 and minX2<maxX1 and minY1<maxY2 and minY2<maxY1 and minZ1<maxZ2 and minZ2<maxZ1
The Sphere vs Triangle and Sphere vs Capsule I won't explain here because they're too complex to be explained especially vs Triangle one (~500 blocks).
If you want you can find them here: https://scratch.mit.edu/projects/560988776/editor/
Nevertheless for beginners it's best to use only the first two here. This mean your projects must be designed so you don't need the complex collision algorithms.
Still again, the more complex algorithm doesn't mean it's better.
In the video game industry , mesh vs mesh collisions are very expensive, for that reason one mesh is divided into multiply primitive objects (like sphere for head, OBB vs for arms and lengs, capsule for torso etc.)
This means work smarter not harder.
In many scenarios you can combine multiple objects to get complex one, instead of needing to have special algorithm for that complex object.
For example if you have wall and player as sphere, instead of needing to have Quad collision which is very expensive operation,
you can instead check for both Sphere vs AABB and Sphere vs Plane (Where intersection between that Plane and AABB must be the desired quad)
This is much faster solution only fallback is that your quads must be aligned on at least one axis.
Also goes for the floor , you can have multiply triangles, but even better solution is to have multiply aabb , and for slopes combinations of AABB and planes,
but for start you don't need to have slopes just have AABB as floor and as player, that is the easier solution for begginers.
Now for the actual thing which needs to happen when collision occurs for the collisions you wan't to be “solid”, it's the same thing as you would in 2D platformers.
You have separated Y and XZ movement, just you need to do X movement and then Z movement.
For XZ when you check collision you return to previous value of that axis.
For Y collision, after hitting ground move up until you don't.
Last edited by ggenije2 (Nov. 23, 2022 17:06:48)
- oh261401
-
7 posts
3D collisions
Idk if I'm supposed to be here, but sphere to capsule is just finding the nearest point on a line segment (with dp) to a point, then doing a sphere to sphere collision there, right?
- ggenije2
-
100+ posts
3D collisions
Idk if I'm supposed to be here, but sphere to capsule is just finding the nearest point on a line segment (with dp) to a point, then doing a sphere to sphere collision there, right?
Yeah, but I believe that capsule can have different sizes on ending, but usually you would not need such a feature.
- legocreeper68
-
9 posts
3D collisions
I not sure if anybody is seeing this, but I am working on my own 3d engine which is on my profile and want to add sphere triangle collision for the engine for platformer, but more specifically my Wii sports in scratch Idea, such as island flyover mode crash detection. If you could help that would be greatly appreciated.
- CallenLogan
-
19 posts
3D collisions
I was able to make a 3d game, idk if it will help, but the code is prob in there
https://scratch.mit.edu/projects/1160543998/
https://scratch.mit.edu/projects/1160543998/
- Discussion Forums
- » Show and Tell
-
» 3D collisions