Exemple #1
0
        public override Contact GenerateContact(RigidBody2D rb, float dt)
        {
            //Profiler says this method takes up a lot of processing time (AKA, it's run quite often)
            if (rb as CircleBody != null)
            {
                CircleBody c = rb as CircleBody;
                Vector2 normal = rb.Pos - this.Pos;
                float normLen = normal.Length();
                float dist = normLen - (this.radius + c.radius);
                normal /= normLen;
                Vector2 pa = this.Pos + normal * this.radius;
                Vector2 pb = rb.Pos - normal * c.radius;

                return new Contact(normal, dist, this, rb, pa, pb);
            }
            else if (rb as LineBody != null)
            {
                LineBody pb = rb as LineBody;
                return pb.GenerateContact(this, dt);
            }
            else if (rb as AABBBody != null)
            {
                AABBBody b = rb as AABBBody;
                Vector2 closestPt = Intersection.ClosestPointAABBPt(this.Pos, b);
                Vector2 normal = closestPt - this.Pos;
                float normLen = normal.Length();
                normal /= normLen;

                return new Contact(normal, normLen - this.Radius, this, b, this.Pos + (normal * this.Radius), closestPt);
            }
            else
            {
                throw new NotImplementedException();
            }
        }
 public BallBehavior(TextBlock scoreText)
     : base("BallBehavior")
 {
     body = null;
     this.scoreText = scoreText;
     score = 0;
 }
Exemple #3
0
 public void AddRigidBody(RigidBody2D rb)
 {
     if (rb as LineBody != null)
     {
         lines.Add(rb);
     }
     else
     {
         bodies.Add(rb);
     }
 }
Exemple #4
0
        public Contact(Vector2 normal, float d, RigidBody2D a, RigidBody2D b, Vector2 pointA, Vector2 pointB)
        {
            Normal = normal;
            Dist = d;
            A = a;
            B = b;
            this.pointA = pointA;
            this.pointB = pointB;

            //I = (1+e)*N*(Vr • N) / (1/Ma + 1/Mb)
            //Impulse = (Vector2.Dot(b.Vel - a.Vel, normal) / (a.InvMass + b.InvMass));
            Impulse = 0;
        }
Exemple #5
0
        public override Contact GenerateContact(RigidBody2D rb, float dt)
        {
            if (rb as CircleBody != null)
            {
                CircleBody c = rb as CircleBody;
                float dist = DistanceToPoint(c.Pos) - c.Radius;

                Vector2 pointOnPlane = c.Pos - (normal * (dist+c.Radius));
                Vector2 pointOnBall = c.Pos - (normal * c.Radius);

                return new Contact(normal, dist, this, rb, pointOnPlane, pointOnBall);
            }
            else if (rb as LineBody != null)
            {
                return new Contact();
            }
            else
            {
                throw new NotImplementedException();
            }
        }
Exemple #6
0
 public override void SetPrefab(Constraint2DPrefab instance, RigidBody2D value)
 {
     instance.OtherBody = value;
 }
 public override void _Ready()
 {
     Target = GetNode <RigidBody2D>(TargetPath);
 }
Exemple #8
0
        static void Main(string[] args)
        {
            // Initialize mass properties
            MassProperties massProp        = new MassProperties(1, 1);
            Guid           structureMassId = Guid.NewGuid();

            massProp.SetMass(structureMassId, 4280);                           // kg, structure weight without fuel
            SimpleGravity gravity = new SimpleGravity(new Vector2D(0, -1.62)); // m/s^2

            // Initialize lander
            Vector2D    initalPosition     = new Vector2D(0, 3000);
            Vector2D    initialVelocity    = new Vector2D(0, -20);
            double      initialOrientation = Util.DegToRad(45);
            RigidBody2D lander             = new RigidBody2D(massProp, initalPosition, initialVelocity, initialOrientation, 0);

            lander.Gravity = gravity;

            // Initialize fuel tanks
            FuelTank rcsTank  = new FuelTank(massProp, 633);
            FuelTank mainTank = new FuelTank(massProp, 10334);

            // Initialize engines
            Vector2D engineMountPoint = new Vector2D(0, -4); // m
            double   orientation      = Util.DegToRad(-90);
            double   maxThrust        = 45040;               // N
            double   fuelUsage        = 13;                  // kg/s, guess
            Thruster mainEngine       = new Thruster(massProp, lander, mainTank, engineMountPoint, orientation, maxThrust, fuelUsage, true);

            // Thrusters
            List <Vector2D> rcsMountPoints = new List <Vector2D>()
            {
                new Vector2D(-5, 0), new Vector2D(-5, 0), new Vector2D(5, 0), new Vector2D(5, 0)
            };
            List <double> rcsOrientations = new List <double>()
            {
                90, -90, 90, -90
            };
            List <Thruster> rcsThrusters = new List <Thruster>();
            double          rcsMaxThrust = 1760;
            double          rcsFuelUsage = 15; // kg/s

            for (int i = 0; i < 4; i++)
            {
                rcsThrusters.Add(new Thruster(massProp, lander, rcsTank, rcsMountPoints[i], rcsOrientations[i], rcsMaxThrust, rcsFuelUsage, false));
            }

            // Flight control
            LanderFlightControl flightControl = new LanderFlightControl(lander, mainEngine, rcsThrusters);

            // logging
            Logger log = new Logger(5);

            log.MainEngine = mainEngine;
            log.RigidBody  = lander;

            // Initialize scheduler
            EqualScheduler scheduler = new EqualScheduler(40); //hz

            scheduler.Add(flightControl, 0);
            scheduler.Add(mainEngine, 10);

            for (int i = 0; i < 4; i++)
            {
                scheduler.Add(rcsThrusters[i], 20 + i);
            }

            scheduler.Add(lander, 40);
            scheduler.Add(log, 50);

            // run sim
            scheduler.Run(TimeSpan.FromSeconds(125));
            log.WriteCSV("simdata_" + DateTime.Now.ToString("yyyyMMddTHHmmss") + ".csv");
        }
Exemple #9
0
 // Start is called before the first frame update
 void Start()
 {
     rb2d = GetComponent <RigidBody2D>();
     invoke("GoBall", 2);
 }
