Пример #1
0
        public void CreateDecal(Vector3 decalPosition, BoundingBox area, string textureName, float lifetime, float size, DecalLayers layer)
        {
            Texture2D texture = Renderer.Instance.LookupTexture(textureName);
            Decal decal = new Decal(ref decalPosition, texture, lifetime, size, layer);

            Vector3 difference = area.Max - area.Min;
            float length = difference.Z;
            float height = difference.Y;
            float width = difference.X;

            BEPUphysics.CollisionShapes.ConvexShapes.SphereShape sphereShape = new BEPUphysics.CollisionShapes.ConvexShapes.SphereShape(width);
            //BEPUphysics.CollisionShapes.ConvexShapes.BoxShape boxShape = new BEPUphysics.CollisionShapes.ConvexShapes.BoxShape(width, height, length);
            BEPUphysics.MathExtensions.RigidTransform startingTransform = new BEPUphysics.MathExtensions.RigidTransform(decalPosition);
            Vector3 sweep = Vector3.Zero;

            // area is is local space, transform to world space
            area.Min += decalPosition;
            area.Max += decalPosition;

            // use the broad phase's acceleration structure to find all the objects which are in the decal area
            var candidates = BEPUphysics.ResourceManagement.Resources.GetBroadPhaseEntryList();
            decalsWorld.BroadPhase.QueryAccelerator.GetEntries(area, candidates);

            foreach (BEPUphysics.BroadPhaseEntries.BroadPhaseEntry candidate in candidates)
            {
                if (candidate.OwningActor != null)
                {
                    BEPUphysics.RayHit rayHit;
                    bool gotHit = false;
                    Vector3 location = Vector3.Zero;
                    Vector3 up = Vector3.Up;

                    for(int i = 0; i < cardinalRays.Count; i++)
                    {
                        Ray ray = cardinalRays[i];
                        ray.Position = decalPosition;
                        if (candidate.RayCast(ray, width, out rayHit))
                        {
                            gotHit = true;
                            location = rayHit.Location;
                            up = ups[i];
                            break;
                        }
                    }

                    if (!gotHit)
                    {
                        gotHit = true;
                        Actor actor = (Actor)candidate.OwningActor;
                        location = actor.PhysicsObject.Position;
                        //if (candidate.ConvexCast(sphereShape, ref startingTransform, ref sweep, out rayHit))
                        //{
                        //    gotHit = true;
                        //    location = rayHit.Location;
                        //}
                    }

                    if (gotHit)
                    {
                        // apply the decal
                        Actor actor = (Actor)candidate.OwningActor;
                        if (!decal.ApplyToActor(actor, location, up))
                            break;
                    }
                }
            }

            AddDecal(decal);
        }
Пример #2
0
        public void CreateDecal(Ray ray, float length, string textureName, float lifetime, float size, DecalLayers layer)
        {
            Texture2D texture = Renderer.Instance.LookupTexture(textureName);
            Decal decal = new Decal(ref ray.Position, texture, lifetime, size, layer);

            // collide the ray with the world, apply decal to everything it hits
            IList<BEPUphysics.RayCastResult> results = new List<BEPUphysics.RayCastResult>();
            decalsWorld.RayCast(ray, length, results);

            foreach (BEPUphysics.RayCastResult result in results)
            {
                if (result.HitObject.OwningActor != null)
                {
                    // apply the decal
                    if (!decal.ApplyToActor((Actor)result.HitObject.OwningActor, result.HitData.Location, Vector3.Backward))
                        break;
                }
            }

            AddDecal(decal);
        }