Exemplo n.º 1
0
        public void Evaluate(int SpreadMax)
        {
            if (this.FWorld.PluginIO.IsConnected)
            {
                this.FHit.SliceCount = SpreadMax;

                List<double> fraction = new List<double>();
                List<Vector3D> position = new List<Vector3D>();
                List<Vector3D> normal = new List<Vector3D>();
                List<SoftBody> body = new List<SoftBody>();
                List<int> bodyid = new List<int>();
                List<int> qidx = new List<int>();
                List<int> nidx = new List<int>();
                List<EFeature> feat = new List<EFeature>();
                List<Node> nodes = new List<Node>();

                for (int i = 0; i < SpreadMax; i++)
                {
                    Vector3 from = this.FFrom[i].ToBulletVector();
                    Vector3 to = this.FTo[i].ToBulletVector();

                    List<SoftBody> sbs = this.FWorld[0].SoftBodies;
                    //List<SRayCast> results = new List<SRayCast>();

                    SRayCast closest = null;

                    for (int ib = 0; ib < sbs.Count; ++ib)
                    {
                        SoftBody psb = sbs[ib];
                        SRayCast res = new SRayCast();
                        if (psb.RayTest(from, to, res))
                        {
                            if (closest == null)
                            { closest = res; }
                            else
                            { if (res.Fraction < closest.Fraction) { closest = res; } }
                        }
                    }

                    if (closest != null)
                    {
                        this.FHit[i] = true;
                        fraction.Add(closest.Fraction);

                        Vector3 pos = from + (to - from) * closest.Fraction;
                        position.Add(pos.ToVVVVector());
                        body.Add(closest.Body);
                        qidx.Add(i);
                        SoftBodyCustomData sd = (SoftBodyCustomData)closest.Body.UserObject;
                        bodyid.Add(sd.Id);
                        feat.Add(closest.Feature);

                        int nodeidx = closest.Index;

                        Node fnode = null;

                        if (closest.Feature == EFeature.Face)
                        {
                            var f = closest.Body.Faces[closest.Index];
                            Node node = f.N[0];
                            for (int ni = 1; ni < 3; ++ni)
                            {
                                if ((node.X - pos).LengthSquared() >
                                    (f.N[ni].X - pos).LengthSquared())
                                {
                                    fnode = f.N[ni];
                                }
                            }
                        }

                        if (closest.Feature == EFeature.Node)
                        {
                            fnode = closest.Body.Nodes[nodeidx];
                        }

                        nidx.Add(nodeidx);
                        nodes.Add(fnode);

                    }
                    else
                    {
                        this.FHit[i] = false;
                    }
                }

                this.FId.AssignFrom(bodyid);
                this.FHitFraction.AssignFrom(fraction);
                this.FHitPosition.AssignFrom(position);
                this.FQueryIndex.AssignFrom(qidx);
                this.FBody.AssignFrom(body);
                this.FHitFeature.AssignFrom(feat);
                this.FHitIndex.AssignFrom(nidx);
                this.FNode.AssignFrom(nodes);
            }
            else
            {
                this.FHit.SliceCount = 0;
                this.FId.SliceCount = 0;
                this.FHitFraction.SliceCount = 0;
                this.FHitPosition.SliceCount = 0;
                this.FHitFeature.SliceCount = 0;
                this.FHitIndex.SliceCount = 0;
                this.FBody.SliceCount = 0;
                this.FNode.SliceCount = 0;
            }
        }
Exemplo n.º 2
0
		public bool RayTest(Vector3 rayFrom, Vector3 rayTo, SRayCast results)
		{
            IntPtr rayCast = btSoftBody_sRayCast_new();
            bool ret = btSoftBody_rayTest(_native, ref rayFrom, ref rayTo, rayCast);
            results.Body = this;
            results.Feature = btSoftBody_sRayCast_getFeature(rayCast);
            results.Fraction = btSoftBody_sRayCast_getFraction(rayCast);
            results.Index = btSoftBody_sRayCast_getIndex(rayCast);
            btSoftBody_sRayCast_delete(rayCast);
            return ret;
		}
