示例#1
0
        private Node CreateBall(Vector3 pos)
        {
            var cmp = new MyBall ();

            var spr = new Sprite (Resource.GetTexture ("media/Earth-32x32.png"), 32, 32);

            var col = new SphereCollision (16);
            col.SetOffset (col.Radius, col.Radius, 0);

            var body = new PhysicsBody ();
            body.Type = ColliderType.Dynamic;
            body.SetShape (col);
            body.SetMaterial (new PhysicsMaterial ());
            body.ApplyForce (50000, 0, 0);

            var node = new Node ("Ball " + ballIndex++);
            node.Attach (spr);
            node.Attach (cmp);
            node.Attach (col);
            node.Attach (body);

            node.Translation = pos;

            return node;
        }
 public void UpdateSphereCollision(int index, SphereCollision col)
 {
     if (index < sphereCollisions.Length)
     {
         sphereCollisions[index] = col;
     }
 }
示例#3
0
        public static Boolean GetCollision(SphereCollision collidableA, RingCollision collidableB, out Vector3 position,
                                           out Vector3 normal)
        {
            // first let's get the collision parameters from the Plane Collision.
            SphereOnPlaneCollision.GetCollision(collidableA, collidableB, out var planeCollision, out var planeNormal,
                                                out var scale1, out var scale2);

            // Now get distance from our origin to the point planeCollision

            // transformation matrix 1
            var tm1 = collidableA.Parent.ScaleMatrix * collidableA.Parent.RotationMatrix;

            // transformation matrix 2
            var tm2 = collidableB.Parent.ScaleMatrix * collidableB.Parent.RotationMatrix;

            var sv1 = Vector3.Transform(collidableB.SpanVector1, tm2);
            var sv2 = Vector3.Transform(collidableB.SpanVector2, tm2);

            var distanceOnPlane = (scale1 * sv1 + scale2 * sv2).Length();

            var ringNorm = (scale1 * scale1 + scale2 * scale2) * collidableB.Radius;

            var ringScale1 = scale1 / ringNorm;
            var ringScale2 = scale2 / ringNorm;

            position = collidableB.Position + Vector3.Transform(collidableB.Origin, tm2) + ringScale1 * sv1 + ringScale2 * sv2;

            normal = collidableA.Position - position;
            var collide = normal.Length() < collidableA.Radius + collidableB.InnerRadius;

            normal.Normalize();

            return(collide);
        }
示例#4
0
        public static RayCollisionPoint GetCollision(Ray ray, SphereCollision sphere)
        {
            // https://gamedev.stackexchange.com/questions/96459/fast-ray-sphere-collision-code

            // distance from ray start to center of sphere
            Vector3 distance = ray.Position - sphere.Position;

            float radiusSquared = sphere.Radius * sphere.Radius;
            float pd            = Vector3.Dot(distance, ray.Direction);

            if (pd > 0 || Vector3.Dot(distance, distance) < radiusSquared)
            {
                return(new RayCollisionPoint());                // no collision
            }
            Vector3 a = distance - pd * ray.Direction;

            float aSquared = Vector3.Dot(a, a);

            if (aSquared > radiusSquared)
            {
                return(new RayCollisionPoint());
            }

            float h = (float)Math.Sqrt(radiusSquared - aSquared);

            Vector3 i = a - h * ray.Direction;

            return(new RayCollisionPoint(sphere.Position + i, i, h));
        }
        public static Boolean GetCollision(SphereCollision collidableA, SphereCollision collidableB, out Vector3 position,
                                           out Vector3 normal)
        {
            var radiusDist = collidableA.Radius * collidableA.Parent.Scale.X + collidableB.Radius * collidableB.Parent.Scale.X;
            var dist       = collidableA.Position - collidableB.Position;
            var rd2        = radiusDist * radiusDist;
            var dist2      = dist.LengthSquared();

            if (dist2 >= rd2 - 0.0001f)
            {
                // no collision
                position = new Vector3();
                normal   = new Vector3();
                return(false);
            }

            // we got a collision.
            // normal vector should always be from A to B, thus B collides with A and needs to exit.
            normal = dist;
            normal.Normalize();

            position = collidableB.Position + normal * collidableB.Radius * collidableB.Parent.Scale.X;

            return(true);
        }
示例#6
0
 void Start()
 {
     collisionCheck         = GetComponent <SphereCollision>();
     Fallingspeed           = 0.1f;
     JumpSpeed              = 0f;
     SoundSourceSphere      = GetComponent <AudioSource>();
     SoundSourceSphere.clip = AudioContainer.au_BackBeat;
     SoundSourceSphere.Play();
 }
