Discuss Scratch

littlebunny06
Scratcher
100+ posts

Collisions in 3D Space.

So, I made a project in which a the user can navigate a 3D environment. I want collisions in this project too. I have the collision detection down, except I do not know what to do from there.

Here is my project if you are interested. The collision script is contained within the sprite “Camera”: https://scratch.mit.edu/projects/302486594/
nyankatpro
Scratcher
500+ posts

Collisions in 3D Space.

What i do for collisions is to move the player back to their previous spot, and to cancel their velocity for that direction. Think about how collisions work in a 2D game, but with the extra Z-axis added in.
littlebunny06
Scratcher
100+ posts

Collisions in 3D Space.

nyankatpro wrote:

What i do for collisions is to move the player back to their previous spot, and to cancel their velocity for that direction. Think about how collisions work in a 2D game, but with the extra Z-axis added in.
Yes, I was thinking about that, but how my code works is that a boolean variable reads true or false whether the player hitbox intersects another environment hitbox. I don't really know how to cancel the velocities based off of that. For example, if the player brushes a wall at an oblique angle, he maintains an x-velocity but loses the y velocity. I don't really know a way to code that. Additionally, if the player is moving at a relatively high velocity, he can phase through walls in one frame.
nyankatpro
Scratcher
500+ posts

Collisions in 3D Space.

littlebunny06 wrote:

nyankatpro wrote:

What i do for collisions is to move the player back to their previous spot, and to cancel their velocity for that direction. Think about how collisions work in a 2D game, but with the extra Z-axis added in.
Yes, I was thinking about that, but how my code works is that a boolean variable reads true or false whether the player hitbox intersects another environment hitbox. I don't really know how to cancel the velocities based off of that. For example, if the player brushes a wall at an oblique angle, he maintains an x-velocity but loses the y velocity. I don't really know a way to code that. Additionally, if the player is moving at a relatively high velocity, he can phase through walls in one frame.
Basically, what you can do is move the player's x,y and z separately. You move in one direction, check to see if the player is touching a wall, and if they are, move them back and cancel their velocity for that direction. As for the phasing, you can split the player's movement into segments (either halves or fourth, depending on how fast the player can go), and check for collision at each of the intervals.
littlebunny06
Scratcher
100+ posts

Collisions in 3D Space.

nyankatpro wrote:

Basically, what you can do is move the player's x,y and z separately. You move in one direction, check to see if the player is touching a wall, and if they are, move them back and cancel their velocity for that direction. As for the phasing, you can split the player's movement into segments (either halves or fourth, depending on how fast the player can go), and check for collision at each of the intervals.
Implementing this method is going really well so far! I am, however having a really weird issue where I get stuck to a wall like a magnet and cannot move along the x axis(I've only implemented x axis collision). The camera is supposed to jump back to its old position but it's old position is colliding? Here is a screenshot of the code:
that_generic_guy
Scratcher
59 posts

Collisions in 3D Space.

I have NO idea how 3D works but you could have a list containing the last ~50 player xyz locations. If the player is touching something it rewinds the list until it's not stuck. Or, you could check out what some of the other 3D projects do, since I'm just guessing.
that_generic_guy
Scratcher
59 posts

Collisions in 3D Space.

Oh and maybe you need to have it the “old x” variables only change to the x if there are no collisions. I think that maybe the problem.
littlebunny06
Scratcher
100+ posts

Collisions in 3D Space.

Ok, I fixed it. I tried the Old position method but the camera stutters upon colliding with a wall. Then, I tried a method which I have used with 2D collisions, changing the camera position by the | velocity | / velocity * -1. The camera still stutters, I don't know why.

Powered by DjangoBB