Exemple #10
0
        void CreateScene()
        {
            scene = new Scene();
            scene.CreateComponent <Octree>();
            scene.CreateComponent <DebugRenderer>();
            // Create camera node
            CameraNode = scene.CreateChild("Camera");
            // Set camera's position
            CameraNode.Position = (new Vector3(0.0f, 0.0f, -10.0f));

            Camera camera = CameraNode.CreateComponent <Camera>();

            camera.Orthographic = true;

            var graphics = Graphics;

            camera.OrthoSize = (float)graphics.Height * PixelSize;
            camera.Zoom      = 1.2f * Math.Min((float)graphics.Width / 1280.0f, (float)graphics.Height / 800.0f); // Set zoom according to user's resolution to ensure full visibility (initial zoom (1.2) is set for full visibility at 1280x800 resolution)

            // Create 2D physics world component
            scene.CreateComponent <PhysicsWorld2D>();

            var      cache      = ResourceCache;
            Sprite2D boxSprite  = cache.GetSprite2D("Urho2D/Box.png");
            Sprite2D ballSprite = cache.GetSprite2D("Urho2D/Ball.png");

            // Create ground.
            Node groundNode = scene.CreateChild("Ground");

            groundNode.Position = (new Vector3(0.0f, -3.0f, 0.0f));
            groundNode.Scale    = new Vector3(200.0f, 1.0f, 0.0f);

            // Create 2D rigid body for gound
            /*RigidBody2D groundBody = */
            groundNode.CreateComponent <RigidBody2D>();

            StaticSprite2D groundSprite = groundNode.CreateComponent <StaticSprite2D>();

            groundSprite.Sprite = boxSprite;

            // Create box collider for ground
            CollisionBox2D groundShape = groundNode.CreateComponent <CollisionBox2D>();

            // Set box size
            groundShape.Size = new Vector2(0.32f, 0.32f);
            // Set friction
            groundShape.Friction = 0.5f;

            for (uint i = 0; i < NumObjects; ++i)
            {
                Node node = scene.CreateChild("RigidBody");
                node.Position = (new Vector3(NextRandom(-0.1f, 0.1f), 5.0f + i * 0.4f, 0.0f));

                // Create rigid body
                RigidBody2D body = node.CreateComponent <RigidBody2D>();
                body.BodyType = BodyType2D.Dynamic;
                StaticSprite2D staticSprite = node.CreateComponent <StaticSprite2D>();

                if (i % 2 == 0)
                {
                    staticSprite.Sprite = boxSprite;

                    // Create box
                    CollisionBox2D box = node.CreateComponent <CollisionBox2D>();
                    // Set size
                    box.Size = new Vector2(0.32f, 0.32f);
                    // Set density
                    box.Density = 1.0f;
                    // Set friction
                    box.Friction = 0.5f;
                    // Set restitution
                    box.Restitution = 0.1f;
                }
                else
                {
                    staticSprite.Sprite = ballSprite;

                    // Create circle
                    CollisionCircle2D circle = node.CreateComponent <CollisionCircle2D>();
                    // Set radius
                    circle.Radius = 0.16f;
                    // Set density
                    circle.Density = 1.0f;
                    // Set friction.
                    circle.Friction = 0.5f;
                    // Set restitution
                    circle.Restitution = 0.1f;
                }
            }
        }
        void CreateScene()
        {
            scene = new Scene();
            scene.CreateComponent <Octree>();
            scene.CreateComponent <DebugRenderer>();
            PhysicsWorld2D physicsWorld = scene.CreateComponent <PhysicsWorld2D>(); // Create 2D physics world component

            physicsWorld.DrawJoint = true;                                          // Display the joints (Note that DrawDebugGeometry() must be set to true to acually draw the joints)
            drawDebug = true;                                                       // Set DrawDebugGeometry() to true

            // Create camera
            CameraNode = scene.CreateChild("Camera");
            // Set camera's position
            CameraNode.Position = (new Vector3(0.0f, 0.0f, 0.0f)); // Note that Z setting is discarded; use camera.zoom instead (see MoveCamera() below for example)

            camera = CameraNode.CreateComponent <Camera>();
            camera.Orthographic = true;

            var graphics = GetSubsystem <Graphics>();

            camera.OrthoSize = (float)graphics.Height * PixelSize;
            camera.Zoom      = 1.2f * Math.Min((float)graphics.Width / 1280.0f, (float)graphics.Height / 800.0f); // Set zoom according to user's resolution to ensure full visibility (initial zoom (1.2) is set for full visibility at 1280x800 resolution)

            // Set up a viewport to the Renderer subsystem so that the 3D scene can be seen
            Viewport viewport = new Viewport(scene, camera);
            Renderer renderer = GetSubsystem <Renderer>();

            renderer.SetViewport(0, viewport);

            Zone zone = renderer.DefaultZone;

            zone.FogColor = (new Color(0.1f, 0.1f, 0.1f)); // Set background color for the scene

            // Create 4x3 grid
            for (uint i = 0; i < 5; ++i)
            {
                Node        edgeNode = scene.CreateChild("VerticalEdge");
                RigidBody2D edgeBody = edgeNode.CreateComponent <RigidBody2D>();
                if (dummyBody == null)
                {
                    dummyBody = edgeBody; // Mark first edge as dummy body (used by mouse pick)
                }
                CollisionEdge2D edgeShape = edgeNode.CreateComponent <CollisionEdge2D>();
                edgeShape.SetVertices(new Vector2(i * 2.5f - 5.0f, -3.0f), new Vector2(i * 2.5f - 5.0f, 3.0f));
                edgeShape.Friction = 0.5f; // Set friction
            }

            for (uint j = 0; j < 4; ++j)
            {
                Node edgeNode = scene.CreateChild("HorizontalEdge");
                /*RigidBody2D edgeBody = */
                edgeNode.CreateComponent <RigidBody2D>();
                CollisionEdge2D edgeShape = edgeNode.CreateComponent <CollisionEdge2D>();
                edgeShape.SetVertices(new Vector2(-5.0f, j * 2.0f - 3.0f), new Vector2(5.0f, j * 2.0f - 3.0f));
                edgeShape.Friction = 0.5f; // Set friction
            }

            var cache = GetSubsystem <ResourceCache>();

            // Create a box (will be cloned later)
            Node box = scene.CreateChild("Box");

            box.Position = (new Vector3(0.8f, -2.0f, 0.0f));
            StaticSprite2D boxSprite = box.CreateComponent <StaticSprite2D>();

            boxSprite.Sprite = cache.Get <Sprite2D>("Urho2D/Box.png");
            RigidBody2D boxBody = box.CreateComponent <RigidBody2D>();

            boxBody.BodyType       = BodyType2D.BT_DYNAMIC;
            boxBody.LinearDamping  = 0.0f;
            boxBody.AngularDamping = 0.0f;
            CollisionBox2D shape = box.CreateComponent <CollisionBox2D>(); // Create box shape

            shape.Size        = new Vector2(0.32f, 0.32f);                 // Set size
            shape.Density     = 1.0f;                                      // Set shape density (kilograms per meter squared)
            shape.Friction    = 0.5f;                                      // Set friction
            shape.Restitution = 0.1f;                                      // Set restitution (slight bounce)

            // Create a ball (will be cloned later)
            Node ball = scene.CreateChild("Ball");

            ball.Position = (new Vector3(1.8f, -2.0f, 0.0f));
            StaticSprite2D ballSprite = ball.CreateComponent <StaticSprite2D>();

            ballSprite.Sprite = cache.Get <Sprite2D>("Urho2D/Ball.png");
            RigidBody2D ballBody = ball.CreateComponent <RigidBody2D>();

            ballBody.BodyType       = BodyType2D.BT_DYNAMIC;
            ballBody.LinearDamping  = 0.0f;
            ballBody.AngularDamping = 0.0f;
            CollisionCircle2D ballShape = ball.CreateComponent <CollisionCircle2D>(); // Create circle shape

            ballShape.Radius      = 0.16f;                                            // Set radius
            ballShape.Density     = 1.0f;                                             // Set shape density (kilograms per meter squared)
            ballShape.Friction    = 0.5f;                                             // Set friction
            ballShape.Restitution = 0.6f;                                             // Set restitution: make it bounce

            // Create a polygon
            Node polygon = scene.CreateChild("Polygon");

            polygon.Position = (new Vector3(1.6f, -2.0f, 0.0f));
            polygon.SetScale(0.7f);
            StaticSprite2D polygonSprite = polygon.CreateComponent <StaticSprite2D>();

            polygonSprite.Sprite = cache.Get <Sprite2D>("Urho2D/Aster.png");
            RigidBody2D polygonBody = polygon.CreateComponent <RigidBody2D>();

            polygonBody.BodyType = BodyType2D.BT_DYNAMIC;
            CollisionPolygon2D polygonShape = polygon.CreateComponent <CollisionPolygon2D>();

            polygonShape.VertexCount = 6; // Set number of vertices (mandatory when using SetVertex())
            polygonShape.SetVertex(0, new Vector2(-0.8f, -0.3f));
            polygonShape.SetVertex(1, new Vector2(0.5f, -0.8f));
            polygonShape.SetVertex(2, new Vector2(0.8f, -0.3f));
            polygonShape.SetVertex(3, new Vector2(0.8f, 0.5f));
            polygonShape.SetVertex(4, new Vector2(0.5f, 0.9f));
            polygonShape.SetVertex(5, new Vector2(-0.5f, 0.7f));
            polygonShape.Density     = 1.0f; // Set shape density (kilograms per meter squared)
            polygonShape.Friction    = 0.3f; // Set friction
            polygonShape.Restitution = 0.0f; // Set restitution (no bounce)

            // Create a ConstraintDistance2D
            CreateFlag("ConstraintDistance2D", -4.97f, 3.0f); // Display Text3D flag
            Node        boxDistanceNode  = box.Clone(CreateMode.REPLICATED);
            Node        ballDistanceNode = ball.Clone(CreateMode.REPLICATED);
            RigidBody2D ballDistanceBody = ballDistanceNode.GetComponent <RigidBody2D>();

            boxDistanceNode.Position  = (new Vector3(-4.5f, 2.0f, 0.0f));
            ballDistanceNode.Position = (new Vector3(-3.0f, 2.0f, 0.0f));

            ConstraintDistance2D constraintDistance = boxDistanceNode.CreateComponent <ConstraintDistance2D>(); // Apply ConstraintDistance2D to box

            constraintDistance.OtherBody       = ballDistanceBody;                                              // Constrain ball to box
            constraintDistance.OwnerBodyAnchor = boxDistanceNode.Position2D;
            constraintDistance.OtherBodyAnchor = ballDistanceNode.Position2D;
            // Make the constraint soft (comment to make it rigid, which is its basic behavior)
            constraintDistance.FrequencyHz  = 4.0f;
            constraintDistance.DampingRatio = 0.5f;

            // Create a ConstraintFriction2D ********** Not functional. From Box2d samples it seems that 2 anchors are required, Urho2D only provides 1, needs investigation ***********
            CreateFlag("ConstraintFriction2D", 0.03f, 1.0f); // Display Text3D flag
            Node boxFrictionNode  = box.Clone(CreateMode.REPLICATED);
            Node ballFrictionNode = ball.Clone(CreateMode.REPLICATED);

            boxFrictionNode.Position  = (new Vector3(0.5f, 0.0f, 0.0f));
            ballFrictionNode.Position = (new Vector3(1.5f, 0.0f, 0.0f));

            ConstraintFriction2D constraintFriction = boxFrictionNode.CreateComponent <ConstraintFriction2D>(); // Apply ConstraintDistance2D to box

            constraintFriction.OtherBody = ballFrictionNode.GetComponent <RigidBody2D>();                       // Constraint ball to box

            // Create a ConstraintGear2D
            CreateFlag("ConstraintGear2D", -4.97f, -1.0f);                // Display Text3D flag
            Node        baseNode = box.Clone(CreateMode.REPLICATED);
            RigidBody2D tempBody = baseNode.GetComponent <RigidBody2D>(); // Get body to make it static

            tempBody.BodyType = BodyType2D.BT_STATIC;
            baseNode.Position = (new Vector3(-3.7f, -2.5f, 0.0f));
            Node ball1Node = ball.Clone(CreateMode.REPLICATED);

            ball1Node.Position = (new Vector3(-4.5f, -2.0f, 0.0f));
            RigidBody2D ball1Body = ball1Node.GetComponent <RigidBody2D>();
            Node        ball2Node = ball.Clone(CreateMode.REPLICATED);

            ball2Node.Position = (new Vector3(-3.0f, -2.0f, 0.0f));
            RigidBody2D ball2Body = ball2Node.GetComponent <RigidBody2D>();

            ConstraintRevolute2D gear1 = baseNode.CreateComponent <ConstraintRevolute2D>(); // Apply constraint to baseBox

            gear1.OtherBody = ball1Body;                                                    // Constrain ball1 to baseBox
            gear1.Anchor    = ball1Node.Position2D;
            ConstraintRevolute2D gear2 = baseNode.CreateComponent <ConstraintRevolute2D>(); // Apply constraint to baseBox

            gear2.OtherBody = ball2Body;                                                    // Constrain ball2 to baseBox
            gear2.Anchor    = ball2Node.Position2D;

            ConstraintGear2D constraintGear = ball1Node.CreateComponent <ConstraintGear2D>(); // Apply constraint to ball1

            constraintGear.OtherBody       = ball2Body;                                       // Constrain ball2 to ball1
            constraintGear.OwnerConstraint = gear1;
            constraintGear.OtherConstraint = gear2;
            constraintGear.Ratio           = 1.0f;

            ball1Body.ApplyAngularImpulse(0.015f, true); // Animate

            // Create a vehicle from a compound of 2 ConstraintWheel2Ds
            CreateFlag("ConstraintWheel2Ds compound", -2.45f, -1.0f); // Display Text3D flag
            Node car = box.Clone(CreateMode.REPLICATED);

            car.Scale    = new Vector3(4.0f, 1.0f, 0.0f);
            car.Position = (new Vector3(-1.2f, -2.3f, 0.0f));
            StaticSprite2D tempSprite = car.GetComponent <StaticSprite2D>(); // Get car Sprite in order to draw it on top

            tempSprite.OrderInLayer = 0;                                     // Draw car on top of the wheels (set to -1 to draw below)
            Node ball1WheelNode = ball.Clone(CreateMode.REPLICATED);

            ball1WheelNode.Position = (new Vector3(-1.6f, -2.5f, 0.0f));
            Node ball2WheelNode = ball.Clone(CreateMode.REPLICATED);

            ball2WheelNode.Position = (new Vector3(-0.8f, -2.5f, 0.0f));

            ConstraintWheel2D wheel1 = car.CreateComponent <ConstraintWheel2D>();

            wheel1.OtherBody      = ball1WheelNode.GetComponent <RigidBody2D>();
            wheel1.Anchor         = ball1WheelNode.Position2D;
            wheel1.Axis           = new Vector2(0.0f, 1.0f);
            wheel1.MaxMotorTorque = 20.0f;
            wheel1.FrequencyHz    = 4.0f;
            wheel1.DampingRatio   = 0.4f;

            ConstraintWheel2D wheel2 = car.CreateComponent <ConstraintWheel2D>();

            wheel2.OtherBody      = ball2WheelNode.GetComponent <RigidBody2D>();
            wheel2.Anchor         = ball2WheelNode.Position2D;
            wheel2.Axis           = new Vector2(0.0f, 1.0f);
            wheel2.MaxMotorTorque = 10.0f;
            wheel2.FrequencyHz    = 4.0f;
            wheel2.DampingRatio   = 0.4f;

            // ConstraintMotor2D
            CreateFlag("ConstraintMotor2D", 2.53f, -1.0f); // Display Text3D flag
            Node boxMotorNode = box.Clone(CreateMode.REPLICATED);

            tempBody          = boxMotorNode.GetComponent <RigidBody2D>(); // Get body to make it static
            tempBody.BodyType = BodyType2D.BT_STATIC;
            Node ballMotorNode = ball.Clone(CreateMode.REPLICATED);

            boxMotorNode.Position  = (new Vector3(3.8f, -2.1f, 0.0f));
            ballMotorNode.Position = (new Vector3(3.8f, -1.5f, 0.0f));

            ConstraintMotor2D constraintMotor = boxMotorNode.CreateComponent <ConstraintMotor2D>();

            constraintMotor.OtherBody        = ballMotorNode.GetComponent <RigidBody2D>(); // Constrain ball to box
            constraintMotor.LinearOffset     = new Vector2(0.0f, 0.8f);                    // Set ballNode position relative to boxNode position = (0,0)
            constraintMotor.AngularOffset    = 0.1f;
            constraintMotor.MaxForce         = 5.0f;
            constraintMotor.MaxTorque        = 10.0f;
            constraintMotor.CorrectionFactor = 1.0f;
            constraintMotor.CollideConnected = true; // doesn't work

            // ConstraintMouse2D is demonstrated in HandleMouseButtonDown() function. It is used to "grasp" the sprites with the mouse.
            CreateFlag("ConstraintMouse2D", 0.03f, -1.0f); // Display Text3D flag

            // Create a ConstraintPrismatic2D
            CreateFlag("ConstraintPrismatic2D", 2.53f, 3.0f); // Display Text3D flag
            Node boxPrismaticNode = box.Clone(CreateMode.REPLICATED);

            tempBody          = boxPrismaticNode.GetComponent <RigidBody2D>(); // Get body to make it static
            tempBody.BodyType = BodyType2D.BT_STATIC;
            Node ballPrismaticNode = ball.Clone(CreateMode.REPLICATED);

            boxPrismaticNode.Position  = new Vector3(3.3f, 2.5f, 0.0f);
            ballPrismaticNode.Position = new Vector3(4.3f, 2.0f, 0.0f);

            ConstraintPrismatic2D constraintPrismatic = boxPrismaticNode.CreateComponent <ConstraintPrismatic2D>();

            constraintPrismatic.OtherBody        = ballPrismaticNode.GetComponent <RigidBody2D>(); // Constrain ball to box
            constraintPrismatic.Axis             = new Vector2(1.0f, 1.0f);                        // Slide from [0,0] to [1,1]
            constraintPrismatic.Anchor           = new Vector2(4.0f, 2.0f);
            constraintPrismatic.LowerTranslation = -1.0f;
            constraintPrismatic.UpperTranslation = 0.5f;
            constraintPrismatic.EnableLimit      = true;
            constraintPrismatic.MaxMotorForce    = 1.0f;
            constraintPrismatic.MotorSpeed       = 0.0f;

            // ConstraintPulley2D
            CreateFlag("ConstraintPulley2D", 0.03f, 3.0f); // Display Text3D flag
            Node boxPulleyNode  = box.Clone(CreateMode.REPLICATED);
            Node ballPulleyNode = ball.Clone(CreateMode.REPLICATED);

            boxPulleyNode.Position  = (new Vector3(0.5f, 2.0f, 0.0f));
            ballPulleyNode.Position = (new Vector3(2.0f, 2.0f, 0.0f));

            ConstraintPulley2D constraintPulley = boxPulleyNode.CreateComponent <ConstraintPulley2D>(); // Apply constraint to box

            constraintPulley.OtherBody             = ballPulleyNode.GetComponent <RigidBody2D>();       // Constrain ball to box
            constraintPulley.OwnerBodyAnchor       = boxPulleyNode.Position2D;
            constraintPulley.OtherBodyAnchor       = ballPulleyNode.Position2D;
            constraintPulley.OwnerBodyGroundAnchor = boxPulleyNode.Position2D + new Vector2(0.0f, 1.0f);
            constraintPulley.OtherBodyGroundAnchor = ballPulleyNode.Position2D + new Vector2(0.0f, 1.0f);
            constraintPulley.Ratio = 1.0f; // Weight ratio between ownerBody and otherBody

            // Create a ConstraintRevolute2D
            CreateFlag("ConstraintRevolute2D", -2.45f, 3.0f); // Display Text3D flag
            Node boxRevoluteNode = box.Clone(CreateMode.REPLICATED);

            tempBody          = boxRevoluteNode.GetComponent <RigidBody2D>(); // Get body to make it static
            tempBody.BodyType = BodyType2D.BT_STATIC;
            Node ballRevoluteNode = ball.Clone(CreateMode.REPLICATED);

            boxRevoluteNode.Position  = (new Vector3(-2.0f, 1.5f, 0.0f));
            ballRevoluteNode.Position = (new Vector3(-1.0f, 2.0f, 0.0f));

            ConstraintRevolute2D constraintRevolute = boxRevoluteNode.CreateComponent <ConstraintRevolute2D>(); // Apply constraint to box

            constraintRevolute.OtherBody      = ballRevoluteNode.GetComponent <RigidBody2D>();                  // Constrain ball to box
            constraintRevolute.Anchor         = new Vector2(-1.0f, 1.5f);
            constraintRevolute.LowerAngle     = -1.0f;                                                          // In radians
            constraintRevolute.UpperAngle     = 0.5f;                                                           // In radians
            constraintRevolute.EnableLimit    = true;
            constraintRevolute.MaxMotorTorque = 10.0f;
            constraintRevolute.MotorSpeed     = 0.0f;
            constraintRevolute.EnableMotor    = true;

            // Create a ConstraintRope2D
            CreateFlag("ConstraintRope2D", -4.97f, 1.0f); // Display Text3D flag
            Node boxRopeNode = box.Clone(CreateMode.REPLICATED);

            tempBody          = boxRopeNode.GetComponent <RigidBody2D>();
            tempBody.BodyType = BodyType2D.BT_STATIC;
            Node ballRopeNode = ball.Clone(CreateMode.REPLICATED);

            boxRopeNode.Position  = (new Vector3(-3.7f, 0.7f, 0.0f));
            ballRopeNode.Position = (new Vector3(-4.5f, 0.0f, 0.0f));

            ConstraintRope2D constraintRope = boxRopeNode.CreateComponent <ConstraintRope2D>();

            constraintRope.OtherBody        = ballRopeNode.GetComponent <RigidBody2D>(); // Constrain ball to box
            constraintRope.OwnerBodyAnchor  = new Vector2(0.0f, -0.5f);                  // Offset from box (OwnerBody) : the rope is rigid from OwnerBody center to this ownerBodyAnchor
            constraintRope.MaxLength        = 0.9f;                                      // Rope length
            constraintRope.CollideConnected = true;

            // Create a ConstraintWeld2D
            CreateFlag("ConstraintWeld2D", -2.45f, 1.0f); // Display Text3D flag
            Node boxWeldNode  = box.Clone(CreateMode.REPLICATED);
            Node ballWeldNode = ball.Clone(CreateMode.REPLICATED);

            boxWeldNode.Position  = (new Vector3(-0.5f, 0.0f, 0.0f));
            ballWeldNode.Position = (new Vector3(-2.0f, 0.0f, 0.0f));

            ConstraintWeld2D constraintWeld = boxWeldNode.CreateComponent <ConstraintWeld2D>();

            constraintWeld.OtherBody    = ballWeldNode.GetComponent <RigidBody2D>(); // Constrain ball to box
            constraintWeld.Anchor       = boxWeldNode.Position2D;
            constraintWeld.FrequencyHz  = 4.0f;
            constraintWeld.DampingRatio = 0.5f;

            // Create a ConstraintWheel2D
            CreateFlag("ConstraintWheel2D", 2.53f, 1.0f); // Display Text3D flag
            Node boxWheelNode  = box.Clone(CreateMode.REPLICATED);
            Node ballWheelNode = ball.Clone(CreateMode.REPLICATED);

            boxWheelNode.Position  = (new Vector3(3.8f, 0.0f, 0.0f));
            ballWheelNode.Position = (new Vector3(3.8f, 0.9f, 0.0f));

            ConstraintWheel2D constraintWheel = boxWheelNode.CreateComponent <ConstraintWheel2D>();

            constraintWheel.OtherBody        = ballWheelNode.GetComponent <RigidBody2D>(); // Constrain ball to box
            constraintWheel.Anchor           = ballWheelNode.Position2D;
            constraintWheel.Axis             = new Vector2(0.0f, 1.0f);
            constraintWheel.EnableMotor      = true;
            constraintWheel.MaxMotorTorque   = 1.0f;
            constraintWheel.MotorSpeed       = 0.0f;
            constraintWheel.FrequencyHz      = 4.0f;
            constraintWheel.DampingRatio     = 0.5f;
            constraintWheel.CollideConnected = true; // doesn't work
        }
