8
\$\begingroup\$

I'm trying to create a game where objects can be dragged, but I'm having trouble with the physics. With rigid bodies that have similar masses, they sometimes push through each other when dragged and cause unwanted movements. Here is a demo.

I'd like to do this using only Unity 3D's built-in physics.

In the demo, you can use the mouse to click and drag objects. The cylinders all have box colliders and masses of 10000. They also have various constraints depending on their orientation (none can rotate, and all are constrained in x or y). The sphere has a mass of 1, a sphere collider and a z constraint.

The sphere and cylinders interact well. However dragging the cylinders across one another causes intersection. I do not want this!

The cylinders have interpolate set to 'Interpolate' and collision detection set to 'Continuous'. I have thrown the 'DontGoThroughThings' script onto all the cylinders, but it doesn't seem to do anything. As for setting the velocity of the rigidbodies, I have used both of these following lines with basically the same result:

rb.AddForce(speed * diff * Time.deltaTime - rb.velocity, ForceMode.VelocityChange);
//rb.velocity = speed * diff * Time.deltaTime;

Without writing my own collision detection code, I'm out of ideas. Is there a simple setting or something I've missed?

With the context explained, here's a more precise question: "How can I get the cylinders in my demo to not move through each other?"

\$\endgroup\$
6
  • \$\begingroup\$ Does reducing the mass of the cylinders make any difference? \$\endgroup\$
    – MGB
    Feb 20, 2014 at 14:11
  • \$\begingroup\$ No, it'll make them go through more easily I think. Also if the mass is similar to the sphere, the sphere will start to go through the cylinders too. \$\endgroup\$
    – Jeff
    Feb 20, 2014 at 14:18
  • \$\begingroup\$ What makes you think this? It's when mass ratios are larger that the physics engine has more trouble with precision. \$\endgroup\$
    – MGB
    Feb 21, 2014 at 15:00
  • \$\begingroup\$ Because I tried it :P I can upload a demo where all the masses are set to 1 if you want. \$\endgroup\$
    – Jeff
    Feb 21, 2014 at 17:12
  • \$\begingroup\$ One thing I'm thinking is it may be due to the way I'm changing velocity. I'm going to try changing to force instead \$\endgroup\$
    – Jeff
    Feb 21, 2014 at 17:25

2 Answers 2

2
\$\begingroup\$

Without looking at your project/code I would like to suggest this:

private bool _mouseOverObject = false;

void Update(){
    if(_mouseOverObject && Input.GetMouseDown(0){
        rigidbody.isKinematic = false; //Let Unity Physics affect the item.
    } else {
        rigidbody.isKinematic = true; //Let it affect other physics but not be affected itself
    }
}

void OnMouseOver(){
    _mouseOverObject = true;
}

void OnMouseExit(){
    _mouseOverObject = false;
}

It will allow you to restrict physics but within Unity's scope and physics engine. You can jiggle around with the script to make it lock only one object to be moved etc but this should get you started.

\$\endgroup\$
0
\$\begingroup\$

You want the collision-detection set to Continuous Dynamic for fast moving objects. Continuous is used for static- or slow-moving objects that Continuous Dynamic objects collide with. See here.

(Keep in mind that continuous collision detection is expensive, so use it sparingly)

\$\endgroup\$
1
  • \$\begingroup\$ I get the same results when I set each of the cylinders to Continuous Dynamic. Is that what you meant? \$\endgroup\$
    – Jeff
    Feb 20, 2014 at 1:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .