Exemplo n.º 1
0
    private Node CreateWheel(Sprite2D wspr, Node basenode, Vector2 relPos)
    {
        var w1 = CreateSpriteNode(scene, wspr);

        w1.GetComponent <RigidBody2D>().SetBullet(true);
        var c = AddCollider <CollisionCircle2D>(w1);

        c.SetRadius(1.25f);

        //ConstraintRevolute2D wj1 = basenode.CreateComponent<ConstraintRevolute2D>();
        //wj1.SetOtherBody(w1.GetComponent<RigidBody2D>());
        //w1.SetPosition2D(relPos);
        //wj1.SetAnchor(relPos);

        ConstraintWheel2D wj1 = basenode.CreateComponent <ConstraintWheel2D>();

        wj1.SetOtherBody(w1.GetComponent <RigidBody2D>());
        w1.SetPosition2D(relPos);
        wj1.SetAnchor(relPos);
        wj1.SetAxis(Vector2.UnitY);
        wj1.SetFrequencyHz(4);
        wj1.SetDampingRatio(0.4f);

        var ep = scene.CreateChild();

        ep.SetPosition(new Vector3(-2f, -1, 14));
        pe = ep.CreateComponent <ParticleEmitter2D>();
        pe.SetEffect(cache.GetResource <ParticleEffect2D>("particles/dust.pex"));

        activeWheels.Add(new WheelData {
            rb = w1.GetComponent <RigidBody2D>(), pe = pe
        });

        return(w1);
    }
Exemplo n.º 2
0
    public Node CreateWheel(Sprite2D sprite, Vector2 relativePosition, float radius, int suspensionFrequency, float suspensionDamping,
                            ParticleEffect2D particles, float distanceToEmitParticles)
    {
        Node wheelNode = Racer2D.CreateSpriteNode(sprite);

        wheelNode.SetPosition2D(relativePosition);

        // CreateSpriteNode adds a RigidBody for us, so we get it here
        RigidBody2D wheelRigidBody = wheelNode.GetComponent <RigidBody2D>();

        // We activate CCD
        wheelRigidBody.SetBullet(true);

        Racer2D.AddCollider <CollisionCircle2D>(wheelNode).SetRadius(radius);

        // The Box2D wheel joint provides spring for simulating suspension
        ConstraintWheel2D wheelJoint = Node.CreateComponent <ConstraintWheel2D>();

        wheelJoint.SetOtherBody(wheelRigidBody);
        wheelJoint.SetAnchor(relativePosition);
        wheelJoint.SetAxis(Vector2.UnitY);
        wheelJoint.SetFrequencyHz(suspensionFrequency);
        wheelJoint.SetDampingRatio(suspensionDamping);

        // Each wheel has a particle emitter to emit particles when it's in contact with the surface
        Node particlesNode = Node.Scene.CreateChild();

        particlesNode.SetPosition(new Vector3(relativePosition.X, relativePosition.Y, 14));
        ParticleEmitter2D particleEmitter = particlesNode.CreateComponent <ParticleEmitter2D>();

        particleEmitter.SetEffect(particles);

        // We create a new Wheel struct and add to the _wheels list
        _wheels.Add(new Wheel(wheelRigidBody, wheelJoint, particleEmitter, distanceToEmitParticles));

        return(wheelNode);
    }