示例#7
0
        public static Boolean GetCollision(SphereCollision collidableA, BoundEdgeCollision collidableB, out Vector3 position,
                                           out Vector3 normal, out float scale, out float distance)
        {
            if (SphereOnEdgeCollision.GetCollision(collidableA, collidableB, out position, out normal, out scale, out distance))
            {
                return(collidableB.Restriction(scale, distance));
            }

            return(false);
        }
示例#8
0
        public static Boolean GetCollision(SphereCollision collidableA, BoundPlaneCollision collidableB, out Vector3 position,
                                           out Vector3 normal, out float scale1, out float scale2)
        {
            if (SphereOnPlaneCollision.GetCollision(collidableA, collidableB, out position, out normal, out scale1, out scale2))
            {
                //Console.WriteLine($"SOBPC @{collidableB.Position + collidableB.Origin} + {scale1} * {collidableB.SpanVector1} + {scale2} * {collidableB.SpanVector2} = {position}");


                return(collidableB.Restriction(scale1, scale2));
            }

            return(false);
        }
示例#9
0
 void Start()
 {
     Sphere           = FindObjectOfType <SphereCollision>();
     Movement         = Sphere.GetComponent <Movement>();
     collisionCheck   = GetComponent <BoxCollider>();
     timer            = 0f;
     LooseSceneTimer  = 0;
     IsJumping        = false;
     SoundSource      = GetComponent <AudioSource>();
     StartLooseTimer  = false;
     unloadLevelTimer = 0;
     ShallUnloadLevel = false;
     PickedDiamond    = false;
 }
示例#10
0
        public static Boolean GetCollision(SphereCollision collidableA, PlaneCollision collidableB, out Vector3 position,
                                           out Vector3 normal, out float scale1, out float scale2)
        {
            Vector3 eq = VectorMathHelper.SolveLinearEquation(collidableB.Normal / collidableB.Normal.Length(), collidableB.SpanVector1,
                                                              collidableB.SpanVector2, collidableA.Position - collidableB.Origin);

            // get the point of collision
            position = collidableB.Origin + eq.Y * collidableB.SpanVector1 + eq.Z * collidableB.SpanVector2;
            normal   = (eq.X < 0 ? -1 : 1) * collidableB.Normal;           // invert normal if the scalar is negative

            scale1 = eq.Y;
            scale2 = eq.Z;

            return(Math.Abs(eq.X) < collidableA.Radius);
        }
示例#11
0
        public static Node Create()
        {
            var cmp = new MyTullet ();

            var spr = new Sprite (40, 72);
            spr.SetOffset (-20, -52);

            var col = new SphereCollision (cmp.effectiveRange);
            //col.DrawEnabled = true;

            var node = new Node ("Tullet");
            node.Attach (cmp);
            node.Attach (spr);
            node.Attach (col);

            node.DrawPriority = -2;
            node.Translate (0, -8, 0);

            return node;
        }
示例#12
0
        public static Node Create(Vector3 pos, Quaternion rot)
        {
            var cmp = new MyProjectile ();

            var spr = new Sprite (16, 32);
            spr.AddTexture (Resource.GetTexture("media/Projectile.png"));
            spr.SetOffset (-8, 16);

            var col = new SphereCollision (10);
            //col.DrawEnabled = true;

            var node = new Node ("Projectile");
            node.Attach (cmp);
            node.Attach (spr);
            node.Attach (col);

            node.Translation = pos;
            node.Rotation = rot;

            return node;
        }
        public static Boolean GetCollision(SphereCollision collidableA, EdgeCollision collidableB, out Vector3 position,
                                           out Vector3 normal, out float scale, out float distance)
        {
            // transformation matrix 1
            var tm = collidableB.Parent.ScaleMatrix * collidableB.Parent.RotationMatrix;

            var pos    = collidableA.Position;
            var origin = collidableB.Position + Vector3.Transform(collidableB.Origin, tm);
            var sv     = Vector3.Transform(collidableB.SpanVector, tm);

            scale = (sv.X * (pos.X - origin.X) + sv.Y * (pos.Y - origin.Y) + sv.Z * (pos.Z - origin.Z)) /
                    (sv.X * sv.X + sv.Y * sv.Y + sv.Z * sv.Z);

            //Console.WriteLine($"{scale}");

            position = origin + scale * sv;
            normal   = pos - position;
            normal.Normalize();

            distance = (pos - position).Length();

            return(distance < collidableB.Distance + collidableA.Radius);
        }
 public static Vector3 HandleCollision(SphereCollision collider, SphereCollision collidable, Vector3 position, Vector3 normal)
 {
     return(position + normal * collider.Radius);
 }
