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
        private void SpawnDustParticle(Vector3 origin, Registry registry)
        {
            var particleEmitter = new ParticleEmitter2D(
                origin.Xy,                      // Origin Position
                .4f,                            // Min Lifetime
                1.5f,                           // Max Lifetime
                100,                            // Min Velocity
                200,                            // Max Velocity
                0.1f,                           // Gravity Modifier
                5,                              // Count
                Color4.DarkGray,                // Begin Color
                Color4.LightGray,               // End Color
                new Vector2(3, 3),              // Begin Size
                new Vector2(0, 0),              // End Size
                2                               // Size Deviation
                );

            particleEmitter.IsActive = true;

            registry.SpawnEntity(
                //new BoxCollider(20,20),
                //new DebugRenderer(new Vector4(1,1,0,1)),
                new TransformComponent(Matrix4.Identity),
                particleEmitter,
                new LifeTimeComponent(2)
                );
        }
Exemplo n.º 3
0
        private void SpawnParticle(bool blood, Vector2 origin, Registry registry)
        {
            var transformComponent = new TransformComponent(
                new Vector3(origin.X, origin.Y, 5),
                new Quaternion(0, 0, 0),
                new Vector3(1)
                );

            var particleEmitter = new ParticleEmitter2D(
                origin,                                         // Origin Position
                blood ? 1f : 0.2f,                              // Min Lifetime
                blood ? .8f : 2f,                               // Max Lifetime
                100,                                            // Min Velocity
                400,                                            // Max Velocity
                1f,                                             // Gravity Modifier
                blood ? 20 : 5,                                 // Count
                blood ? Color4.Red : Color4.Yellow,             // Begin Color
                blood ? Color4.Red : Color4.Red,                // End Color
                blood ? new Vector2(5, 5) : new Vector2(3, 3),  // Begin Size
                new Vector2(0, 0),                              // End Size
                blood ? 2 : 1                                   // Size Deviation
                );

            particleEmitter.IsActive = true;

            registry.SpawnEntity(
                //new BoxCollider(20,20),
                //new DebugRenderer(new Vector4(1,1,0,1)),
                transformComponent,
                particleEmitter,
                new LifeTimeComponent(2)
                );
        }
		protected override void OnShow(Node node, Asset asset)
		{
			node.SetScale(1f);
			ParticleEffect2D particleEffect = ResourceCache.GetParticleEffect2D(asset.RelativePathToAsset);
			if (particleEffect == null)
				return;
			
			ParticleEmitter2D particleEmitter = node.CreateComponent<ParticleEmitter2D>();
			particleEmitter.Effect = particleEffect;
			App.Viewport.SetClearColor(Color.Black);

			//TODO: remove it and let user to control position by mouse
			node.RunActions(new RepeatForever(new RotateAroundBy(0.5f, new Vector3(1, 0, 0), 0, 0, 90, TransformSpace.Parent)));
		}
Exemplo n.º 5
0
        void CreateScene()
        {
            scene = new Scene();
            scene.CreateComponent <Octree>();

            // 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)

            var cache = ResourceCache;
            ParticleEffect2D particleEffect = cache.GetParticleEffect2D("Urho2D/sun.pex");

            if (particleEffect == null)
            {
                return;
            }

            particleNode = scene.CreateChild("ParticleEmitter2D");
            ParticleEmitter2D particleEmitter = particleNode.CreateComponent <ParticleEmitter2D>();

            particleEmitter.Effect = particleEffect;

            ParticleEffect2D greenSpiralEffect = cache.GetParticleEffect2D("Urho2D/greenspiral.pex");

            if (greenSpiralEffect == null)
            {
                return;
            }

            Node greenSpiralNode = scene.CreateChild("GreenSpiral");
            ParticleEmitter2D greenSpiralEmitter = greenSpiralNode.CreateComponent <ParticleEmitter2D>();

            greenSpiralEmitter.Effect = greenSpiralEffect;
        }
Exemplo n.º 6
0
        private void Instatiate(ParticleEmitter2D emitter)
        {
            var seed = DateTime.Now.Second;

            emitter.PositionsAndVelocitys = new Vector4[emitter.Length];
            emitter.AnglesAndTorques      = new Vector2[emitter.Length];
            emitter.Sizes     = new Vector2[emitter.Length];
            emitter.LifeTimes = new float[emitter.Length];
            emitter.Colors    = new Color4[emitter.Length];

            for (int i = 0; i < emitter.Length; i++)
            {
                emitter.PositionsAndVelocitys[i].X = emitter.Origin.X;
                emitter.PositionsAndVelocitys[i].Y = emitter.Origin.Y;
                emitter.PositionsAndVelocitys[i].Z = (float)Math.Cos(360d * new Random(seed++).NextDouble()) * MathHelper.Lerp(emitter.MinVelocity, emitter.MaxVelocity, (float)new Random(seed++).NextDouble());
                emitter.PositionsAndVelocitys[i].W = (float)Math.Sin(360d * new Random(seed++).NextDouble()) * MathHelper.Lerp(emitter.MinVelocity, emitter.MaxVelocity, (float)new Random(seed++).NextDouble());
            }

            for (int i = 0; i < emitter.Length; i++)
            {
                emitter.Sizes[i].X = MathHelper.Lerp(emitter.BeginSize.X - emitter.DeviationSize, emitter.BeginSize.X + emitter.DeviationSize, (float)new Random(seed++).NextDouble());
                emitter.Sizes[i].Y = MathHelper.Lerp(emitter.BeginSize.Y - emitter.DeviationSize, emitter.BeginSize.Y + emitter.DeviationSize, (float)new Random(seed++).NextDouble());
            }

            for (int i = 0; i < emitter.Length; i++)
            {
                emitter.AnglesAndTorques[i].X = 360f * (float)new Random(seed++).NextDouble();
                emitter.AnglesAndTorques[i].Y = 10f * (float)new Random(seed++).NextDouble();
            }

            for (int i = 0; i < emitter.Length; i++)
            {
                emitter.Colors[i] = emitter.Begincolor;
            }

            for (int i = 0; i < emitter.Length; i++)
            {
                emitter.LifeTimes[i] = emitter.MinLifeTime + (float)new Random(seed++).NextDouble() * (emitter.MaxLifeTime - emitter.MinLifeTime);
            }
        }