Exemple #12
0
 public void BodyEntered(RigidBody2D body2D)
 {
     EmitSignal(nameof(GoalReached));
 }
Exemple #13
0
 public Wheel(RigidBody2D rigidBody, ConstraintWheel2D constraint, ParticleEmitter2D particleEmitter, float particlesDistance)
 {
     _rigidBody = rigidBody; _constraint = constraint; _particleEmitter = particleEmitter; _particlesDistance = particlesDistance;
 }
 public ComputerPaddleBehavior(RigidBody2D ballbody)
     : base()
 {
     this.ballbody = ballbody;
 }
Exemple #15
0
        void CreateScene()
        {
            scene = new Scene();
            scene.CreateComponent <Octree>();
            scene.CreateComponent <DebugRenderer>();
            // Create camera node
            CameraNode = scene.CreateChild("Camera");
            // Set camera's position
            CameraNode.Position = new Vector3(0.0f, 5.0f, -10.0f);

            Camera camera = CameraNode.CreateComponent <Camera>();

            camera.Orthographic = true;

            var graphics = Graphics;

            camera.OrthoSize = graphics.Height * 0.05f;
            camera.Zoom      = 1.5f * Math.Min(graphics.Width / 1280.0f, graphics.Height / 800.0f);        // Set zoom according to user's resolution to ensure full visibility (initial zoom (1.5) is set for full visibility at 1280x800 resolution)

            // Create 2D physics world component
            PhysicsWorld2D physicsWorld = scene.CreateComponent <PhysicsWorld2D>();

            physicsWorld.DrawJoint = (true);

            // Create ground
            Node groundNode = scene.CreateChild("Ground");
            // Create 2D rigid body for gound
            RigidBody2D groundBody = groundNode.CreateComponent <RigidBody2D>();
            // Create edge collider for ground
            CollisionEdge2D groundShape = groundNode.CreateComponent <CollisionEdge2D>();

            groundShape.SetVertices(new Vector2(-40.0f, 0.0f), new Vector2(40.0f, 0.0f));

            const float y        = 15.0f;
            RigidBody2D prevBody = groundBody;

            for (uint i = 0; i < NumObjects; ++i)
            {
                Node node = scene.CreateChild("RigidBody");

                // Create rigid body
                RigidBody2D body = node.CreateComponent <RigidBody2D>();
                body.BodyType = BodyType2D.Dynamic;

                // Create box
                CollisionBox2D box = node.CreateComponent <CollisionBox2D>();
                // Set friction
                box.Friction = 0.2f;
                // Set mask bits.
                box.MaskBits = 0xFFFF & ~0x0002;

                if (i == NumObjects - 1)
                {
                    node.Position       = new Vector3(1.0f * i, y, 0.0f);
                    body.AngularDamping = 0.4f;
                    box.SetSize(3.0f, 3.0f);
                    box.Density      = 100.0f;
                    box.CategoryBits = 0x0002;
                }
                else
                {
                    node.Position = new Vector3(0.5f + 1.0f * i, y, 0.0f);
                    box.SetSize(1.0f, 0.25f);
                    box.Density      = 20.0f;
                    box.CategoryBits = 0x0001;
                }

                ConstraintRevolute2D joint = node.CreateComponent <ConstraintRevolute2D>();
                joint.OtherBody        = prevBody;
                joint.Anchor           = new Vector2(i, y);
                joint.CollideConnected = false;

                prevBody = body;
            }

            ConstraintRope2D constraintRope = groundNode.CreateComponent <ConstraintRope2D>();

            constraintRope.OtherBody       = prevBody;
            constraintRope.OwnerBodyAnchor = new Vector2(0.0f, y);
            constraintRope.MaxLength       = NumObjects - 1.0f + 0.01f;
        }