示例#15
0
 void Start()
 {
     explosionRadius = GetComponent <SphereCollision> ();
     anim            = GetComponent <Animator> ();
     audioSource     = GetComponent <AudioSource> ();
 }
示例#16
0
        //        internal TransformSceneHandle targetEffector;

        public void SetUp()
        {
            initialized = 0;
            int length = joints.Length;

            edges            = new NativeArray <Edge>(length, Allocator.Persistent);
            positions        = new NativeArray <float3>(length, Allocator.Persistent);
            velocities       = new NativeArray <float3>(length, Allocator.Persistent);
            forces           = new NativeArray <float3>(length, Allocator.Persistent);
            masses           = new NativeArray <float>(length, Allocator.Persistent);
            flags            = new NativeArray <int>(length, Allocator.Persistent);
            constraints      = new NativeArray <Constraint>(2, Allocator.Persistent);
            sphereCollisions = new NativeArray <SphereCollision>(1, Allocator.Persistent);


            for (int i = 0; i < sphereCollisions.Length; i++)
            {
                sphereCollisions[i] = new SphereCollision()
                {
                    enabled        = 0,
                    centerPosition = float3.zero,
                    radious        = 0.0f
                };
            }
            for (int i = 0; i < flags.Length; i++)
            {
                flags[i] = 0;
            }
            flags[0]       = 0x8000;
            constraints[0] = new Constraint()
            {
                pointIndex    = 0,
                fixedPosition = rootOffset,
                fixedVelocity = float3.zero
            };
            constraints[1] = new Constraint()
            {
                pointIndex    = -1,
                fixedPosition = rootOffset,
                fixedVelocity = float3.zero
            };
            vectorData = new NativeArray <float3>(3, Allocator.Persistent);
            gravity    = new float3(0.0f, -9.8f, 0.0f);
#if false
            stiffness = 500.0f; //
                                //            restLength = 1.0f;
            dampingCoefficient = 1.0f;
            dragCoefficient    = 0.1f;

            restitutionCoefficient = 0.3f;
#else
            stiffness = 1300.0f; //
                                 //            restLength = 1.0f;
            dampingCoefficient = 7.0f;
            dragCoefficient    = 2.1f;

            restitutionCoefficient = 0.3f;
#endif
            UnitVector =
                new float3(baseAxis == BaseAxis.X ? 1.0f : 0.0f,
                           baseAxis == BaseAxis.Y ? 1.0f : 0.0f,
                           baseAxis == BaseAxis.Z ? 1.0f : 0.0f);

            MakeChain(length);
            baseAxis = AnimationJob.BaseAxis.Y;
            timeIntervalInSeconds  = 1 / 30.0f; // Time.fixedDeltaTime;
            resolution             = 0.5f;
            timeIntervalInSeconds *= resolution;
            elapsed = 0.0f;
        }
示例#17
0
 public static Vector3 HandleCollision(SphereCollision collider, BoundPlaneCollision collidable, Vector3 position, Vector3 normal)
 {
     return(SphereOnPlaneCollision.HandleCollision(collider, collidable, position, normal));
 }
示例#18
0
 public static Boolean GetCollision(SphereCollision collidableA, PlaneCollision collidableB, out Vector3 position,
                                    out Vector3 normal)
 {
     return(GetCollision(collidableA, collidableB, out position, out normal, out var f1, out var f2));
 }
 void Start()
 {
     explosionRadius = GetComponent<SphereCollision> ();
     anim = GetComponent<Animator> ();
     audioSource = GetComponent<AudioSource> ();
 }
示例#20
0
 protected override void Start()
 {
     base.Start();
     sphereCollider = gameObject.AddComponent <SphereCollision>();
     planeCollider  = gameObject.AddComponent <PlaneCollision>();
 }
 public static Vector3 HandleCollision(SphereCollision collider, EdgeCollision collidable, Vector3 position, Vector3 normal)
 {
     return(position + normal * (collidable.Distance + collider.Radius));
 }
示例#22
0
 public static Vector3 HandleCollision(SphereCollision collider, RingCollision collidable, Vector3 position, Vector3 normal)
 {
     return(position + normal * (collider.Radius + collidable.InnerRadius));
 }