Exemplo n.º 7
0
    public Vehicle CreateChassis(Vector2 colliderCenter, float colliderRadius, int massDensity, Vector3 exhaustPosition,
                                 ParticleEffect2D exhaustParticles, Sound engineSound, Sound tireSound, Sound[] suspensionSounds,
                                 int horsePower, int maxSpeedFwd, int maxSpeedBwd, int rollForce)
    {
        // We set out private fields
        _horsePower = horsePower;
        _maxSpdFwd  = maxSpeedFwd;
        _maxSpdBwd  = maxSpeedBwd;
        _rollForce  = rollForce;
        _rigidBody  = GetComponent <RigidBody2D>();

        // We add the collider (circle collider at the moment)
        var col = Racer2D.AddCollider <CollisionCircle2D>(Node, dens: massDensity, fric: 0);

        col.SetRadius(colliderRadius);
        col.SetCenter(colliderCenter);

        // We create the exhaust particle system
        var exhaustParticlesNode = Node.CreateChild();

        exhaustParticlesNode.SetPosition(exhaustPosition);
        _exhaustParticles = exhaustParticlesNode.CreateComponent <ParticleEmitter2D>();
        _exhaustParticles.SetEffect(exhaustParticles);

        // We setup the engine sound and other sound effect
        engineSound.SetLooped(true);
        _soundSource = Node.CreateComponent <SoundSource3D>();
        _soundSource.SetNearDistance(10);
        _soundSource.SetFarDistance(50);
        _accelSound            = engineSound;
        _brakeSound            = tireSound;
        _suspensionSoundSource = Node.CreateComponent <SoundSource3D>();
        _suspensionSounds      = suspensionSounds;

        // We return the Vehicle for convenience, since this function is intended to be the vehicle's init function
        return(this);
    }
Exemplo n.º 8
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);
    }
Exemplo n.º 9
0
        private void Advance(ParticleEmitter2D emitter, TimeStep ts)
        {
            emitter.EmitterLifetime += ts;
            if (emitter.EmitterLifetime >= emitter.MaxLifeTime)
            {
                emitter.IsActive = false;
                return;
            }

            for (int i = 0; i < emitter.Length; i++)
            {
                emitter.PositionsAndVelocitys[i].W += PhysicsGlobals.Gravity * emitter.GravityModfier * PhysicsWorld2D.PixelToMeter * ts;
                emitter.PositionsAndVelocitys[i].X += emitter.PositionsAndVelocitys[i].Z * ts;
                emitter.PositionsAndVelocitys[i].Y += emitter.PositionsAndVelocitys[i].W * ts;
            }

            for (int i = 0; i < emitter.Length; i++)
            {
                emitter.AnglesAndTorques[i].X += emitter.AnglesAndTorques[i].Y * ts;
            }

            for (int i = 0; i < emitter.Length; i++)
            {
                float r = MathHelper.Lerp(emitter.Begincolor.R, emitter.Endcolor.R, emitter.EmitterLifetime / emitter.LifeTimes[i]);
                float g = MathHelper.Lerp(emitter.Begincolor.G, emitter.Endcolor.G, emitter.EmitterLifetime / emitter.LifeTimes[i]);
                float b = MathHelper.Lerp(emitter.Begincolor.B, emitter.Endcolor.B, emitter.EmitterLifetime / emitter.LifeTimes[i]);
                float a = MathHelper.Lerp(emitter.Begincolor.A, emitter.Endcolor.A, emitter.EmitterLifetime / emitter.LifeTimes[i]);
                emitter.Colors[i] = new Color4(r, g, b, a);
            }

            for (int i = 0; i < emitter.Length; i++)
            {
                emitter.Sizes[i].X = MathHelper.Lerp(emitter.BeginSize.X, emitter.EndSize.X, emitter.EmitterLifetime / emitter.LifeTimes[i]);
                emitter.Sizes[i].Y = MathHelper.Lerp(emitter.BeginSize.Y, emitter.EndSize.Y, emitter.EmitterLifetime / emitter.LifeTimes[i]);
            }
        }
Exemplo n.º 10
0
 public Wheel(RigidBody2D rigidBody, ConstraintWheel2D constraint, ParticleEmitter2D particleEmitter, float particlesDistance)
 {
     _rigidBody = rigidBody; _constraint = constraint; _particleEmitter = particleEmitter; _particlesDistance = particlesDistance;
 }