Beispiel #1
0
        private void OnSimulationPre()
        {
            if (ConstraintGameObject == null && m_lineGeometry.isEnabled())
            {
                var geometryContacts = new agxCollide.GeometryContactPtrVector();
                var numContacts      = GetSimulation().getSpace().getGeometryContacts(geometryContacts, m_lineGeometry);
                if (numContacts > 0)
                {
                    var ray                    = m_camera.ScreenPointToRay(Input.mousePosition);
                    var rayHandedOrigin        = ray.origin.ToHandedVec3();
                    var closestGeometryContact = geometryContacts[0];
                    var closestDistance2       = rayHandedOrigin.distance2(closestGeometryContact.points().at(0).point);
                    for (int i = 1; i < geometryContacts.Count; ++i)
                    {
                        var gc        = geometryContacts[i];
                        var distance2 = rayHandedOrigin.distance2(gc.points().at(0).point);
                        if (distance2 < closestDistance2)
                        {
                            closestDistance2       = distance2;
                            closestGeometryContact = gc;
                        }
                    }

                    var       raycastBody = closestGeometryContact.rigidBody(0);
                    RigidBody body        = null;
                    if (raycastBody != null && raycastBody.getMotionControl() == agx.RigidBody.MotionControl.DYNAMICS)
                    {
                        var bodies = FindObjectsOfType <RigidBody>();
                        for (int i = 0; body == null && i < bodies.Length; ++i)
                        {
                            if (bodies[i].Native != null && bodies[i].Native.getUuid().str() == raycastBody.getUuid().str())
                            {
                                body = bodies[i];
                            }
                        }
                    }

                    ConstraintGameObject = TryCreateConstraint(closestGeometryContact.points().at(0).point.ToHandedVector3(),
                                                               body,
                                                               MouseButtonToDofTypes(m_mouseButtonState.ButtonDown),
                                                               "PickHandlerConstraint");

                    if (ConstraintGameObject != null)
                    {
                        ConstraintGameObject.AddComponent <Rendering.PickHandlerRenderer>();
                        Constraint.DrawGizmosEnable = false;
                        m_distanceFromCamera        = FindDistanceFromCamera(m_camera,
                                                                             Constraint.AttachmentPair.ReferenceFrame.Position);
                    }

                    m_lineGeometry.setEnable(false);
                }
            }
        }