예제 #1
0
        public PhysicalJeep(StarlingGameSpriteWithJeepTextures textures, StarlingGameSpriteWithPhysics Context)
        {
            this.CurrentInput = new KeySample();
            this.CameraRotation = Math.PI / 2;

            this.textures = textures;
            this.driverseat = new DriverSeat();
            this.Context = Context;

            visual0 = new VisualJeep(textures, Context);

            for (int i = 0; i < 7; i++)
            {
                this.KarmaInput0.Enqueue(
                    new KeySample()
                );
            }

            Func<double, double, double[]> ff = (a, b) => { return new double[] { a, b }; };


            {
                xwheels = new[] { 
                        //top left
                        new Wheel { b2world = Context.groundkarma_b2world, x = -1.1, y = -1.2, width = 0.4, length = 0.8, revolving = true, powered = true },

                        //top right
                        new Wheel{b2world= Context.groundkarma_b2world, x =1.1,  y =-1.2,  width =0.4,  length =0.8,  revolving =true,  powered =true},


                        //back left
                        new Wheel{b2world= Context.groundkarma_b2world, x =-1.1,  y =1.2,  width =0.4,  length =0.8,  revolving =false,  powered =false},

                        //back right
                        new Wheel{b2world= Context.groundkarma_b2world, x =1.1,  y =1.2,  width =0.4,  length =0.8,  revolving =false,  powered =false},
                    };



                karmaunit4_physics = new Car(
                   b2world: Context.groundkarma_b2world,
                   width: 2,
                   length: 4,
                   position: ff(0, 0),
                   angle: 180,

                   // how fast can the jeep go?
                   power: 120,
                   max_speed: 120,

                   max_steer_angle: 33,
                    //max_steer_angle: 40,

                   wheels: xwheels
               );

            }

            {


                xwheels = new[] { 
                        //top left
                        new Wheel { b2world = Context.ground_b2world, x = -1.1, y = -1.2, width = 0.4, length = 0.8, revolving = true, powered = true },

                        //top right
                        new Wheel{b2world= Context.ground_b2world, x =1.1,  y =-1.2,  width =0.4,  length =0.8,  revolving =true,  powered =true},


                        //back left
                        new Wheel{b2world= Context.ground_b2world, x =-1.1,  y =1.2,  width =0.4,  length =0.8,  revolving =false,  powered =false},

                        //back right
                        new Wheel{b2world= Context.ground_b2world, x =1.1,  y =1.2,  width =0.4,  length =0.8,  revolving =false,  powered =false},
                    };



                unit4_physics = new Car(
                    b2world: Context.ground_b2world,
                    width: 2,
                    length: 4,
                    position: ff(0, 0),
                    angle: 180,

                    // how fast can the jeep go?
                    power: 120,
                    max_speed: 120,

                    max_steer_angle: 33,
                    //max_steer_angle: 40,

                    wheels: xwheels
                );

                var fix = unit4_physics.fix;
                var fix_data = new Action<double>(
                    jeep_forceA =>
                    {
                        if (jeep_forceA < 1)
                            return;

                        if (oncollision != null)
                            oncollision(this, jeep_forceA);
                    }
                );
                fix.SetUserData(fix_data);


                xwheels[0].setAngle += a =>
                {
                    var cm = new Matrix();
                    cm.translate(-2, -2);
                    cm.scale(2, 4);
                    cm.rotate(a.DegreesToRadians());

                    cm.translate(-18, -20);

                    visual0.tire0.transformationMatrix = cm;
                };

                xwheels[1].setAngle += a =>
                {

                    var cm = new Matrix();
                    cm.translate(-2, -2);
                    cm.scale(2, 4);
                    cm.rotate(a.DegreesToRadians());

                    cm.translate(18, -20);

                    visual0.tire1.transformationMatrix = cm;
                };
            }

            {
                //initialize body
                var def = new b2BodyDef();
                def.type = b2Body.b2_dynamicBody;
                def.linearDamping = 0.55;  //gradually reduces velocity, makes the car reduce speed slowly if neither accelerator nor brake is pressed
                def.bullet = true; //dedicates more time to collision detection - car travelling at high speeds at low framerates otherwise might teleport through obstacles.
                def.angularDamping = 0.3;

                this.damagebody = Context.damage_b2world.CreateBody(def);

                //initialize shape
                var fixdef = new b2FixtureDef();
                fixdef.density = 1.0;
                fixdef.friction = 0.3; //friction when rubbing agaisnt other shapes
                fixdef.restitution = 0.4;  //amount of force feedback when hitting something. >0 makes the car bounce off, it's fun!

                var fixdef_shape = new b2PolygonShape();

                fixdef.shape = fixdef_shape;
                fixdef_shape.SetAsBox(2 / 2, 4 / 2);
                var fix = damagebody.CreateFixture(fixdef);

            }

            Context.internalunits.Add(this);

        }
예제 #2
0
        public void ExtractVelocityFromInput(KeySample __keyDown, Car unit4_physics)
        {
            //var rot = 0;
            //var dx = 0.0;
            //var dy = 0.0;

            unit4_physics.accelerate = Car.ACC_NONE;
            unit4_physics.steer_left = Car.STEER_NONE;
            unit4_physics.steer_right = Car.STEER_NONE;

            if (__keyDown == null)
                return;

            if (__keyDown[Keys.Up])
            {
                // we have reasone to keep walking

                unit4_physics.accelerate = Car.ACC_ACCELERATE;
                //dy = 1;
            }

            if (__keyDown[Keys.Down])
            {
                // we have reasone to keep walking
                // go slow backwards
                //dy = -0.5;
                unit4_physics.accelerate = Car.ACC_BRAKE;

            }


            if (__keyDown[Keys.Left])
            {
                // we have reasone to keep walking

                unit4_physics.steer_left = Car.STEER_LEFT;

            }

            if (__keyDown[Keys.Right])
            {
                // we have reasone to keep walking

                unit4_physics.steer_right = Car.STEER_RIGHT;

            }
        }