Exemple #16
0
 public ProximityBehaviour(RigidBody2D target, int ticks, int near, int far) : base(target, ticks)
 {
     this.near = near;
     this.far  = far;
 }
 // Called when the node enters the scene tree for the first time.
 public override void _Ready()
 {
     parentRigidBody = GetParent <RigidBody2D>();
 }
Exemple #18
0
 // Start is called before the first frame update
 void Start()
 {
     rb = GetCompnonent <RigidBody2D>();
 }
 public Behaviour(RigidBody2D target, int ticks)
 {
     this.target = target;
     this.ticks  = ticks;
 }
Exemple #20
0
 public override void SetUrho(Constraint2D instance, RigidBody2D value)
 {
     instance.OtherBody = value;
 }
Exemple #21
0
 // Use this for initialization
 void Start()
 {
     rb = GetComponent <Rigidbody2D> ();
 }
 public Constraint2D(RigidBody2D bodyA, RigidBody2D bodyB)
 {
     BodyA = bodyA;
     BodyB = bodyB;
 }
Exemple #23
0
 // Start is called before the first frame update
 void Start()
 {
     _rigidBody2D = GetComponent <RigidBody2D>();
 }
 public DistanceConstraint(RigidBody2D bodyA, RigidBody2D bodyB, float dist)
     : base(bodyA, bodyB)
 {
     distance = dist;
 }
