source: 2014/24/EemeliK/Zombieland/Jypeli/Game/TopDownPhysicsGame.cs @ 5974

Revision 5974, 5.2 KB checked in by empaheik, 5 years ago (diff)
Line 
1#region MIT License
2/*
3 * Copyright (c) 2009 University of Jyväskylä, Department of Mathematical
4 * Information Technology.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24#endregion
25
26/*
27 * Authors: Tero Jäntti, Tomi Karppinen, Janne Nikkanen.
28 */
29
30using System;
31using Physics2DDotNet;
32using Physics2DDotNet.Joints;
33using Physics2DDotNet.Solvers;
34using System.Collections.Generic;
35
36namespace Jypeli
37{
38    /// <summary>
39    /// Peli, johon voi lisätä pintoja, joiden päällä oliot voivat liukua. Peliin lisätyt <code>PhysicsObject</code>-oliot
40    /// käyttäytyvät fysiikan lakien mukaan.
41    /// </summary>
42    public class TopDownPhysicsGame : PhysicsGameBase
43    {
44        private FrictionLogic frictionlogic;
45        private List<PhysicsObject> surfaces = new List<PhysicsObject>();
46
47        private SequentialImpulsesSolver solver;
48
49        /// <summary>
50        /// The surfaces on which other objects may slide.
51        /// </summary>
52        /// <remarks>
53        /// The purpose of having surfaces in a separate list is to
54        /// have less checks between objects and thus keep the performance
55        /// good. Although the worst-case scenario is still O(n^2), this
56        /// happens only when there are a lot of surfaces. One way to
57        /// optimize that might be to store the surfaces in a spatial hash.
58        ///
59        /// It might make sense not to add the surfaces to the physics engine at all.
60        /// Or to have only IShape objects as surfaces instead of physics objects.
61        /// Although someone might of course want to listen collisions for surfaces.
62        /// </remarks>
63        internal List<PhysicsObject> Surfaces { get { return surfaces; } }
64       
65        /// <summary>
66        /// Painovoima. Mitä suurempi painovoima, sitä suurempi liikekitka
67        /// kaikille olioille.
68        /// </summary>
69        public double Gravity { get; set; }
70
71        /// <summary>
72        /// Liikekitka pinnalla. Tätä arvoa käytetään, kun liikkuva kappale
73        /// ei ole minkään lisätyn pinnan päällä. Arvot tyypillisesti välillä
74        /// <c>0.0</c>-<c>1.0</c>.
75        /// </summary>
76        public double KineticFriction { get; set; }
77
78        /// <summary>
79        /// Alustaa uuden fysiikkapelin.
80        /// </summary>
81        public TopDownPhysicsGame()
82            : this( 1 )
83        {
84        }
85
86        /// <summary>
87        /// Alustaa uuden fysiikkapelin.
88        /// </summary>
89        /// <param name="device">Mikä monitori käytössä, 1=ensimmäinen</param>
90        public TopDownPhysicsGame( int device )
91            : base( device )
92        {
93            phsEngine.BroadPhase = new Physics2DDotNet.Detectors.SelectiveSweepDetector();
94            solver = new SequentialImpulsesSolver();
95            phsEngine.Solver = (CollisionSolver)solver;
96
97            Gravity = 1.0;
98            KineticFriction = 1.0;
99
100            SetPhysics();
101        }
102
103        private void SetPhysics()
104        {
105            solver.Iterations = 12;
106            solver.SplitImpulse = true;
107            //solver.BiasFactor = 0.7f;
108            solver.BiasFactor = 0.0f;
109            //solver.AllowedPenetration = 0.1f;
110            solver.AllowedPenetration = 0.01f;
111
112            frictionlogic = new FrictionLogic( this, new Lifespan() );
113            phsEngine.AddLogic( frictionlogic );
114        }
115
116        /// <summary>
117        /// Lisää peliin pinnan, jonka päällä muut oliot voivat liukua.
118        /// </summary>
119        /// <remarks>
120        /// Pinnalle asetetaan automaattisesti <c>IgnoresCollisionResponse</c> arvoon <c>true</c>.
121        /// </remarks>
122        /// <param name="surface"></param>
123        public void AddSurface( PhysicsObject surface )
124        {
125            surface.IgnoresCollisionResponse = true;
126            surfaces.Add( surface );
127            Add( surface );
128        }
129
130        /// <summary>
131        /// Nollaa kaiken (kontrollit, näyttöobjektit, ajastimet ja fysiikkamoottorin).
132        /// </summary>
133        public override void ClearAll()
134        {
135            surfaces.Clear();
136            base.ClearAll();
137        }
138    }
139}
Note: See TracBrowser for help on using the repository browser.