This repository has been archived by the owner on Apr 7, 2021. It is now read-only.
forked from google/box2d.dart
/
ball_cage.dart
122 lines (101 loc) · 4.54 KB
/
ball_cage.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*******************************************************************************
* Copyright (c) 2015, Daniel Murphy, Google
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
******************************************************************************/
library BallCage;
import 'package:box2d_flame/box2d.dart';
import 'demo.dart';
class BallCage extends Demo {
/** Starting position of ball cage in the world. */
static const double START_X = -20.0;
static const double START_Y = -20.0;
/** The radius of the balls forming the arena. */
static const double WALL_BALL_RADIUS = 2.0;
/** Radius of the active ball. */
static const double ACTIVE_BALL_RADIUS = 1.0;
/** Constructs a new BallCage. */
BallCage() : super("Ball cage");
/** Entrypoint. */
static void main() {
final cage = new BallCage();
cage.initialize();
cage.initializeAnimation();
cage.runAnimation();
}
void initialize() {
// Define the circle shape.
final circleShape = new CircleShape();
circleShape.radius = WALL_BALL_RADIUS;
// Create fixture using the circle shape.
final circleFixtureDef = new FixtureDef();
circleFixtureDef.shape = circleShape;
circleFixtureDef.friction = .9;
circleFixtureDef.restitution = 1.0;
// Create a body def.
final circleBodyDef = new BodyDef();
int maxShapeinRow = 10;
final double borderLimitX =
START_X + maxShapeinRow * 2 * circleShape.radius;
final double borderLimitY =
START_Y + maxShapeinRow * 2 * circleShape.radius;
for (int i = 0; i < maxShapeinRow; i++) {
final double shiftX = START_X + circleShape.radius * 2 * i;
final double shiftY = START_Y + circleShape.radius * 2 * i;
circleBodyDef.position = new Vector2(shiftX, START_Y);
Body circleBody = world.createBody(circleBodyDef);
bodies.add(circleBody);
circleBody.createFixtureFromFixtureDef(circleFixtureDef);
circleBodyDef.position = new Vector2(shiftX, borderLimitY);
circleBody = world.createBody(circleBodyDef);
bodies.add(circleBody);
circleBody.createFixtureFromFixtureDef(circleFixtureDef);
circleBodyDef.position = new Vector2(START_X, shiftY);
circleBody = world.createBody(circleBodyDef);
bodies.add(circleBody);
circleBody.createFixtureFromFixtureDef(circleFixtureDef);
circleBodyDef.position = new Vector2(borderLimitX, shiftY);
circleBody = world.createBody(circleBodyDef);
bodies.add(circleBody);
circleBody.createFixtureFromFixtureDef(circleFixtureDef);
}
// Create a bouncing ball.
final bouncingCircle = new CircleShape();
bouncingCircle.radius = ACTIVE_BALL_RADIUS;
// Create fixture for that ball shape.
final activeFixtureDef = new FixtureDef();
activeFixtureDef.restitution = 1.0;
activeFixtureDef.density = 0.05;
activeFixtureDef.shape = bouncingCircle;
// Create the active ball body.
final activeBodyDef = new BodyDef();
activeBodyDef.linearVelocity = new Vector2(0.0, -20.0);
activeBodyDef.position = new Vector2(15.0, 15.0);
activeBodyDef.type = BodyType.DYNAMIC;
activeBodyDef.bullet = true;
final activeBody = world.createBody(activeBodyDef);
bodies.add(activeBody);
activeBody.createFixtureFromFixtureDef(activeFixtureDef);
}
}
void main() {
BallCage.main();
}