Exemple #25
0
        /// <summary>
        /// Update method
        /// </summary>
        /// <param name="gameTime">game time</param>
        protected override void Update(TimeSpan gameTime)
        {
            this.input = WaveServices.Input;

            KeyboardState currentKeyboardState = this.input.KeyboardState;

            if (currentKeyboardState.IsConnected)
            {
                if (currentKeyboardState.IsKeyPressed(Keys.O) &&
                    this.lastKeyboardState.IsKeyReleased(Keys.O))
                {
                    this.RenderManager.DebugLines = !this.RenderManager.DebugLines;
                }

                this.lastKeyboardState = currentKeyboardState;
            }

            if (this.input.TouchPanelState.IsConnected)
            {
                this.touchState = this.input.TouchPanelState;

                if (this.touchState.Count > 0 && this.mouseJoint == null)
                {
                    this.TouchPosition = this.touchState[0].Position;
                    this.vsm.ToVirtualPosition(ref this.TouchPosition);

                    foreach (Entity entity in this.Owner.Scene.EntityManager.FindAllByTag("Draggable"))
                    {
                        Collider2D collider = entity.FindComponent <Collider2D>(false);
                        if (collider != null)
                        {
                            if (collider.Contain(TouchPosition))
                            {
                                RigidBody2D rigidBody = entity.FindComponent <RigidBody2D>();
                                if (rigidBody != null)
                                {
                                    if (rigidBody.PhysicBodyType == WaveEngine.Common.Physics2D.RigidBodyType2D.Dynamic)
                                    {
                                        this.ConnectedEntity = entity;

                                        //Create Joint
                                        this.mouseJoint = new MouseJoint2D()
                                        {
                                            Target = this.TouchPosition,
                                            //MaxForce = 100,
                                            //DampingRatio = 0.5f,
                                            //FrequencyHz = 2000,
                                        };
                                        this.ConnectedEntity.AddComponent(mouseJoint);

                                        break;
                                    }
                                }
                            }
                        }
                    }
                }

                if (this.touchState.Count == 0 && this.mouseJoint != null)
                {
                    if (!this.ConnectedEntity.IsDisposed)
                    {
                        this.ConnectedEntity.RemoveComponent(this.mouseJoint);
                    }

                    this.mouseJoint = null;
                }

                if (this.mouseJoint != null)
                {
                    this.TouchPosition = this.touchState[0].Position;
                    this.vsm.ToVirtualPosition(ref this.TouchPosition);
                    this.mouseJoint.Target = this.TouchPosition;
                }
            }
        }
Exemple #26
0
        private void ResetEye()
        {
            RigidBody2D body = eye.FindComponent <RigidBody2D>();

            body.ResetPosition(initPosition + EyePosition);
        }
Exemple #27
0
        /// <summary>
        /// Update Method
        /// </summary>
        /// <param name="gameTime">Current Game Time</param>
        protected override void Update(TimeSpan gameTime)
        {
            this.input = WaveServices.Input;

            if (this.input.TouchPanelState.IsConnected)
            {
                this.touchState = this.input.TouchPanelState;

                // Checks Mouse Left Button Click and anyone entity linked
                if (this.touchState.Count > 0 && this.mouseJoint == null)
                {
                    // Udpates Mouse Position
                    this.touchPosition = this.touchState[0].Position;

                    foreach (Entity entity in this.Scene.EntityManager.FindAllByTag("Draggable"))
                    {
                        Collider2D collider = entity.FindComponent <Collider2D>(false);
                        if (collider != null)
                        {
                            // Collider Test
                            if (collider.Contain(touchPosition))
                            {
                                RigidBody2D rigidBody = entity.FindComponent <RigidBody2D>();
                                if (rigidBody != null)
                                {
                                    // Forbiden Mouse Joint of Kinematic Bodies
                                    if (rigidBody.PhysicBodyType == PhysicBodyType.Dynamic)
                                    {
                                        this.connectedEntity = entity;

                                        // Create Mouse Joint
                                        this.mouseJoint = new FixedMouseJoint2D(this.touchPosition);
                                        this.connectedEntity.FindComponent <JointMap2D>().AddJoint("mouseJoint", this.mouseJoint);

                                        // We can break after collider test when true, but we'll miss overlapped entities if Physic entity is
                                        // under a non Physic entity. We are breaking here just for sample.
                                        break;
                                    }
                                }
                            }
                        }
                    }
                }

                // Checks Mouse Left Button Release
                if (this.touchState.Count == 0 && this.mouseJoint != null)
                {
                    if (!this.connectedEntity.IsDisposed)
                    {
                        // Remove Fixed Joint
                        this.connectedEntity.FindComponent <JointMap2D>().RemoveJoint("mouseJoint");
                    }

                    this.mouseJoint = null;
                }

                // If joint exists then update joint anchor position
                if (this.mouseJoint != null)
                {
                    this.touchPosition          = this.touchState[0].Position;
                    this.mouseJoint.WorldAnchor = this.touchPosition;
                }
            }
        }