Exemplo n.º 3
0
        public override void OnHandleInput()
        {
            if (Input.KeysPressed.Contains(Keys.B))
            {
                PreviousDemo();
            }
            else if (Input.KeysPressed.Contains(Keys.N))
            {
                NextDemo();
            }

            if (Input.KeysDown.Count != 0)
            {
                if (demos[demo] == Init_ClusterCombine || demos[demo] == Init_ClusterCar)
                {
                    if (Input.KeysDown.Contains(Keys.Up))
                    {
                        motorControl.MaxTorque = 1;
                        motorControl.Goal += FrameDelta * 2;
                    }
                    else if (Input.KeysDown.Contains(Keys.Down))
                    {
                        motorControl.MaxTorque = 1;
                        motorControl.Goal -= FrameDelta * 2;
                    }
                    else if (Input.KeysDown.Contains(Keys.Left))
                    {
                        steerControlF.Angle += FrameDelta;
                        steerControlR.Angle += FrameDelta;
                    }
                    else if (Input.KeysDown.Contains(Keys.Right))
                    {
                        steerControlF.Angle -= FrameDelta;
                        steerControlR.Angle -= FrameDelta;
                    }
                }
            }

            if (Input.MousePressed == MouseButtons.Right)
            {
                results.Fraction = 1;
                if (pickConstraint == null)
                {
                    Vector3 rayFrom = Freelook.Eye;
                    Vector3 rayTo = GetRayTo(Input.MousePoint, Freelook.Eye, Freelook.Target, Graphics.FieldOfView);
                    Vector3 rayDir = rayTo - rayFrom;
                    rayDir.Normalize();

                    SRayCast res = new SRayCast();
                    var softBodies = (World as SoftRigidDynamicsWorld).SoftBodyArray;
                    if (softBodies.Any(b => b.RayTest(rayFrom, rayTo, res)))
                    {
                        results = res;
                        impact = rayFrom + (rayTo - rayFrom) * results.Fraction;
                        drag = !cutting;
                        lastMousePos = Input.MousePoint;

                        NodePtrArray nodes;
                        switch (results.Feature)
                        {
                            case EFeature.Face:
                                nodes = results.Body.Faces[results.Index].N;
                                break;
                            case EFeature.Tetra:
                                nodes = results.Body.Tetras[results.Index].Nodes;
                                break;
                            default:
                                nodes = null;
                                break;
                        }
                        if (nodes != null)
                        {
                            node = nodes.Aggregate((min, n) =>
                                (n.X - impact).LengthSquared <
                                (min.X - impact).LengthSquared ? n : min
                            );
                            goal = node.X;
                        }
                        else
                        {
                            node = null;
                        }
                    }
                }
            }
            else if (Input.MouseReleased == MouseButtons.Right)
            {
                if (!drag && cutting && results.Fraction < 1)
                {
                    using (var isphere = new ImplicitSphere(impact, 1))
                    {
                        results.Body.Refine(isphere, 0.0001f, true);
                    }
                }
                results.Fraction = 1;
                drag = false;
            }

            // Mouse movement
            if (Input.MouseDown == MouseButtons.Right)
            {
                if (node != null && results.Fraction < 1)
                {
                    if (!drag)
                    {
                        int x = Input.MousePoint.X - lastMousePos.X;
                        int y = Input.MousePoint.Y - lastMousePos.Y;
                        if ((x * x) + (y * y) > 6)
                        {
                            drag = true;
                        }
                    }
                    if (drag)
                    {
                        lastMousePos = Input.MousePoint;
                    }
                }
            }

            base.OnHandleInput();
        }
Exemplo n.º 4
0
 public bool RayTest(ref Vector3 rayFrom, ref Vector3 rayTo, SRayCast results)
 {
     return btSoftBody_rayTest(_native, ref rayFrom, ref rayTo, results._native);
 }
