Skip to content

Commit

Permalink
Merge branch 'camera'
Browse files Browse the repository at this point in the history
  • Loading branch information
zenled committed Dec 25, 2017
2 parents e21dc07 + c5a0843 commit cda6582
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 6 deletions.
12 changes: 9 additions & 3 deletions web/game.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import 'input_handler.dart';

import 'game_objects/root_object.dart';
import 'game_objects/player/player.dart';
import 'game_objects/main_camera.dart';
import 'game_objects/pyramid.dart';

class Game {
Expand All @@ -34,6 +35,8 @@ class Game {

// game objects
RootObject rootObject;
MainCamera mainCamera;
Player player;

Game() {
// inits Matrix-es
Expand Down Expand Up @@ -86,9 +89,11 @@ class Game {

// Player pyramid = new Player();
// pyramid.translateZ(-10.0);
Player player = new Player();
player = new Player();
player.z = -10.0;

mainCamera = new MainCamera(player);

Pyramid pyramid = new Pyramid();
pyramid.translateZ(-20.0);
pyramid.translateX(5.0);
Expand Down Expand Up @@ -121,8 +126,7 @@ class Game {
gl.enable(DEPTH_TEST);
gl.disable(BLEND);

_pMatrix =
Matrix4.perspective(45.0, canvas.width / canvas.height, 0.1, 100.0);
mainCamera.updatePMatrix();

rootObject.draw();
}
Expand Down Expand Up @@ -154,6 +158,8 @@ class Game {

Matrix4 get pMatrix => _pMatrix;

void set pMatrix(Matrix4 value) => _pMatrix = value;

Matrix4 get mvMatrix => _mvMatrix;

InputHandler get inputHandler => _inputHandler;
Expand Down
1 change: 1 addition & 0 deletions web/game_objects/game_object.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import '../global.dart' as global;
import '../input_handler.dart';
import '../matrix4.dart';

export 'game_object.dart';
export '../global.dart';
export '../input_handler.dart';
export '../matrix4.dart';
Expand Down
42 changes: 42 additions & 0 deletions web/game_objects/main_camera.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import 'game_object.dart';
import '../global.dart';

class MainCamera {
static const fovy_degrees = 45.0;
static const near_plane = 0.1;
static const far_plane = 100.0;

// at what position relative to [following] will the camera be
static const offsetX = 0.0;
static const offsetY = -2.0;
static const offsetZ = -10.0;

static const double _initial_rotationX = 20.0;

double rotationX;

final GameObject following;

/// GameObject that the camera will follow
MainCamera(this.following) {
rotationX = _initial_rotationX;
}

void updatePMatrix() {
pMatrix = Matrix4.perspective(
fovy_degrees,
canvas.width / canvas.height,
near_plane,
far_plane,
);

Matrix4 m4 = new Matrix4()..identity();
m4.rotateX(radians(rotationX));
m4.rotateY(radians(-following.rotationY));
pMatrix.translate([offsetX, offsetY, offsetZ]);
m4.translate([-following.x, -following.y, -following.z]);

pMatrix *= m4;
}
}
8 changes: 5 additions & 3 deletions web/game_objects/player/player.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
library Player;

import '../game_object.dart';
import '../pyramid.dart';

class Player extends GameObject {
static const xSpeed = 0.04;
static const ySpeed = 0.04;
static const zSpeed = 0.04;

static const yRotationSpeed = 2.0;

// how many degrees shotld the drone tilt (this does not affect position)
Expand All @@ -32,7 +34,7 @@ class Player extends GameObject {
translateZ(zSpeed);
droneBody.rotationX = movingTilt_moving;
preserveTiltFB = true;
}
}
if (!preserveTiltFB) {
droneBody.rotationX = movingTilt_notMoving;
}
Expand All @@ -48,7 +50,7 @@ class Player extends GameObject {
translateX(-xSpeed);
droneBody.rotationZ = movingTilt_moving;
preserveTiltLR = true;
}
}
if (!preserveTiltLR) {
droneBody.rotationZ = movingTilt_notMoving;
}
Expand Down
2 changes: 2 additions & 0 deletions web/global.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ int get attributePointerColor => game.attributePointerColor;

Matrix4 get pMatrix => game.pMatrix;

void set pMatrix(Matrix4 value) => game.pMatrix = value;

Matrix4 get mvMatrix => game.mvMatrix;

mvPushMatrix() => game.mvPushMatrix();
Expand Down

0 comments on commit cda6582

Please sign in to comment.