-
Notifications
You must be signed in to change notification settings - Fork 0
Tutorial 12
Jim edited this page Dec 4, 2023
·
1 revision
class CowTutorial extends App {
// NxN grid
// each grid cell is 1 unit long (this part set up in main)
static int N = 16;
// helper function for point-in-rectangle
boolean inBetween(double x, double x_min, double x_max) {
return (x > x_min) && (x < x_max);
}
// "state" for parabolic motion
Vector2 s; // position
Vector2 v; // velocity
Vector2 a; // acceleration
double h; // timestep ("delta t")
void setup() {
s = new Vector2(0.0, 0.0);
v = new Vector2(2.0, 4.0);
a = new Vector2(0.0, -1.0);
h = 0.1;
}
void loop() {
// highlight mouse's square in grid
if (true) {
// highlight square
int x = (int) mousePosition.x;
int y = (int) mousePosition.y;
drawCornerRectangle(new Vector2(x, y), new Vector2(x + 1, y + 1), Vector3.yellow);
// draw grid
for (int i = 0; i <= N; ++i) {
drawLine(new Vector2(0.0, i), new Vector2(N, i), Vector3.black);
drawLine(new Vector2(i, 0.0), new Vector2(i, N), Vector3.black);
}
}
// is mouse in rectangle?
if (true) {
Vector2 lowerLeftCorner = new Vector2(10.0, 40.0);
Vector2 upperRightCorner = new Vector2(30.0, 70.0);
drawCornerRectangle(lowerLeftCorner, upperRightCorner, Vector3.blue);
boolean isMouseInsideOfTheRectangle =
inBetween(mousePosition.x, lowerLeftCorner.x, upperRightCorner.x)
&& inBetween(mousePosition.y, lowerLeftCorner.y, upperRightCorner.y);
Vector3 circleColor;
if (isMouseInsideOfTheRectangle) {
circleColor = Vector3.red;
} else {
circleColor = Vector3.green;
}
drawCircle(mousePosition, 0.1, circleColor);
}
// parabolic (projectile) motion
if (true) {
// explicit euler update
v = v.plus(a.times(h)); // v += h * a;
s = s.plus(v.times(h)); // s += h * v;
drawCircle(s, 0.2, Vector3.red);
}
}
public static void main(String[] arguments) {
App app = new CowTutorial();
app.setWindowBackgroundColor(Vector3.white);
// // the units of your game
// make world an N x N square
app.setWindowSizeInWorldUnits(N, N);
// put world origin in lower left corner of the screen
app.setWindowCenterInWorldUnits(N / 2, N / 2);
// // NOT the units of your game
// how much of your screen the window takes up
app.setWindowHeightInPixels(1024);
// where the window spawns on your screen
app.setWindowTopLeftCornerInPixels(16, 16);
app.run();
}
}