Exemplo n.º 5
0
        protected override void OnHandleInput()
        {
            base.OnHandleInput();

            if (Input.MousePressed == MouseButtons.Right)
            {
                results.Fraction = 1;
                if (pickConstraint == null)
                {
                    Vector3 rayFrom = Freelook.Eye;
                    Vector3 rayTo = GetRayTo(Input.MousePoint, Freelook.Eye, Freelook.Target, FieldOfView);
                    Vector3 rayDir = rayTo - rayFrom;
                    rayDir.Normalize();

                    SRayCast res = new SRayCast();
                    var softBodies = (PhysicsContext.World as SoftRigidDynamicsWorld).SoftBodyArray;
                    if (softBodies.Any(b => b.RayTest(rayFrom, rayTo, res)))
                    {
                        results = res;
                        impact = rayFrom + (rayTo - rayFrom) * results.Fraction;
                        drag = !(PhysicsContext as Physics).cutting;
                        lastMousePos = Input.MousePoint;

                        NodePtrArray nodes;
                        switch (results.Feature)
                        {
                            case EFeature.Face:
                                nodes = results.Body.Faces[results.Index].N;
                                break;
                            case EFeature.Tetra:
                                nodes = results.Body.Tetras[results.Index].Nodes;
                                break;
                            default:
                                nodes = null;
                                break;
                        }
                        if (nodes != null)
                        {
                            node = nodes.Aggregate((min, n) =>
                                (n.X - impact).LengthSquared() <
                                (min.X - impact).LengthSquared() ? n : min
                            );
                            goal = node.X;
                        }
                        else
                        {
                            node = null;
                        }
                    }
                }
            }
            else if (Input.MouseReleased == MouseButtons.Right)
            {
                if (!drag && (PhysicsContext as Physics).cutting && results.Fraction < 1)
                {
                    using (var isphere = new ImplicitSphere(impact, 1))
                    {
                        results.Body.Refine(isphere, 0.0001f, true);
                    }
                }
                results.Fraction = 1;
                drag = false;
            }

            // Mouse movement
            if (Input.MouseDown == MouseButtons.Right)
            {
                if (node != null && results.Fraction < 1)
                {
                    if (!drag)
                    {
                        int x = Input.MousePoint.X - lastMousePos.X;
                        int y = Input.MousePoint.Y - lastMousePos.Y;
                        if ((x * x) + (y * y) > 6)
                        {
                            drag = true;
                        }
                    }
                    if (drag)
                    {
                        lastMousePos = Input.MousePoint;
                    }
                }
            }

            (PhysicsContext as Physics).HandleInput(Input, FrameDelta);
        }
Exemplo n.º 6
0
        public override void OnHandleInput()
        {
            if (Input.KeysPressed.Contains(Keys.B))
            {
                PreviousDemo();
            }
            else if (Input.KeysPressed.Contains(Keys.N))
            {
                NextDemo();
            }

            if (Input.KeysDown.Count != 0)
            {
                if (demos[demo] == Init_ClusterCombine || demos[demo] == Init_ClusterCar)
                {
                    if (Input.KeysDown.Contains(Keys.Up))
                    {
                        motorControl.MaxTorque = 1;
                        motorControl.Goal += FrameDelta * 2;
                    }
                    else if (Input.KeysDown.Contains(Keys.Down))
                    {
                        motorControl.MaxTorque = 1;
                        motorControl.Goal -= FrameDelta * 2;
                    }
                    else if (Input.KeysDown.Contains(Keys.Left))
                    {
                        steerControlF.Angle += FrameDelta;
                        steerControlR.Angle += FrameDelta;
                    }
                    else if (Input.KeysDown.Contains(Keys.Right))
                    {
                        steerControlF.Angle -= FrameDelta;
                        steerControlR.Angle -= FrameDelta;
                    }
                }
            }

            if (Input.MousePressed == MouseButtons.Right)
            {
                results.Fraction = 1;
                if (pickConstraint == null)
                {
                    Vector3 rayFrom = Freelook.Eye;
                    Vector3 rayTo = GetRayTo(Input.MousePoint, Freelook.Eye, Freelook.Target, Graphics.FieldOfView);
                    Vector3 rayDir = (rayTo - rayFrom);
                    rayDir.Normalize();
                    AlignedSoftBodyArray sbs = SoftWorld.SoftBodyArray;
                    for (int ib = 0; ib < sbs.Count; ++ib)
                    {
                        SoftBody psb = sbs[ib];
                        SRayCast res = new SRayCast();
                        if (psb.RayTest(ref rayFrom, ref rayTo, res))
                        {
                            results = res;
                        }
                        else
                        {
                            res.Dispose();
                        }
                    }
                    if (results.Fraction < 1)
                    {
                        impact = rayFrom + (rayTo - rayFrom) * results.Fraction;
                        drag = !cutting;
                        lastMousePos = Input.MousePoint;
                        node = null;
                        switch (results.Feature)
                        {
                            case EFeature.Tetra:
                                {
                                    Tetra tet = results.Body.Tetras[results.Index];
                                    node = tet.Nodes[0];
                                    for (int i = 1; i < 4; ++i)
                                    {
                                        if ((node.Position - impact).LengthSquared >
                                            (tet.Nodes[i].Position - impact).LengthSquared)
                                        {
                                            node = tet.Nodes[i];
                                        }
                                    }
                                    break;
                                }
                            case EFeature.Face:
                                {
                                    Face f = results.Body.Faces[results.Index];
                                    node = f.N[0];
                                    for (int i = 1; i < 3; ++i)
                                    {
                                        if ((node.Position - impact).LengthSquared >
                                            (f.N[i].Position - impact).LengthSquared)
                                        {
                                            node = f.N[i];
                                        }
                                    }
                                }
                                break;
                        }
                        if (node != null)
                            goal = node.Position;
                        //return;
                    }
                }
            }
            else if (Input.MouseReleased == MouseButtons.Right)
            {
                if ((!drag) && cutting && (results.Fraction < 1))
                {
                    using (ImplicitSphere isphere = new ImplicitSphere(ref impact, 1))
                    {
                        results.Body.Refine(isphere, 0.0001f, true);
                    }
                }
                results.Fraction = 1;
                drag = false;
            }

            // Mouse movement
            if (Input.MouseDown == MouseButtons.Right)
            {
                if (node != null && (results.Fraction < 1))
                {
                    if (!drag)
                    {
                        int x = Input.MousePoint.X - lastMousePos.X;
                        int y = Input.MousePoint.Y - lastMousePos.Y;
                        if ((x * x) + (y * y) > 6)
                        {
                            drag = true;
                        }
                    }
                    if (drag)
                    {
                        lastMousePos = Input.MousePoint;
                    }
                }
            }

            base.OnHandleInput();
        }