Exemple #28
0
 public Constraint(RigidBody2D bodyA, RigidBody2D bodyB)
 {
     BodyA = bodyA;
     BodyB = bodyB;
 }
 public override bool TryParsePrefab(string text, out RigidBody2D value)
 {
     throw new System.NotImplementedException();
 }
        /// <summary>
        /// Creates the ball.
        /// </summary>
        private void CreateBall()
        {
            int index = WaveServices.Random.NextBool() ? 1 : 0;
            string textureName = ballTextures[index];

            this.ball = new Entity("ball" + ball_instances++)
            {
                Tag = "ball"
            }
            .AddComponent(new Transform2D()
            {
                Origin = Vector2.Center,
                X = WaveServices.Random.Next(400, 900),
                Y = WaveServices.Random.Next(370, 580),
                DrawOrder = 0.4f,
            })
            .AddComponent(new CircleCollider2D())
            .AddComponent(new Sprite(textureName)
            {
                //TintColor = Color.Yellow,
            })
            .AddComponent(new BallBehavior())
            .AddComponent(new RigidBody2D()
            {
                PhysicBodyType = PhysicBodyType.Static,
                CollisionCategories = Physic2DCategory.Cat2,
                CollidesWith = Physic2DCategory.None,
                Restitution = 0.6f,
            })
            .AddComponent(new SpriteRenderer(DefaultLayers.Alpha));

            this.gamePlayScene.EntityManager.Add(this.ball);

            this.ballTransform = this.ball.FindComponent<Transform2D>();
            this.rigidBodyBall = this.ball.FindComponent<RigidBody2D>();
            this.ballSprite = this.ball.FindComponent<Sprite>();

            // Collision
            this.rigidBodyBall.OnPhysic2DCollision += (s, o) =>
            {
                if (o.Body2DB.Owner.Tag == "Border")
                {
                    this.gamePlayScene.EntityManager.Remove(o.Body2DA.Owner);

                }
            };

            // Start Position
            this.startTransform.X = this.ballTransform.X;
            this.startTransform.Y = this.ballTransform.Y;            
        }
Exemple #31
0
 public RigidBody2DAgent(RigidBody2D _body)
 {
     Body = _body;
     _body.GetTree().Connect("physics_frame", this, "_OnSceneTreeFrame");
 }
