Jypeli  5
The simple game programming library
Inertia.cs
Siirry tämän tiedoston dokumentaatioon.
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 
30 using System;
31 using Physics2DDotNet;
32 using Physics2DDotNet.Shapes;
33 
34 namespace Jypeli
35 {
36  public partial class PhysicsObject
37  {
38  private const double DefaultMass = 1.0;
39  private bool _momentOfInertiaSet = false;
40 
44  public bool CanRotate
45  {
46  get { return !double.IsPositiveInfinity( MomentOfInertia ); }
47  set
48  {
49  if ( !value )
50  {
51  MomentOfInertia = double.PositiveInfinity;
52  _momentOfInertiaSet = true;
53  }
54  else
55  {
56  SetMassAndInertia( this.Mass );
57  _momentOfInertiaSet = false;
58  }
59  }
60  }
61 
68  public double Mass
69  {
70  get { return Body.Mass.Mass; }
71  set
72  {
73  // We change the moment of inertia as well. If the mass is changed from
74  // 1.0 to 100000.0 for example, it would look funny if a heavy object would
75  // spin wildly from a small touch.
76 
77  if ( _momentOfInertiaSet )
78  {
79  // The moment of inertia has been set manually,
80  // let's keep it that way and just set the mass.
81  Body.Mass.Mass = value;
82  }
83  else
84  {
85  SetMassAndInertia( value );
86  }
87  }
88  }
89 
94  public double MomentOfInertia
95  {
96  get { return Body.Mass.MomentOfInertia; }
97  set
98  {
99  Body.Mass.MomentOfInertia = value;
100  _momentOfInertiaSet = true;
101  }
102  }
103 
110  public static PhysicsObject CreateStaticObject( double width, double height )
111  {
112  PhysicsObject o = new PhysicsObject( width, height );
113  o.MakeStatic();
114  return o;
115  }
116 
124  public static PhysicsObject CreateStaticObject( Image image )
125  {
126  PhysicsObject o = new PhysicsObject( image );
127  o.MakeStatic();
128  return o;
129  }
130 
138  public static PhysicsObject CreateStaticObject( double width, double height, Shape shape )
139  {
140  PhysicsObject o = new PhysicsObject( width, height, shape );
141  o.MakeStatic();
142  return o;
143  }
144 
145  [Obsolete( "Use CollisionShapeParameters or the PhysicsTemplates class." )]
146  internal static PhysicsObject CreateStaticObject( double width, double height, Shape shape, CollisionShapeQuality quality )
147  {
148  PhysicsObject o = new PhysicsObject( width, height, shape );
149  o.MakeStatic();
150  return o;
151  }
152 
153  public static PhysicsObject CreateStaticObject( double width, double height, Shape shape, CollisionShapeParameters parameters )
154  {
155  PhysicsObject o = new PhysicsObject( width, height, shape, parameters );
156  o.MakeStatic();
157  return o;
158  }
159 
160  [Obsolete( "Use function with CollisionShapeParameters" )]
161  internal static PhysicsObject CreateStaticObject( double width, double height, Shape shape, double maxDistanceBetweenVertices, double gridSpacing )
162  {
163  PhysicsObject o = new PhysicsObject( width, height, shape, maxDistanceBetweenVertices, gridSpacing );
164  o.MakeStatic();
165  return o;
166  }
167 
168  internal static PhysicsObject CreateStaticObject( double width, double height, Shape shape, IShape physicsShape )
169  {
170  PhysicsObject o = new PhysicsObject( width, height, shape, physicsShape );
171  o.MakeStatic();
172  return o;
173  }
174 
179  public void MakeStatic()
180  {
181  Mass = double.PositiveInfinity;
182  CanRotate = false;
183  IgnoresGravity = true;
184  }
185 
195  public double LinearDamping
196  {
197  get { return Body.LinearDamping; }
198  set { Body.LinearDamping = value; }
199  }
200 
206  public double AngularDamping
207  {
208  get { return Body.AngularDamping; }
209  set { Body.AngularDamping = value; }
210  }
211 
215  private void SetMassAndInertia( double mass )
216  {
217  Body.Mass = Body.GetMassInfo( mass, Body.Shape );
218  }
219  }
220 }
Kuvio.
Definition: Shapes.cs:48
void MakeStatic()
Tekee oliosta staattisen. Staattinen olio ei liiku muiden olioiden törmäyksistä, vaan ainoastaan muut...
Definition: Inertia.cs:179
PhysicsObject(double width, double height)
Luo uuden fysiikkaolion.
double LinearDamping
Olion hidastuminen. Hidastaa olion vauhtia, vaikka se ei osuisi mihinkään. Vähän kuin väliaineen (esi...
Definition: Inertia.cs:196
static PhysicsObject CreateStaticObject(double width, double height, Shape shape)
Tekee vapaamuotoisen kappaleen, joka on staattinen (eli pysyy paikallaan).
Definition: Inertia.cs:138
bool IgnoresGravity
Jättääkö olio painovoiman huomioimatta.
bool CanRotate
Jos false, olio ei voi pyöriä.
Definition: Inertia.cs:45
Peliolio, joka noudattaa fysiikkamoottorin määräämiä fysiikan lakeja. Voidaan kuitenkin myös laittaa ...
Definition: Coefficients.cs:36
Törmäyskuvion laatuun vaikuttavat parametrit.
double AngularDamping
Olion pyörimisen hidastuminen.
Definition: Inertia.cs:207
static PhysicsObject CreateStaticObject(double width, double height, Shape shape, CollisionShapeParameters parameters)
Definition: Inertia.cs:153
double Mass
Olion massa. Mitä suurempi massa, sitä suurempi voima tarvitaan olion liikuttamiseksi.
Definition: Inertia.cs:69
Kuva.
Definition: Image.cs:24
double MomentOfInertia
Olion hitausmomentti. Mitä suurempi hitausmomentti, sitä enemmän vääntöä tarvitaan olion pyörittämise...
Definition: Inertia.cs:95
Body Body
Fysiikkamoottorin käyttämä tietorakenne.
Definition: Dimensions.cs:42
static PhysicsObject CreateStaticObject(double width, double height)
Tekee suorakulmaisen kappaleen, joka on staattinen (eli pysyy paikallaan).
Definition: Inertia.cs:110
Kappaleen kuvion laatu törmäyksentunnistuksessa.
static PhysicsObject CreateStaticObject(Image image)
Tekee suorakulmaisen kappaleen, joka on staattinen (eli pysyy paikallaan). Kappaleen koko ja ulkonäkö...
Definition: Inertia.cs:124