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 | |
---|
41 | /// <summary> |
---|
42 | /// Jos <c>false</c>, olio ei voi pyöriä. |
---|
43 | /// </summary> |
---|
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 | |
---|
62 | /// <summary> |
---|
63 | /// Olion massa. Mitä suurempi massa, sitä suurempi voima tarvitaan olion liikuttamiseksi. |
---|
64 | /// </summary> |
---|
65 | /// <remarks> |
---|
66 | /// Massan asettaminen muuttaa myös hitausmomenttia (<c>MomentOfInertia</c>). |
---|
67 | /// </remarks> |
---|
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 | |
---|
90 | /// <summary> |
---|
91 | /// Olion hitausmomentti. Mitä suurempi hitausmomentti, sitä enemmän vääntöä tarvitaan |
---|
92 | /// olion pyörittämiseksi. |
---|
93 | /// </summary> |
---|
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 | |
---|
104 | /// <summary> |
---|
105 | /// Tekee suorakulmaisen kappaleen, joka on staattinen (eli pysyy paikallaan). |
---|
106 | /// </summary> |
---|
107 | /// <param name="width">Kappaleen leveys</param> |
---|
108 | /// <param name="height">Kappaleen korkeus</param> |
---|
109 | /// <returns>Fysiikkaolio</returns> |
---|
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 | |
---|
117 | /// <summary> |
---|
118 | /// Tekee suorakulmaisen kappaleen, joka on staattinen (eli pysyy paikallaan). |
---|
119 | /// Kappaleen koko ja ulkonäkö ladataan parametrina annetusta kuvasta. |
---|
120 | /// </summary> |
---|
121 | /// <param name="width">Kappaleen leveys</param> |
---|
122 | /// <param name="height">Kappaleen korkeus</param> |
---|
123 | /// <returns>Fysiikkaolio</returns> |
---|
124 | public static PhysicsObject CreateStaticObject( Image image ) |
---|
125 | { |
---|
126 | PhysicsObject o = new PhysicsObject( image ); |
---|
127 | o.MakeStatic(); |
---|
128 | return o; |
---|
129 | } |
---|
130 | |
---|
131 | /// <summary> |
---|
132 | /// Tekee vapaamuotoisen kappaleen, joka on staattinen (eli pysyy paikallaan). |
---|
133 | /// </summary> |
---|
134 | /// <param name="width">Kappaleen leveys</param> |
---|
135 | /// <param name="height">Kappaleen korkeus</param> |
---|
136 | /// <param name="shape">Kappaleen muoto</param> |
---|
137 | /// <returns>Fysiikkaolio</returns> |
---|
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 | |
---|
175 | /// <summary> |
---|
176 | /// Tekee oliosta staattisen. Staattinen olio ei liiku muiden olioiden törmäyksistä, |
---|
177 | /// vaan ainoastaan muuttamalla suoraan sen paikkaa tai nopeutta. |
---|
178 | /// </summary> |
---|
179 | public void MakeStatic() |
---|
180 | { |
---|
181 | Mass = double.PositiveInfinity; |
---|
182 | CanRotate = false; |
---|
183 | IgnoresGravity = true; |
---|
184 | } |
---|
185 | |
---|
186 | /// <summary> |
---|
187 | /// Olion hidastuminen. Hidastaa olion vauhtia, vaikka se ei |
---|
188 | /// osuisi mihinkään. Vähän kuin väliaineen (esim. ilman tai veden) |
---|
189 | /// vastus. Oletusarvo on 1.0, jolloin hidastumista ei ole. Mitä |
---|
190 | /// pienempi arvo, sitä enemmän kappale hidastuu. |
---|
191 | /// |
---|
192 | /// Yleensä kannattaa käyttää arvoja, jotka ovat lähellä ykköstä, |
---|
193 | /// esim. 0.95. |
---|
194 | /// </summary> |
---|
195 | public double LinearDamping |
---|
196 | { |
---|
197 | get { return Body.LinearDamping; } |
---|
198 | set { Body.LinearDamping = value; } |
---|
199 | } |
---|
200 | |
---|
201 | /// <summary> |
---|
202 | /// Olion pyörimisen hidastuminen. |
---|
203 | /// |
---|
204 | /// <see cref="LinearDamping"/> |
---|
205 | /// </summary> |
---|
206 | public double AngularDamping |
---|
207 | { |
---|
208 | get { return Body.AngularDamping; } |
---|
209 | set { Body.AngularDamping = value; } |
---|
210 | } |
---|
211 | |
---|
212 | /// <summary> |
---|
213 | /// This updates the mass and momentOfInertia based on the new mass. |
---|
214 | /// </summary> |
---|
215 | private void SetMassAndInertia( double mass ) |
---|
216 | { |
---|
217 | Body.Mass = Body.GetMassInfo( mass, Body.Shape ); |
---|
218 | } |
---|
219 | } |
---|
220 | } |
---|