x


Mesh collider collision detection

I'm working on a 2D game that lets the user move physics enabled objects directly (using the mouse or touches). These GameObjects have colliders and rigidbody attached. For now, we're using boxes, but we plan on having more complex shapes, so we may need to use mesh colliders.

I need to prevent the user from dragging a shape into another one, while still being able to stick them one against the other. I tried using Rigidbody.MovePosition, directly changing the transform position and using AddForce, but all these methods cause varying levels of penetration between objects.

I had excellent results with SweepTest, only moving the object if SweepTest returned false, but SweepTest only works with primitive colliders, NOT with mesh colliders (even when enabling convex mode).

I've considered using MovePosition, then check if the collider fires the OnCollisionEnter callback, and then move back the object if it does, but the callback executes too late, it isn't called as soon as two objects collide, but only at the next FixedUpdate. It could be managed, but would be very cumbersome to work with.

It seems like the only reasonable solution is to combine primitive colliders to form complex shapes, but I'd like to make sure if there isn't another solution, if anyone would like to share it.

It would be great if there was some form of direct collision testing. Something like TestCollision(colliderA, colliderB), that would return true or false, and work with mesh colliders. A bit like SweepTest, but without the sweeping part.

Any pointer is super appreciated, Thanks!

more ▼

asked Oct 20, 2011 at 04:41 AM

dsp4 gravatar image

dsp4
1 4 4 4

Remember that mesh colliders only interact with primitive colliders, not other mesh colliders! (unless you mark them as convex). Could that be your problem?

Oct 20, 2011 at 04:45 AM syclamoth

Yes, they're all set as convex. They actually do collide. The problem arises when a user tries to forcefully drag one object into another, visually, they'll overlap. As soon as there is no more interaction from the user, the objects get out of each other, but it doesn't look too good, and sometimes, it creates explosive results, as the physics engine adds forces to try and separate the objects.

Oct 20, 2011 at 05:19 AM dsp4

Well, if you use MovePosition along with OnCollision callbacks, you could set something up where it just doesn't move any further in that direction if it detects that it has run into something. A little hacky I know, but it might work for you.

Oct 20, 2011 at 06:01 AM syclamoth
(comments are locked)
10|3000 characters needed characters left

0 answers: sort voted first
Be the first one to answer this question
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x4312
x3025
x2202
x17

asked: Oct 20, 2011 at 04:41 AM

Seen: 2835 times

Last Updated: Oct 20, 2011 at 06:01 AM