Пример #1
0
        public void Step(float elapsed, Simulation sim)
        {
            Vector2 thisPos;
            Vector2 thisDelta;
            Vector2 thisTarget;
            LineSegment<Vector2> thisSegment;
            float thisDistance;

            var target = new Vector2();
            var distance = 0f;
            var hasTarget = false;

            for (int i = 0; i < sim.Balls.Length; i++) {
                if (sim.Balls[i].Carrier != null) {
                    thisPos = sim.Balls[i].Carrier.Fixture.Body.Position;
                    SegmentFor(thisPos, out thisSegment, out thisDelta);
                    thisTarget = Trig.ClosestPointOnSegment(thisSegment, thisPos);
                    thisTarget += thisDelta;
                }
                else {
                    thisPos = sim.Balls[i].Fixture.Body.Position;
                    SegmentFor(thisPos, out thisSegment, out thisDelta);
                    var ballStop = sim.Balls[i].BallStop;
                    LineSegment<Vector2> ballVector;
                    ballVector.A = thisPos;
                    ballVector.B = ballStop;
                    if (!Trig.SegmentSegmentIntersection(ballVector, thisSegment, out thisTarget)) {
                        thisTarget = Trig.ClosestPointOnSegment(thisSegment, thisPos);
                    }
                    thisTarget += thisDelta;
                }

                thisDistance = Trig.DistanceSquared(Fixture.Body.Position, thisPos);
                if (!hasTarget || thisDistance < distance) {
                    target = thisTarget;
                    distance = thisDistance;
                    hasTarget = true;
                }
            }

            if (!hasTarget) return;

            var delta = target - Fixture.Body.Position;
            if (delta.X == 0 && delta.Y == 0) {
                Fixture.Body.LinearVelocity = new Vector2(0f, 0f);
                return;
            }

            var direction = delta;
            direction.Normalize();

            var moveDistance = Trig.DistanceSquared(Fixture.Body.Position, target);
            if (moveDistance <= speed * elapsed * speed * elapsed) {
                Fixture.Body.LinearVelocity = delta/elapsed;
                return;
            }

            Fixture.Body.LinearVelocity = direction * speed;
        }
Пример #2
0
 public Ball(Simulation sim, BallData spawn)
 {
     simulation = sim;
     var body = new Body(sim.World);
     var shape = new CircleShape(radius, 1f);
     body.BodyType = BodyType.Dynamic;
     body.FixedRotation = true;
     body.LinearDamping = drag;
     body.IsBullet = true;
     Fixture = body.CreateFixture(shape);
     Fixture.Restitution = restitution;
     Fixture.Friction = 0f;
     Carrier = null;
     SpawnPos = spawn.Position;
     Spawn();
 }
Пример #3
0
        void HandleLoad(object sender, EventArgs e)
        {
            Programs.Load();
            Fps = new Fps();
            Starfield = new Starfield();
            MapMesh = new MapMesh(map);
            GoalMesh = new SensorMesh<GoalCompiled>(map.Goals, new Texture("Images/goal.png"));
            SpeedupMesh = new SensorMesh<SpeedupCompiled>(map.Speedups, new Texture("Images/speedup.png"));
            ShipMesh = new ObjMesh("Models/ship.obj");
            BallMesh = new ObjMesh("Models/ball.obj");
            GoalieMesh = new ObjMesh("Models/goalie.obj");
            Simulation = new Simulation(map);
            Ship = Simulation.Ships[0];
            Nebula = new Image("Images/nebula.png", GameWindow.Width, GameWindow.Height);
            BallSpawn = new Image("Images/ballspawn.png", -1f, 1f, -1f, 1f);

            GL.ClearColor(Color.Black);
            GL.ClearDepth(1.0f);

            GL.Enable(EnableCap.DepthTest);
            GL.DepthFunc(DepthFunction.Lequal);

            GL.Enable(EnableCap.CullFace);
            GL.CullFace(CullFaceMode.Back);

            GL.Enable(EnableCap.Blend);
            GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha);

            GL.Hint(HintTarget.PerspectiveCorrectionHint, HintMode.Nicest);
            GL.Hint(HintTarget.GenerateMipmapHint, HintMode.Nicest);
            GL.Hint(HintTarget.LineSmoothHint, HintMode.Nicest);
            GL.Hint(HintTarget.PointSmoothHint, HintMode.Nicest);
            GL.Hint(HintTarget.PolygonSmoothHint, HintMode.Nicest);

            Programs.Material.Use();
            Programs.Material.LightDiffuse = light_diffuse;
            Programs.Material.LightAmbient = light_ambient;
            Programs.Material.LightSpecular = light_specular;
            Programs.Material.LightPosition = light_position;
        }