Jypeli  9
The simple game programming library
Dimensions.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 
32 namespace Jypeli
33 {
34  public partial class GameObject
35  {
36  private Vector _size;
37  private Shape _shape;
38 
43  public override Vector Size
44  {
45  get
46  {
48  {
49  // This is needed if, for example, child objects have
50  // been added right before the size is read.
51  RefreshLayout();
52  }
53  return _size;
54  }
55  set
56  {
57  if ( value.X < 0.0 || value.Y < 0.0 )
58  throw new ArgumentException( "The size must be positive." );
59 
60  Vector oldSize = _size;
61  _size = value;
62 
64  {
65  UpdateChildSizes(oldSize, value);
66  }
67  }
68  }
69 
74  public override Angle Angle { get; set; }
75 
79  public virtual Shape Shape
80  {
81  get { return _shape; }
82  set { _shape = value; }
83  }
84 
88  internal string ShapeString
89  {
90  get { return Shape.GetType().Name; }
91  set { Shape = Shape.FromString( value ); }
92  }
93 
94  private void InitDimensions( double width, double height, Shape shape )
95  {
96  this._size = new Vector( width, height );
97  this._shape = shape;
98  }
99 
103  public bool IsInside( Vector point )
104  {
105  Vector p = this.AbsolutePosition;
106  double pX, pY;
107  double pointX, pointY;
108 
109  if ( AbsoluteAngle == Angle.Zero )
110  {
111  // A special (faster) case of the general case below
112  pX = p.X;
113  pY = p.Y;
114  pointX = point.X;
115  pointY = point.Y;
116  }
117  else
118  {
119  Vector unitX = Vector.FromLengthAndAngle( 1, this.AbsoluteAngle );
120  Vector unitY = unitX.LeftNormal;
121  pX = p.ScalarProjection( unitX );
122  pY = p.ScalarProjection( unitY );
123  pointX = point.ScalarProjection( unitX );
124  pointY = point.ScalarProjection( unitY );
125  }
126 
127  if ( Shape.IsUnitSize )
128  {
129  double x = 2 * ( pointX - pX ) / this.Width;
130  double y = 2 * ( pointY - pY ) / this.Height;
131  if ( this.Shape.IsInside( x, y ) )
132  return true;
133  }
134  else
135  {
136  double x = pointX - pX;
137  double y = pointY - pY;
138  if ( this.Shape.IsInside( x, y ) )
139  return true;
140  }
141 
142  return IsInsideChildren( point );
143  }
144 
148  public bool IsInsideRect( Vector point )
149  {
150  Vector p = this.AbsolutePosition;
151 
152  if ( AbsoluteAngle == Angle.Zero )
153  {
154  // A special (faster) case of the general case below
155  if ( point.X >= ( p.X - Width / 2 )
156  && point.X <= ( p.X + Width / 2 )
157  && point.Y >= ( p.Y - Height / 2 )
158  && point.Y <= ( p.Y + Height / 2 ) ) return true;
159  }
160  else
161  {
162  Vector unitX = Vector.FromLengthAndAngle( 1, this.AbsoluteAngle );
163  Vector unitY = unitX.LeftNormal;
164  double pX = p.ScalarProjection( unitX );
165  double pY = p.ScalarProjection( unitY );
166  double pointX = point.ScalarProjection( unitX );
167  double pointY = point.ScalarProjection( unitY );
168 
169  if ( pointX >= ( pX - Width / 2 )
170  && pointX <= ( pX + Width / 2 )
171  && pointY >= ( pY - Height / 2 )
172  && pointY <= ( pY + Height / 2 ) ) return true;
173  }
174 
175  return IsInsideChildren( point );
176  }
177 
184  public bool IsBetween(Vector pos1, Vector pos2)
185  {
186  Vector normal = (pos2 - pos1).Normalize();
187  double ep = this.AbsolutePosition.ScalarProjection(normal);
188  double p1p = pos1.ScalarProjection(normal);
189  double p2p = pos2.ScalarProjection(normal);
190 
191  if (ep < p1p || ep > p2p)
192  return false;
193 
194  double pn = pos1.ScalarProjection(normal.RightNormal);
195  double en = this.AbsolutePosition.ScalarProjection(normal.RightNormal);
196  return Math.Abs(en - pn) <= 0.5 * Math.Sqrt(this.Width * this.Width + this.Height * this.Height);
197  }
198  }
199 }
Jypeli.GameObject.UpdateChildSizes
void UpdateChildSizes(Vector oldSize, Vector newSize)
Definition: ChildObjects.cs:182
Jypeli.Vector.LeftNormal
Vector LeftNormal
Vasen normaali.
Definition: Vector.cs:89
Jypeli.Vector.X
double X
Definition: Vector.cs:312
Jypeli
Definition: Automobile.cs:5
Jypeli.GameObject.RefreshLayout
void RefreshLayout()
Päivittää lapsiolioiden paikat ja koot, jos widgetille on asetettu asettelija. Tätä metodia EI yleens...
Definition: Layout.cs:135
Jypeli.GameObject.ShapeString
string ShapeString
Olion muoto merkkijonona (kenttäeditorin käyttöön)
Definition: Dimensions.cs:89
Jypeli.Vector.RightNormal
Vector RightNormal
Oikea normaali.
Definition: Vector.cs:97
Jypeli.Shape
Kuvio.
Definition: Shapes.cs:47
Jypeli.Vector.ScalarProjection
double ScalarProjection(Vector unitVector)
Definition: Vector.cs:181
Jypeli.Shape.FromString
static Shape FromString(string shapeStr)
Luo muodon merkkijonosta, esim. "Circle"
Definition: Shapes.cs:144
Jypeli.GameObject.IsInside
bool IsInside(Vector point)
Onko piste p tämän olion sisäpuolella.
Definition: Dimensions.cs:103
Jypeli.GameObject.IsInsideChildren
bool IsInsideChildren(Vector point)
Definition: ChildObjects.cs:201
Jypeli.GameObject._size
Vector _size
Definition: Dimensions.cs:36
Jypeli.Shape.IsInside
virtual bool IsInside(double x, double y)
Onko piste muodon sisällä. Pisteen koordinaatiston origo on muodon keskellä. Muoto on kokoa 1x1 jos I...
Definition: Shapes.cs:272
Jypeli.GameObject.InitDimensions
void InitDimensions(double width, double height, Shape shape)
Definition: Dimensions.cs:94
Jypeli.GameObject._layoutNeedsRefreshing
bool _layoutNeedsRefreshing
Definition: Layout.cs:11
Jypeli.Angle.Zero
static readonly Angle Zero
Nollakulma.
Definition: Angle.cs:44
Jypeli.GameObject.IsInsideRect
bool IsInsideRect(Vector point)
Onko piste p tämän olion ympäröivän suorakulmion sisäpuolella.
Definition: Dimensions.cs:148
Jypeli.Vector.FromLengthAndAngle
static Vector FromLengthAndAngle(double length, double angle)
Luo vektorin pituuden ja kulman perusteella.
Definition: Vector.cs:106
Jypeli.Vector
2D-vektori.
Definition: Vector.cs:59
System
Definition: CFFauxAttributes.cs:29
Jypeli.GameObject.Size
override Vector Size
Olion koko pelimaailmassa. Kertoo olion äärirajat, ei muotoa.
Definition: Dimensions.cs:44
Jypeli.GameObject.autoResizeChildObjects
bool autoResizeChildObjects
Definition: ChildObjects.cs:38
Jypeli.GameObject._shape
Shape _shape
Definition: Dimensions.cs:37
Jypeli.Shape.IsUnitSize
abstract bool IsUnitSize
If true, the shape must be scaled by the size of the object that has the shape. Typically,...
Definition: Shapes.cs:53
Jypeli.GameObject.IsBetween
bool IsBetween(Vector pos1, Vector pos2)
Onko peliolio kahden pisteen välissä
Definition: Dimensions.cs:184
Jypeli.Vector.Y
double Y
Definition: Vector.cs:313
Jypeli.Angle
Suuntakulma (rajoitettu -180 ja 180 asteen välille) asteina ja radiaaneina. Tietoja kulmasta: http://...
Definition: Angle.cs:40