Exemple #32
0
 // Start is called before the first frame update
 void Start()
 {
     rb2d = GetComponent <RigidBody2D>();
 }
        void CreateScene()
        {
            scene = new Scene();
            scene.CreateComponent<Octree>();
            scene.CreateComponent<DebugRenderer>();
            PhysicsWorld2D physicsWorld = scene.CreateComponent<PhysicsWorld2D>(); // Create 2D physics world component
            physicsWorld.DrawJoint=true; // Display the joints (Note that DrawDebugGeometry() must be set to true to acually draw the joints)
            drawDebug = true; // Set DrawDebugGeometry() to true

            // Create camera
            CameraNode = scene.CreateChild("Camera");
            // Set camera's position
            CameraNode.Position = (new Vector3(0.0f, 0.0f, 0.0f)); // Note that Z setting is discarded; use camera.zoom instead (see MoveCamera() below for example)

            camera = CameraNode.CreateComponent<Camera>();
            camera.Orthographic = true;

            var graphics = Graphics;

            camera.OrthoSize=(float)graphics.Height * PixelSize;
            camera.Zoom=1.2f * Math.Min((float)graphics.Width / 1280.0f, (float)graphics.Height / 800.0f); // Set zoom according to user's resolution to ensure full visibility (initial zoom (1.2) is set for full visibility at 1280x800 resolution)

            // Set up a viewport to the Renderer subsystem so that the 3D scene can be seen
            Viewport viewport=new Viewport(Context, scene, camera, null);
            Renderer renderer = Renderer;
            renderer.SetViewport(0, viewport);

            Zone zone = renderer.DefaultZone;
            zone.FogColor = (new Color(0.1f, 0.1f, 0.1f)); // Set background color for the scene

            // Create 4x3 grid
            for (uint i = 0; i < 5; ++i)
            {
                Node edgeNode = scene.CreateChild("VerticalEdge");
                RigidBody2D edgeBody = edgeNode.CreateComponent<RigidBody2D>();
                if (dummyBody == null)
                    dummyBody = edgeBody; // Mark first edge as dummy body (used by mouse pick)
                CollisionEdge2D edgeShape = edgeNode.CreateComponent<CollisionEdge2D>();
                edgeShape.SetVertices(new Vector2(i * 2.5f - 5.0f, -3.0f), new Vector2(i * 2.5f - 5.0f, 3.0f));
                edgeShape.Friction=0.5f; // Set friction
            }

            for (uint j = 0; j < 4; ++j)
            {
                Node edgeNode = scene.CreateChild("HorizontalEdge");
                /*RigidBody2D edgeBody = */
                edgeNode.CreateComponent<RigidBody2D>();
                CollisionEdge2D edgeShape = edgeNode.CreateComponent<CollisionEdge2D>();
                edgeShape.SetVertices(new Vector2(-5.0f, j * 2.0f - 3.0f), new Vector2(5.0f, j * 2.0f - 3.0f));
                edgeShape.Friction=0.5f; // Set friction
            }

            var cache = ResourceCache;

            // Create a box (will be cloned later)
            Node box = scene.CreateChild("Box");
            box.Position = (new Vector3(0.8f, -2.0f, 0.0f));
            StaticSprite2D boxSprite = box.CreateComponent<StaticSprite2D>();
            boxSprite.Sprite=cache.GetSprite2D("Urho2D/Box.png");
            RigidBody2D boxBody = box.CreateComponent<RigidBody2D>();
            boxBody.BodyType= BodyType2D.Dynamic;
            boxBody.LinearDamping=0.0f;
            boxBody.AngularDamping=0.0f;
            CollisionBox2D shape = box.CreateComponent<CollisionBox2D>(); // Create box shape
            shape.Size=new Vector2(0.32f, 0.32f); // Set size
            shape.Density=1.0f; // Set shape density (kilograms per meter squared)
            shape.Friction=0.5f; // Set friction
            shape.Restitution=0.1f; // Set restitution (slight bounce)

            // Create a ball (will be cloned later)
            Node ball = scene.CreateChild("Ball");
            ball.Position = (new Vector3(1.8f, -2.0f, 0.0f));
            StaticSprite2D ballSprite = ball.CreateComponent<StaticSprite2D>();
            ballSprite.Sprite=cache.GetSprite2D("Urho2D/Ball.png");
            RigidBody2D ballBody = ball.CreateComponent<RigidBody2D>();
            ballBody.BodyType= BodyType2D.Dynamic;
            ballBody.LinearDamping=0.0f;
            ballBody.AngularDamping=0.0f;
            CollisionCircle2D ballShape = ball.CreateComponent<CollisionCircle2D>(); // Create circle shape
            ballShape.Radius=0.16f; // Set radius
            ballShape.Density=1.0f; // Set shape density (kilograms per meter squared)
            ballShape.Friction=0.5f; // Set friction
            ballShape.Restitution=0.6f; // Set restitution: make it bounce

            // Create a polygon
            Node polygon = scene.CreateChild("Polygon");
            polygon.Position = (new Vector3(1.6f, -2.0f, 0.0f));
            polygon.SetScale(0.7f);
            StaticSprite2D polygonSprite = polygon.CreateComponent<StaticSprite2D>();
            polygonSprite.Sprite=cache.GetSprite2D("Urho2D/Aster.png");
            RigidBody2D polygonBody = polygon.CreateComponent<RigidBody2D>();
            polygonBody.BodyType= BodyType2D.Dynamic;
            CollisionPolygon2D polygonShape = polygon.CreateComponent<CollisionPolygon2D>();
            polygonShape.VertexCount=6; // Set number of vertices (mandatory when using SetVertex())
            polygonShape.SetVertex(0, new Vector2(-0.8f, -0.3f));
            polygonShape.SetVertex(1, new Vector2(0.5f, -0.8f));
            polygonShape.SetVertex(2, new Vector2(0.8f, -0.3f));
            polygonShape.SetVertex(3, new Vector2(0.8f, 0.5f));
            polygonShape.SetVertex(4, new Vector2(0.5f, 0.9f));
            polygonShape.SetVertex(5, new Vector2(-0.5f, 0.7f));
            polygonShape.Density=1.0f; // Set shape density (kilograms per meter squared)
            polygonShape.Friction=0.3f; // Set friction
            polygonShape.Restitution=0.0f; // Set restitution (no bounce)

            // Create a ConstraintDistance2D
            CreateFlag("ConstraintDistance2D", -4.97f, 3.0f); // Display Text3D flag
            Node boxDistanceNode = box.Clone(CreateMode.Replicated);
            Node ballDistanceNode = ball.Clone(CreateMode.Replicated);
            RigidBody2D ballDistanceBody = ballDistanceNode.GetComponent<RigidBody2D>();
            boxDistanceNode.Position = (new Vector3(-4.5f, 2.0f, 0.0f));
            ballDistanceNode.Position = (new Vector3(-3.0f, 2.0f, 0.0f));

            ConstraintDistance2D constraintDistance = boxDistanceNode.CreateComponent<ConstraintDistance2D>(); // Apply ConstraintDistance2D to box
            constraintDistance.OtherBody=ballDistanceBody; // Constrain ball to box
            constraintDistance.OwnerBodyAnchor=boxDistanceNode.Position2D;
            constraintDistance.OtherBodyAnchor=ballDistanceNode.Position2D;
            // Make the constraint soft (comment to make it rigid, which is its basic behavior)
            constraintDistance.FrequencyHz=4.0f;
            constraintDistance.DampingRatio=0.5f;

            // Create a ConstraintFriction2D ********** Not functional. From Box2d samples it seems that 2 anchors are required, Urho2D only provides 1, needs investigation ***********
            CreateFlag("ConstraintFriction2D", 0.03f, 1.0f); // Display Text3D flag
            Node boxFrictionNode = box.Clone(CreateMode.Replicated);
            Node ballFrictionNode = ball.Clone(CreateMode.Replicated);
            boxFrictionNode.Position = (new Vector3(0.5f, 0.0f, 0.0f));
            ballFrictionNode.Position = (new Vector3(1.5f, 0.0f, 0.0f));

            ConstraintFriction2D constraintFriction = boxFrictionNode.CreateComponent<ConstraintFriction2D>(); // Apply ConstraintDistance2D to box
            constraintFriction.OtherBody=ballFrictionNode.GetComponent<RigidBody2D>(); // Constraint ball to box

            // Create a ConstraintGear2D
            CreateFlag("ConstraintGear2D", -4.97f, -1.0f); // Display Text3D flag
            Node baseNode = box.Clone(CreateMode.Replicated);
            RigidBody2D tempBody = baseNode.GetComponent<RigidBody2D>(); // Get body to make it static
            tempBody.BodyType= BodyType2D.Static;
            baseNode.Position = (new Vector3(-3.7f, -2.5f, 0.0f));
            Node ball1Node = ball.Clone(CreateMode.Replicated);
            ball1Node.Position = (new Vector3(-4.5f, -2.0f, 0.0f));
            RigidBody2D ball1Body = ball1Node.GetComponent<RigidBody2D>();
            Node ball2Node = ball.Clone(CreateMode.Replicated);
            ball2Node.Position = (new Vector3(-3.0f, -2.0f, 0.0f));
            RigidBody2D ball2Body = ball2Node.GetComponent<RigidBody2D>();

            ConstraintRevolute2D gear1 = baseNode.CreateComponent<ConstraintRevolute2D>(); // Apply constraint to baseBox
            gear1.OtherBody=ball1Body; // Constrain ball1 to baseBox
            gear1.Anchor=ball1Node.Position2D;
            ConstraintRevolute2D gear2 = baseNode.CreateComponent<ConstraintRevolute2D>(); // Apply constraint to baseBox
            gear2.OtherBody=ball2Body; // Constrain ball2 to baseBox
            gear2.Anchor=ball2Node.Position2D;

            ConstraintGear2D constraintGear = ball1Node.CreateComponent<ConstraintGear2D>(); // Apply constraint to ball1
            constraintGear.OtherBody=ball2Body; // Constrain ball2 to ball1
            constraintGear.OwnerConstraint=gear1;
            constraintGear.OtherConstraint=gear2;
            constraintGear.Ratio=1.0f;

            ball1Body.ApplyAngularImpulse(0.015f, true); // Animate

            // Create a vehicle from a compound of 2 ConstraintWheel2Ds
            CreateFlag("ConstraintWheel2Ds compound", -2.45f, -1.0f); // Display Text3D flag
            Node car = box.Clone(CreateMode.Replicated);
            car.Scale=new Vector3(4.0f, 1.0f, 0.0f);
            car.Position = (new Vector3(-1.2f, -2.3f, 0.0f));
            StaticSprite2D tempSprite = car.GetComponent<StaticSprite2D>(); // Get car Sprite in order to draw it on top
            tempSprite.OrderInLayer=0; // Draw car on top of the wheels (set to -1 to draw below)
            Node ball1WheelNode = ball.Clone(CreateMode.Replicated);
            ball1WheelNode.Position = (new Vector3(-1.6f, -2.5f, 0.0f));
            Node ball2WheelNode = ball.Clone(CreateMode.Replicated);
            ball2WheelNode.Position = (new Vector3(-0.8f, -2.5f, 0.0f));

            ConstraintWheel2D wheel1 = car.CreateComponent<ConstraintWheel2D>();
            wheel1.OtherBody=ball1WheelNode.GetComponent<RigidBody2D>();
            wheel1.Anchor=ball1WheelNode.Position2D;
            wheel1.Axis=new Vector2(0.0f, 1.0f);
            wheel1.MaxMotorTorque=20.0f;
            wheel1.FrequencyHz=4.0f;
            wheel1.DampingRatio=0.4f;

            ConstraintWheel2D wheel2 = car.CreateComponent<ConstraintWheel2D>();
            wheel2.OtherBody=ball2WheelNode.GetComponent<RigidBody2D>();
            wheel2.Anchor=ball2WheelNode.Position2D;
            wheel2.Axis=new Vector2(0.0f, 1.0f);
            wheel2.MaxMotorTorque=10.0f;
            wheel2.FrequencyHz=4.0f;
            wheel2.DampingRatio=0.4f;

            // ConstraintMotor2D
            CreateFlag("ConstraintMotor2D", 2.53f, -1.0f); // Display Text3D flag
            Node boxMotorNode = box.Clone(CreateMode.Replicated);
            tempBody = boxMotorNode.GetComponent<RigidBody2D>(); // Get body to make it static
            tempBody.BodyType = BodyType2D.Static;
            Node ballMotorNode = ball.Clone(CreateMode.Replicated);
            boxMotorNode.Position = (new Vector3(3.8f, -2.1f, 0.0f));
            ballMotorNode.Position = (new Vector3(3.8f, -1.5f, 0.0f));

            ConstraintMotor2D constraintMotor = boxMotorNode.CreateComponent<ConstraintMotor2D>();
            constraintMotor.OtherBody=ballMotorNode.GetComponent<RigidBody2D>(); // Constrain ball to box
            constraintMotor.LinearOffset=new Vector2(0.0f, 0.8f); // Set ballNode position relative to boxNode position = (0,0)
            constraintMotor.AngularOffset=0.1f;
            constraintMotor.MaxForce=5.0f;
            constraintMotor.MaxTorque=10.0f;
            constraintMotor.CorrectionFactor=1.0f;
            constraintMotor.CollideConnected=true; // doesn't work

            // ConstraintMouse2D is demonstrated in HandleMouseButtonDown() function. It is used to "grasp" the sprites with the mouse.
            CreateFlag("ConstraintMouse2D", 0.03f, -1.0f); // Display Text3D flag

            // Create a ConstraintPrismatic2D
            CreateFlag("ConstraintPrismatic2D", 2.53f, 3.0f); // Display Text3D flag
            Node boxPrismaticNode = box.Clone(CreateMode.Replicated);
            tempBody = boxPrismaticNode.GetComponent<RigidBody2D>(); // Get body to make it static
            tempBody.BodyType = BodyType2D.Static;
            Node ballPrismaticNode = ball.Clone(CreateMode.Replicated);
            boxPrismaticNode.Position = new Vector3(3.3f, 2.5f, 0.0f);
            ballPrismaticNode.Position = new Vector3(4.3f, 2.0f, 0.0f);

            ConstraintPrismatic2D constraintPrismatic = boxPrismaticNode.CreateComponent<ConstraintPrismatic2D>();
            constraintPrismatic.OtherBody=ballPrismaticNode.GetComponent<RigidBody2D>(); // Constrain ball to box
            constraintPrismatic.Axis=new Vector2(1.0f, 1.0f); // Slide from [0,0] to [1,1]
            constraintPrismatic.Anchor=new Vector2(4.0f, 2.0f);
            constraintPrismatic.LowerTranslation=-1.0f;
            constraintPrismatic.UpperTranslation=0.5f;
            constraintPrismatic.EnableLimit=true;
            constraintPrismatic.MaxMotorForce=1.0f;
            constraintPrismatic.MotorSpeed=0.0f;

            // ConstraintPulley2D
            CreateFlag("ConstraintPulley2D", 0.03f, 3.0f); // Display Text3D flag
            Node boxPulleyNode = box.Clone(CreateMode.Replicated);
            Node ballPulleyNode = ball.Clone(CreateMode.Replicated);
            boxPulleyNode.Position = (new Vector3(0.5f, 2.0f, 0.0f));
            ballPulleyNode.Position = (new Vector3(2.0f, 2.0f, 0.0f));

            ConstraintPulley2D constraintPulley = boxPulleyNode.CreateComponent<ConstraintPulley2D>(); // Apply constraint to box
            constraintPulley.OtherBody=ballPulleyNode.GetComponent<RigidBody2D>(); // Constrain ball to box
            constraintPulley.OwnerBodyAnchor=boxPulleyNode.Position2D;
            constraintPulley.OtherBodyAnchor=ballPulleyNode.Position2D;
            constraintPulley.OwnerBodyGroundAnchor=boxPulleyNode.Position2D + new Vector2(0.0f, 1.0f);
            constraintPulley.OtherBodyGroundAnchor=ballPulleyNode.Position2D + new Vector2(0.0f, 1.0f);
            constraintPulley.Ratio=1.0f; // Weight ratio between ownerBody and otherBody

            // Create a ConstraintRevolute2D
            CreateFlag("ConstraintRevolute2D", -2.45f, 3.0f); // Display Text3D flag
            Node boxRevoluteNode = box.Clone(CreateMode.Replicated);
            tempBody = boxRevoluteNode.GetComponent<RigidBody2D>(); // Get body to make it static
            tempBody.BodyType = BodyType2D.Static;
            Node ballRevoluteNode = ball.Clone(CreateMode.Replicated);
            boxRevoluteNode.Position = (new Vector3(-2.0f, 1.5f, 0.0f));
            ballRevoluteNode.Position = (new Vector3(-1.0f, 2.0f, 0.0f));

            ConstraintRevolute2D constraintRevolute = boxRevoluteNode.CreateComponent<ConstraintRevolute2D>(); // Apply constraint to box
            constraintRevolute.OtherBody=ballRevoluteNode.GetComponent<RigidBody2D>(); // Constrain ball to box
            constraintRevolute.Anchor=new Vector2(-1.0f, 1.5f);
            constraintRevolute.LowerAngle=-1.0f; // In radians
            constraintRevolute.UpperAngle=0.5f; // In radians
            constraintRevolute.EnableLimit=true;
            constraintRevolute.MaxMotorTorque=10.0f;
            constraintRevolute.MotorSpeed=0.0f;
            constraintRevolute.EnableMotor=true;

            // Create a ConstraintRope2D
            CreateFlag("ConstraintRope2D", -4.97f, 1.0f); // Display Text3D flag
            Node boxRopeNode = box.Clone(CreateMode.Replicated);
            tempBody = boxRopeNode.GetComponent<RigidBody2D>();
            tempBody.BodyType = BodyType2D.Static;
            Node ballRopeNode = ball.Clone(CreateMode.Replicated);
            boxRopeNode.Position = (new Vector3(-3.7f, 0.7f, 0.0f));
            ballRopeNode.Position = (new Vector3(-4.5f, 0.0f, 0.0f));

            ConstraintRope2D constraintRope = boxRopeNode.CreateComponent<ConstraintRope2D>();
            constraintRope.OtherBody=ballRopeNode.GetComponent<RigidBody2D>(); // Constrain ball to box
            constraintRope.OwnerBodyAnchor=new Vector2(0.0f, -0.5f); // Offset from box (OwnerBody) : the rope is rigid from OwnerBody center to this ownerBodyAnchor
            constraintRope.MaxLength=0.9f; // Rope length
            constraintRope.CollideConnected=true;

            // Create a ConstraintWeld2D
            CreateFlag("ConstraintWeld2D", -2.45f, 1.0f); // Display Text3D flag
            Node boxWeldNode = box.Clone(CreateMode.Replicated);
            Node ballWeldNode = ball.Clone(CreateMode.Replicated);
            boxWeldNode.Position = (new Vector3(-0.5f, 0.0f, 0.0f));
            ballWeldNode.Position = (new Vector3(-2.0f, 0.0f, 0.0f));

            ConstraintWeld2D constraintWeld = boxWeldNode.CreateComponent<ConstraintWeld2D>();
            constraintWeld.OtherBody=ballWeldNode.GetComponent<RigidBody2D>(); // Constrain ball to box
            constraintWeld.Anchor=boxWeldNode.Position2D;
            constraintWeld.FrequencyHz=4.0f;
            constraintWeld.DampingRatio=0.5f;

            // Create a ConstraintWheel2D
            CreateFlag("ConstraintWheel2D", 2.53f, 1.0f); // Display Text3D flag
            Node boxWheelNode = box.Clone(CreateMode.Replicated);
            Node ballWheelNode = ball.Clone(CreateMode.Replicated);
            boxWheelNode.Position = (new Vector3(3.8f, 0.0f, 0.0f));
            ballWheelNode.Position = (new Vector3(3.8f, 0.9f, 0.0f));

            ConstraintWheel2D constraintWheel = boxWheelNode.CreateComponent<ConstraintWheel2D>();
            constraintWheel.OtherBody=ballWheelNode.GetComponent<RigidBody2D>(); // Constrain ball to box
            constraintWheel.Anchor=ballWheelNode.Position2D;
            constraintWheel.Axis=new Vector2(0.0f, 1.0f);
            constraintWheel.EnableMotor=true;
            constraintWheel.MaxMotorTorque=1.0f;
            constraintWheel.MotorSpeed=0.0f;
            constraintWheel.FrequencyHz=4.0f;
            constraintWheel.DampingRatio=0.5f;
            constraintWheel.CollideConnected=true; // doesn't work
        }
Exemple #34
0
 void Awake()
 {
     rb = GetComponent <RigidBody2D>();
 }
 public PaddleBehavior()
     : base("PaddleBehavior")
 {
     body = null;
     transf = null;
 }
Exemple #36
0
 private void Jump()
 {
     RigidBody2D.AddForce(Vector2.up * 400);
 }