source: 2014/24/EemeliK/Zombieland/Jypeli/Physics2DDotNet/PhysicsLogics/GravityPointMass.cs @ 5974

Revision 5974, 4.0 KB checked in by empaheik, 4 years ago (diff)
Line 
1#region MIT License
2/*
3 * Copyright (c) 2005-2008 Jonathan Mark Porter. http://physics2d.googlepages.com/
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights to
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9 * the Software, and to permit persons to whom the Software is furnished to do so,
10 * subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
16 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
17 * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
21 */
22#endregion
23
24
25#if UseDouble
26using Scalar = System.Double;
27#else
28using Scalar = System.Single;
29#endif
30using System;
31
32using AdvanceMath;
33
34namespace Physics2DDotNet.PhysicsLogics
35{
36    /// <summary>
37    /// A PhysicsLogic to causes a Body to have a realistic pull of Gravity with a exponential drop-off.
38    /// </summary>
39#if !(WINDOWS_PHONE || XBOX)
40    [Serializable]
41#endif
42    public sealed class GravityPointMass : PhysicsLogic
43    {
44
45
46        Scalar metersPerDistanceUnit;
47        Body body;
48        /// <summary>
49        /// Creates a new GravityPointMass Instance.
50        /// </summary>
51        /// <param name="body">The body that will be the source of gravity.</param>
52        /// <param name="lifetime">A object Describing how long the object will be in the engine.</param>
53        public GravityPointMass(Body body):
54            this(body,1)
55        {}
56        /// <summary>
57        /// Creates a new GravityPointMass Instance.
58        /// </summary>
59        /// <param name="body">The body that will be the source of gravity.</param>
60        /// <param name="metersPerDistanceUnit">The scale of of the universe.</param>
61        /// <param name="lifetime">A object Describing how long the object will be in the engine.</param>
62        public GravityPointMass(Body body, Scalar metersPerDistanceUnit)
63            : base(GetLifeTime(body))
64        {
65            if (metersPerDistanceUnit <= 0) { throw new ArgumentOutOfRangeException("metersPerDistanceUnit"); }
66            this.body = body;
67            this.metersPerDistanceUnit = metersPerDistanceUnit;
68        }
69        protected internal override void RunLogic(TimeStep step)
70        {
71            foreach (Body e in Bodies)
72            {
73                if (e == body ||
74                    e.IgnoresGravity ||
75                    e.IgnoresPhysicsLogics)
76                {
77                    continue;
78                }
79                Scalar magnitude;
80                Vector2D gravity;
81                Vector2D.Subtract(ref body.State.Position.Linear, ref e.State.Position.Linear, out gravity);
82                Vector2D.Normalize(ref gravity, out magnitude, out gravity);
83                magnitude = (body.Mass.AccelerationDueToGravity /
84                        (magnitude * magnitude * metersPerDistanceUnit * metersPerDistanceUnit));
85                Vector2D.Multiply(ref gravity, ref magnitude, out gravity);
86                Vector2D.Add(ref e.State.Acceleration.Linear, ref gravity, out e.State.Acceleration.Linear);
87            }
88        }
89        protected internal override void BeforeAddCheck(PhysicsEngine engine)
90        {
91            if (body.Engine != engine) { throw new InvalidOperationException("The Body must be added to the Engine before the GravityPointMass."); }
92        }
93    }
94}
Note: See TracBrowser for help on using the repository browser.