- Home /
How to check how much are two objects overlapping?
So there are functions that allow you to see if two objects are overlapping, but I think that they don´t tell you how much are they going through. I can´t use Unity´s physics system and am trying to get how much should an object be moved to return to where it originally collided.
Seems unlikely to me that looking at the overlap extent is going to be a good way to achieve what you're after.
What do you mean by "where it originally collided"? Why can't you cache the position you want it to return to?
Because I don´t have that position, and because it´s difficult to get because are two objects moving to different directions at the same time the ones that will collide.
So again, what do you mean by "where it originally collided"? Is the problem just that collision detection isn't precise enough for your purposes? If so then check that none of your objects are using discrete collision detection.
Answer by VicciGames · Nov 22, 2019 at 03:20 AM
You might need to just use continuous collisions if you are having collision errors.
How come you cannot use Unity's physics system?
Are you having trouble with unnoticed collisions?
If so
Let's look at a scenario if you did use Unity Physics.
With Discrete collisions, Physics only detects when a frame passes. So, if an object happens to be positioned past a desired collision, it will miss at the time the frame is calculated.
To fix this (ccontinuous collisions) we need to Predict the collision. This involves shooting raycasts from close and far edge vertices that determine the distance from the next object BEFORE the frame of collision happens so we can then snap to the correct location ( or perhaps marginally position it/ or even slow down the object) to get a safe collision.
Just want to practice NOT using or CANNOT use Unity Physics?
Okay. well. I would say you need to calculate a Vector3 of atleast one of the objects and offset it when a collision is detected (Not using Unity: Use the above method)
I would create a vector3 at collision and calculate the difference between the current position of the object and the new vector3 and subtract it. Might look very ugly.
You can use a vectors magnitude for the size of the vector.
Just off the top of my brain.
Thanks, but I don´t understand your solution. Am I supposed to store the collision point in a Vector3 and get the difference between that and the center of the object that´s colliding right now? (Which would give the distance to an edge). And what do you mean by "use a vector magnitude for the size of the vector" (sorry if it´s a stupid question). By the way, I really CANNOT use Unity physics. They act modifying positions, but all the positions in my scene are defined by the scales of other objects (just imagine the ground getting bigger and smaller and moving things that way). I made linear collisions with raycasts, but I was having a hard time figuring out collisions between two moving objects. I think that I know now. If I somehow translate both movements into just one linear equation, I can do sweeptests and collision casts. The problem now is that that solution implies doing a rigidbody.sweeptest for every object in the scene each frame, which is terrible for performance (or am I wrong?). I was thinking in a way of reducing this calculations, so just if you know, is there an easyand practical way of doing this? Or a better approach?
You wouldn't necessarily need to do a sweep test on every object every frame. For example you could do a less-expensive spherical overlap test first to see if a sweep test was necessary. You wouldn't necessarily need to do the test every frame.
Whether or not you're going to end up with performance problems depends on lots of things, I think you're just going to have to try stuff. Ultimately, if you're going to put your own physics system together it's going to have to do this sort of thing. PhysX doesn't come for free either.
Your answer
Follow this Question
Related Questions
Checking if object intersects? 1 Answer
Simplest way of detecting if there's an object at position X 1 Answer
[JS] Getting an Error In My Money System Script 1 Answer
OnCollisionStay2D - why is the list of contacts is incomplete? 0 Answers
Trying to get a gameObject to collide with particles in a particle system. 1 Answer