Exemplo n.º 7
0
        protected override void OnHandleInput()
        {
            base.OnHandleInput();

            if (Input.MousePressed == MouseButtons.Right)
            {
                results.Fraction = 1;
                if (pickConstraint == null)
                {
                    Vector3 rayFrom = Freelook.Eye;
                    Vector3 rayTo = GetRayTo(Input.MousePoint, Freelook.Eye, Freelook.Target, FieldOfView);
                    Vector3 rayDir = (rayTo - rayFrom);
                    rayDir.Normalize();
                    AlignedSoftBodyArray sbs = ((SoftRigidDynamicsWorld)PhysicsContext.World).SoftBodyArray;
                    for (int ib = 0; ib < sbs.Count; ++ib)
                    {
                        SoftBody psb = sbs[ib];
                        SRayCast res = new SRayCast();
                        if (psb.RayTest(rayFrom, rayTo, res))
                        {
                            results = res;
                        }
                    }
                    if (results.Fraction < 1)
                    {
                        impact = rayFrom + (rayTo - rayFrom) * results.Fraction;
                        drag = !(PhysicsContext as Physics).cutting;
                        lastMousePos = Input.MousePoint;
                        node = null;
                        switch (results.Feature)
                        {
                            case EFeature.Tetra:
                                {
                                    Tetra tet = results.Body.Tetras[results.Index];
                                    node = tet.Nodes[0];
                                    for (int i = 1; i < 4; ++i)
                                    {
                                        if ((node.X - impact).LengthSquared() >
                                            (tet.Nodes[i].X - impact).LengthSquared())
                                        {
                                            node = tet.Nodes[i];
                                        }
                                    }
                                    break;
                                }
                            case EFeature.Face:
                                {
                                    Face f = results.Body.Faces[results.Index];
                                    node = f.N[0];
                                    for (int i = 1; i < 3; ++i)
                                    {
                                        if ((node.X - impact).LengthSquared() >
                                            (f.N[i].X - impact).LengthSquared())
                                        {
                                            node = f.N[i];
                                        }
                                    }
                                }
                                break;
                        }
                        if (node != null)
                            goal = node.X;
                        //return;
                    }
                }
            }
            else if (Input.MouseReleased == MouseButtons.Right)
            {
                if ((!drag) && (PhysicsContext as Physics).cutting && (results.Fraction < 1))
                {
                    ImplicitSphere isphere = new ImplicitSphere(impact, 1);
                    results.Body.Refine(isphere, 0.0001f, true);
                }
                results.Fraction = 1;
                drag = false;
            }

            // Mouse movement
            if (Input.MouseDown == MouseButtons.Right)
            {
                if (node != null && (results.Fraction < 1))
                {
                    if (!drag)
                    {
                        int x = Input.MousePoint.X - lastMousePos.X;
                        int y = Input.MousePoint.Y - lastMousePos.Y;
                        if ((x * x) + (y * y) > 6)
                        {
                            drag = true;
                        }
                    }
                    if (drag)
                    {
                        lastMousePos = Input.MousePoint;
                    }
                }
            }

            (PhysicsContext as Physics).HandleInput(Input, FrameDelta);
        }