source: 2013/30/MiskaK/MW2(My Warfare 2)/Paranneltu Jypeli/Game/PhysicsGame.cs @ 4507

Revision 4507, 3.7 KB checked in by anlakane, 6 years ago (diff)

Talletus.

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.PhysicsLogics;
33using Physics2DDotNet.Joints;
34using Physics2DDotNet.Solvers;
35using AdvanceMath;
36using System.Collections.Generic;
37using Physics2DDotNet.Ignorers;
38
39namespace Jypeli
40{
41    /// <summary>
42    /// Peli, jossa on fysiikan laskenta mukana. Peliin lisätyt <code>PhysicsObject</code>-oliot
43    /// käyttäytyvät fysiikan lakien mukaan.
44    /// </summary>
45    public class PhysicsGame : PhysicsGameBase
46    {
47        private GravityField gravityfield;
48        private Vector gravity = Vector.Zero;
49
50        static internal Dictionary<int, ObjectIgnorer> IgnoreGroups = null;
51
52        /// <summary>
53        /// Painovoima. Voimavektori, joka vaikuttaa kaikkiin ei-staattisiin kappaleisiin.
54        /// </summary>
55        public Vector Gravity
56        {
57            get
58            {
59                return gravity;
60            }
61            set
62            {
63                gravity = value;
64                updatePhysicsConstants();
65            }
66        }
67
68        /// <summary>
69        /// Alustaa uuden fysiikkapelin.
70        /// </summary>
71        public PhysicsGame()
72            : this( 1 )
73        {
74        }
75
76        /// <summary>
77        /// Alustaa uuden fysiikkapelin.
78        /// </summary>
79        /// <param name="device">Mikä monitori käytössä, 1=ensimmäinen</param>
80        public PhysicsGame( int device )
81            : base( device )
82        {
83            phsEngine.BroadPhase = new Physics2DDotNet.Detectors.SelectiveSweepDetector();
84            //phsEngine.BroadPhase = new Physics2DDotNet.Detectors.SpatialHashDetector();
85
86            SequentialImpulsesSolver phsSolver = new SequentialImpulsesSolver();
87            phsSolver.Iterations = 12;
88            phsSolver.SplitImpulse = true;
89            //phsSolver.BiasFactor = 0.7;
90            phsSolver.BiasFactor = 0.0;
91            //phsSolver.AllowedPenetration = 0.1;
92            phsSolver.AllowedPenetration = 0.01;
93            phsEngine.Solver = (CollisionSolver)phsSolver;
94        }
95
96        private void updatePhysicsConstants()
97        {
98            if ( gravityfield != null ) gravityfield.Lifetime.IsExpired = true;
99
100            if ( gravity != Vector.Zero )
101            {
102                gravityfield = new GravityField( new Vector2D( gravity.X, gravity.Y ), new Lifespan() );
103                phsEngine.AddLogic( gravityfield );
104            }
105        }       
106    }
107}
Note: See TracBrowser for help on using the repository browser.