Mercurial > projects > openmelee
annotate ship.d @ 7:2217fd1fe384
added angular and linear velocity limit
author | zzzzrrr <mason.green@gmail.com> |
---|---|
date | Sat, 21 Mar 2009 19:34:20 -0400 |
parents | eb6059f7035a |
children | 4ee9e4a0c03b |
rev | line source |
---|---|
0 | 1 /* |
2 * Copyright (c) 2009, Mason Green (zzzzrrr) | |
3 * | |
4 * All rights reserved. | |
5 * | |
6 * Redistribution and use in source and binary forms, with or without modification, | |
7 * are permitted provided that the following conditions are met: | |
8 * | |
9 * * Redistributions of source code must retain the above copyright notice, | |
10 * this list of conditions and the following disclaimer. | |
11 * * Redistributions in binary form must reproduce the above copyright notice, | |
12 * this list of conditions and the following disclaimer in the documentation | |
13 * and/or other materials provided with the distribution. | |
14 * * Neither the name of the polygonal nor the names of its contributors may be | |
15 * used to endorse or promote products derived from this software without specific | |
16 * prior written permission. | |
17 * | |
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR | |
22 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | |
23 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | |
24 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | |
25 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | |
26 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | |
27 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | |
28 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
29 */ | |
2 | 30 module openmelee.ship; |
0 | 31 |
32 import tango.io.Stdout: Stdout; | |
33 | |
34 import tango.util.container.LinkedList : LinkedList; | |
35 import blaze.dynamics.bzBody : bzBody; | |
36 import blaze.collision.shapes.bzShape : bzShape; | |
7
2217fd1fe384
added angular and linear velocity limit
zzzzrrr <mason.green@gmail.com>
parents:
6
diff
changeset
|
37 import blaze.common.bzMath: bzVec2, bzCross, bzClamp; |
6 | 38 import blaze.bzWorld : bzWorld; |
39 import blaze.dynamics.forces.bzAttractor: bzAttractor; | |
0 | 40 |
41 alias LinkedList!(bzShape) ShapeList; | |
42 | |
43 abstract class Ship | |
44 { | |
6 | 45 bzWorld world; |
0 | 46 bzBody rBody; |
47 ShapeList shapes; | |
48 bzVec2 engineForce; | |
49 bzVec2 turnForce; | |
50 bzVec2 leftTurnPoint; | |
51 bzVec2 rightTurnPoint; | |
52 | |
7
2217fd1fe384
added angular and linear velocity limit
zzzzrrr <mason.green@gmail.com>
parents:
6
diff
changeset
|
53 float maxLinVel = 100; |
2217fd1fe384
added angular and linear velocity limit
zzzzrrr <mason.green@gmail.com>
parents:
6
diff
changeset
|
54 float maxAngVel = 2; |
2217fd1fe384
added angular and linear velocity limit
zzzzrrr <mason.green@gmail.com>
parents:
6
diff
changeset
|
55 |
6 | 56 this(bzWorld world) { |
7
2217fd1fe384
added angular and linear velocity limit
zzzzrrr <mason.green@gmail.com>
parents:
6
diff
changeset
|
57 |
6 | 58 this.world = world; |
0 | 59 shapes = new ShapeList; |
60 } | |
61 | |
62 void thrust() { | |
7
2217fd1fe384
added angular and linear velocity limit
zzzzrrr <mason.green@gmail.com>
parents:
6
diff
changeset
|
63 |
0 | 64 rBody.force += engineForce.rotate(rBody.angle); |
65 } | |
66 | |
5 | 67 void turnLeft() { |
7
2217fd1fe384
added angular and linear velocity limit
zzzzrrr <mason.green@gmail.com>
parents:
6
diff
changeset
|
68 |
5 | 69 rBody.torque += bzCross(leftTurnPoint.rotate(rBody.angle), |
0 | 70 turnForce.rotate(rBody.angle)); |
5 | 71 } |
72 | |
73 void turnRight() { | |
7
2217fd1fe384
added angular and linear velocity limit
zzzzrrr <mason.green@gmail.com>
parents:
6
diff
changeset
|
74 |
5 | 75 rBody.torque += bzCross(rightTurnPoint.rotate(rBody.angle), |
0 | 76 turnForce.rotate(rBody.angle)); |
77 } | |
6 | 78 |
79 void setGravity() { | |
80 | |
7
2217fd1fe384
added angular and linear velocity limit
zzzzrrr <mason.green@gmail.com>
parents:
6
diff
changeset
|
81 float minRadius = 0.1; |
2217fd1fe384
added angular and linear velocity limit
zzzzrrr <mason.green@gmail.com>
parents:
6
diff
changeset
|
82 float maxRadius = 10; |
2217fd1fe384
added angular and linear velocity limit
zzzzrrr <mason.green@gmail.com>
parents:
6
diff
changeset
|
83 float strength = 3.5; |
6 | 84 bzVec2 center = bzVec2(0,0); |
85 auto attractor = new bzAttractor(rBody, center, strength, minRadius, maxRadius); | |
86 world.addForce(attractor); | |
87 } | |
7
2217fd1fe384
added angular and linear velocity limit
zzzzrrr <mason.green@gmail.com>
parents:
6
diff
changeset
|
88 |
2217fd1fe384
added angular and linear velocity limit
zzzzrrr <mason.green@gmail.com>
parents:
6
diff
changeset
|
89 void limitVelocity() { |
2217fd1fe384
added angular and linear velocity limit
zzzzrrr <mason.green@gmail.com>
parents:
6
diff
changeset
|
90 |
2217fd1fe384
added angular and linear velocity limit
zzzzrrr <mason.green@gmail.com>
parents:
6
diff
changeset
|
91 float vx = rBody.linearVelocity.x; |
2217fd1fe384
added angular and linear velocity limit
zzzzrrr <mason.green@gmail.com>
parents:
6
diff
changeset
|
92 float vy = rBody.linearVelocity.y; |
2217fd1fe384
added angular and linear velocity limit
zzzzrrr <mason.green@gmail.com>
parents:
6
diff
changeset
|
93 float av = rBody.angularVelocity; |
2217fd1fe384
added angular and linear velocity limit
zzzzrrr <mason.green@gmail.com>
parents:
6
diff
changeset
|
94 rBody.linearVelocity.x = bzClamp(vx, -maxLinVel, maxLinVel); |
2217fd1fe384
added angular and linear velocity limit
zzzzrrr <mason.green@gmail.com>
parents:
6
diff
changeset
|
95 rBody.linearVelocity.y = bzClamp(vy, -maxLinVel, maxLinVel); |
2217fd1fe384
added angular and linear velocity limit
zzzzrrr <mason.green@gmail.com>
parents:
6
diff
changeset
|
96 rBody.angularVelocity = bzClamp(av, -maxAngVel, maxAngVel); |
2217fd1fe384
added angular and linear velocity limit
zzzzrrr <mason.green@gmail.com>
parents:
6
diff
changeset
|
97 } |
0 | 98 } |