- Home /
how could I break apart or unstick two intersecting rigid bodies
http://www.mediafire.com/download/x5zys2m44dj9rt5/test1.zip
(The camera script has a "Node" property which you can set to the cube or plane prefab)
I'm trying to do a classic: Be able to drag and drop rigid bodies around while having them collide and bounce with any other rigid bodies along the way. Of course, I'm also running into a classic issue: if I drag fast enough, I am able to intersect the body I'm dragging right into another body. Eventually PhysX might break them free from each other, but ideally I'd want the intersection to not happen in the first place. I'm moving the rigid body by doing rigid body.
Vector3 newPosition = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, -Camera.main.transform.position.z));
rigidbody.MovePosition(newPosition);
The suggestions that I've seen in the forum are usually along the lines of using the DragRigidBody script or the DragObject script. But neither feels right since they incur a noticeable lag between one dragging a finger / mouse to the desired position and the object following after the fact. Actually, considering the perceived velocity at which the object can be dragged, I don't see any difference between using those scripts vs the approach I showed above: moving directly to the new position, if done at a comparable velocity, won't result in bodies intersecting and getting stuck.
Anyway, so since the two drag scripts don't feel snappy enough, I was thinking what if I wait until after the bodies intersect and in OnCollisionEnter(Collision)
for instance, apply some force to break them apart, given the contact points of the collision, or given some insight from inspecting both colliders involved in the collision?
Does anyone have any thoughts on what aspect(s) of the collision to base the direction and force to break the bodies apart?
This is what I mean by the bodies getting stuck together if I drag one of them too fast towards another:
These 2 planes are at the same z coordinate and are therefore intersecting. Eventually, depending on how the intersect, the physics engine will impart some force that separates them so they no longer intersect. But ideally they shouldn't be allowed to intersect in the first place.
is this a 2.5D game? ortho camera?
you can't use planes (at all, ever, for any reason) in unity. make the colliders cubes (same length on all three dimensions)
if you're asking what I think your'e asking, this question comes up a lot
http://answers.unity3d.com/questions/307540/is-it-possible-to-cast-a-ray-but-ignore-z-position.html
in 2.5D games, the COLLIDERS are often REALLY DEEP. so you might have enemies on layer 8 and heros on layer 7 and flying boulders on layer4 (I'm saying where the sprites are) but in fact the COLLIDERS of those things, would all be from like 2 to 20 in all cases, you know?
Answer by Fattie · Oct 15, 2013 at 08:37 AM
Having read all the comments, I believe the answer to your question is:
your colliders must be cubes.
As you say below, I literally mean "cubes" -- pretty much exactly the same length on all sides
Here is my demo project ... please grab the middle cube and move it.
http://www.filedropper.com/test2_4
NOTE -- the "cube" aspect has fixed your problem. (Please tick this answer :) ) But you are indeed on the fundamental limits of physX. Using my demo, try moving REALLY REALLY FAST. Of course, you will break it - but then - that would happen ion the real world, am I correct?
Hope it helps!
Regarding purely the issue of getting the "feel" you want (which is what i first thought you were asking.)
It is very difficult to do this. I think in this case you can and should "just move" the object at all times to exactly where the finger is. It will "smash out of the way" any other objects it encounters. You may have to additionally add "fling" .. when you let go, turn on the rigidbody again and give it a velocity based on it's recent finger-speed.
So to recap:
The key thing was not the meshes being planes (I re-tested and they can be 2d or 3d and it does not matter).
what DOES $$anonymous$$ATTER is that the colliders be either cubes or at least not be too skinny in any one dimension (right now I'm using 10x5x10 and they work fine too.)
As far as the feel,
rigid body.$$anonymous$$ovePosition(thePositionOfThe$$anonymous$$ouseInTheCurrentFrame)
feels snappy just fine.is$$anonymous$$inematic
can be true or false, depending on whether you want the object you drag to also by affected by a force on collision with another object.
I hope I have utterly answered your question - now I have to drink wine and eat lunch!
just incidentally notice the excellent free "filedropper.com" service - amazing
Your answer
![](https://koobas.hobune.stream/wayback/20220613115948im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Rigidbody stuck in colliders when moving and ricosheting. 0 Answers
OnCollisionEnter but have colliding object not move the object it collides with 1 Answer
handling collision with two objects of the same tag 1 Answer
Collisions between two rigidbody's and scripting collisions problem 2 Answers
Cannot get collision to do anything 1 Answer