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

Revision 5974, 3.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#if !(WINDOWS_PHONE || XBOX)
37    [Serializable]
38#endif
39    public sealed class VelocityLimitLogic : PhysicsLogic
40    {
41        Scalar maxLinearVelocity;
42        Scalar maxAngularVelocity;
43        public VelocityLimitLogic(Scalar maxLinearVelocity, Scalar maxAngularVelocity, Lifespan lifetime)
44            : base(lifetime)
45        {
46            if (maxLinearVelocity < 0) { throw new ArgumentOutOfRangeException("maxLinearVelocity"); }
47            if (maxAngularVelocity < 0) { throw new ArgumentOutOfRangeException("maxAngularVelocity"); }
48            this.maxLinearVelocity = maxLinearVelocity;
49            this.maxAngularVelocity = maxAngularVelocity;
50        }
51        protected internal override void RunLogic(TimeStep step)
52        {
53            foreach (Body e in Bodies)
54            {
55                if (e.IgnoresPhysicsLogics) { continue; }
56                Scalar velocity;
57                Vector2D.GetMagnitude(ref e.State.Velocity.Linear, out velocity);
58                if (velocity > maxLinearVelocity)
59                {
60                    velocity = maxLinearVelocity / velocity;
61                    Vector2D.Multiply(ref e.State.Velocity.Linear, ref velocity, out e.State.Velocity.Linear);
62                }
63                if (e.State.Velocity.Angular > maxAngularVelocity)
64                {
65                    e.State.Velocity.Angular = maxAngularVelocity;
66                }
67                else if (e.State.Velocity.Angular < -maxAngularVelocity)
68                {
69                    e.State.Velocity.Angular = -maxAngularVelocity;
70                }
71            }
72        }
73    }
74}
Note: See TracBrowser for help on using the repository browser.