collision detection without physics

I’m trying out unity with a simple 3D puzzle and just need to prevent the pieces from penetrating each other and fit snugly together when moved using their trasnsforms. I don’t really need gravity, rigid bodies or any physics really. Is there a simple way to do this?

Collision detect (with colliders) and physics (gravity, rigidbodies, etc) are two separate features.

However, I would say the easiest way for you to prevent two pieces from penetrating each other would be merely to attach rigidbodies to both objects. Bam. No penetration, no coding required.

Depending on what you’re doing exactly, it sounds like you could just use arrays, rather like this basic Tetris clone does. In other words, just keep the state internally and have the 3D objects represent that state rather than interacting with each other in any way.

–Eric

thanks tempest

i’m new to unity and i’m obviously not grasping something basic here. The first thing I tried was rigid bodies but the pieces still penetrated each other. I assumed that was because i was manipulating the objects directly using transform.Rotate transform.Translate. I’m thinking now maybe it’s something to do with using mesh colliders. I need very precise collision detection so that faces can snap together and i assumed mesh colliders were the way to achieve this. I give the documentation on colliders a thorough read.

best
rod

i checked your Tetris clone project Eric and although it doesn’t compile on the trial version of Unity i’m using I learnt some useful stuff from your script. Thanks. However I don’t think the array approach would be practical for what i’m doing. The pieces are all different shapes and the number of possible arrangements of them is huge. All I need to know when the face of one object is next to another and in which of 6 directions.

-rod

then you will have to use the regular physics for it if you need real collision or implement an own 2D collision handling (or use one of the existing .net based ones which thought will make your application even larger potentially and are naturally quite a bit more complex to use as you must provide them with the 2D collision polygon etc)

i’ve spent some time today trying to make collision work without physics and got nowhere. What happens on the first frame that 2 box colliders have coincident faces? I would have expected OnCollisionEnter to do something but it doesn’t seem to. Anyone got an ideas before i start coding my own collision handler?
I need to accurately move objects around an invisible 3D grid and mass, gravity, momentum etc just send everything out of control.
BTW why do numbers like -1.490116e-08 often appear in the transform position display when i would expect to see zero?

I am thinking that you should define for each piece what relationship it has with each neighboring piece, ie piece A is 1 unit up and .25 units right of piece B. Use box colliders to get an idea when matching pieces are touching each other and if they are within a certain “snap” range, move one of the pieces into the position defined initially. From there, I would parent one of the pieces to the other. Then remove the rigidbody from the now child piece, thus making a compound collider.

I suggest looking at the Collision Message Sending Matrix to see what the requirements are for, well, collision message sending. It looks to me, like the best bet would be to have all the pieces not being moved have non-kinematic rigidbodies. The piece that is being moved would be kinematic until it’s set down.

This is obviously a big abstraction from the details of the coding. I suggest starting with just two square pieces. Get drag and drop working for them. Then get them to snap together when the colliders touch. Then get them to snap together only when they are in roughly the right relative positions to each other. Then figure out how to add a third piece to the joined pieces. A fourth piece. Then maybe figure out how to join two connected pieces to two other connected pieces.

Good luck!

Sorry about the script error; that was a typo that previous versions of Unity didn’t catch. I uploaded a fixed version.

Floating point imprecision; that number is essentially zero.

–Eric

eric

does that mean that vectors should be rounded after translation if precision movement is required?

-rod

No, the imprecision is not enough to be noticed visually. Just don’t try to compare floating point numbers to each other directly (i.e., “if (myFloat1 == myFloat2)”), since that will often fail, but use <= and >= instead.

–Eric

Got the collision working. After some experimentation i wound up with this…

mesh colliders are no good for this - if you have more than one at a time ‘convex’ needs to be selected which generates inaccurate collisions. I used compound box colliders slightly smaller than the object meshes to avoid spurious corner and edge collsions and just detect face contacts. Rigid bodies attached to each piece with Gravity=off and Kinematic=on. I just toggle Kinematic=off for the piece being moved. The script has got quite complex but it’s working well now. thanks for the help.
-rod

i’m rejigging my code a bit and i need to find the parent of a compound box collider group i would have expected ‘collider.gameObject.parent’ to work but it doesn’t. Can’t find it in the docs.

transform.parent, not gameObject.parent.

–Eric

thanks eric but that doesn’t seem to work as expected. what class is the result. it doesn’t seem to be gameObject or Vector3. I guess i’ve just got used to cocoa touch this all seems rather arbitary quirky. It would sure be less frustrating if i could find out how to display my globals in debug mode I do tire of constantly typing Debug.LogI"… just to see what’s going on. Scripting documentation leaves something to be desired but i suppose Open GL in iPhone SDK is not exactly a walk in the park.
-rod

// Make this object the child of another
var someOtherObject : GameObject;
transform.parent = someOtherObject.transform;

All your variables are displayed in the Inspector when running, aside from static variables.

–Eric

The reason you aren’t getting a GameObject or Vector3 from transform.parent is because it’s giving you a Transform. To get the parent GameObject of xobj, do:

GameObject parent = xobj.transform.parent.gameObject;

Good luck,
Yilmaz

eric

just to be clear - globals (variables accessible by scripts attached to other objects)
must be declared using ‘static’ and cannot be seen in debug mode. This seems a
bit unexpected.

myraneal
thanks i don’t think i’d have figured that out from the docs.
BTW what’s the equivalent of introspection i.e. how i do a test like this in javascript?

if (self == gameObject)

To test equality between two GameObjects, use:

if(thisObj.Equals(thatObj))
{
   //code goes here
}

-Yilmaz