mirror of
https://github.com/RandomityGuy/MBHaxe.git
synced 2025-10-30 08:11:25 +00:00
camera basic stuff
This commit is contained in:
parent
8d679e7f04
commit
ce935a04e6
4 changed files with 94 additions and 15 deletions
|
|
@ -1,3 +1,13 @@
|
||||||
|
package src;
|
||||||
|
|
||||||
|
import sdl.Cursor;
|
||||||
|
import sdl.Sdl;
|
||||||
|
import hxd.Window;
|
||||||
|
import hxd.Event;
|
||||||
|
import src.MarbleWorld;
|
||||||
|
import h3d.scene.Object;
|
||||||
|
import src.Marble;
|
||||||
|
import h3d.Camera;
|
||||||
import h3d.Vector;
|
import h3d.Vector;
|
||||||
import hxsl.Types.Matrix;
|
import hxsl.Types.Matrix;
|
||||||
import h3d.scene.Scene;
|
import h3d.scene.Scene;
|
||||||
|
|
@ -7,8 +17,9 @@ enum CameraMode {
|
||||||
FixedOrbit;
|
FixedOrbit;
|
||||||
}
|
}
|
||||||
|
|
||||||
class CameraController {
|
class CameraController extends Object {
|
||||||
var scene:Scene;
|
var marble:Marble;
|
||||||
|
var level:MarbleWorld;
|
||||||
|
|
||||||
var view:Matrix;
|
var view:Matrix;
|
||||||
var projection:Matrix;
|
var projection:Matrix;
|
||||||
|
|
@ -27,17 +38,72 @@ class CameraController {
|
||||||
|
|
||||||
var camZoomSpeed:Float;
|
var camZoomSpeed:Float;
|
||||||
|
|
||||||
public var CameraDistance = 5;
|
public var CameraDistance = 2.5;
|
||||||
public var CameraMinDistance = 1;
|
public var CameraMinDistance = 1;
|
||||||
public var CameraMaxDistance = 25;
|
public var CameraMaxDistance = 25;
|
||||||
public var CameraPitch:Float;
|
public var CameraPitch:Float;
|
||||||
public var CameraYaw:Float;
|
public var CameraYaw:Float;
|
||||||
|
|
||||||
public function new(scene:Scene) {
|
public var phi:Float;
|
||||||
this.scene = scene;
|
public var theta:Float;
|
||||||
|
|
||||||
|
var screenHeight:Int;
|
||||||
|
var screenWidth:Int;
|
||||||
|
|
||||||
|
public function new(marble:Marble) {
|
||||||
|
super();
|
||||||
|
this.marble = marble;
|
||||||
}
|
}
|
||||||
|
|
||||||
function createLookAt() {
|
public function init(level:MarbleWorld) {
|
||||||
this.view = Matrix.lookAtX(LookAtPoint.sub(Position), Up);
|
this.level = level;
|
||||||
|
Window.getInstance().addEventTarget(onEvent);
|
||||||
|
// level.scene.addEventListener(onEvent);
|
||||||
|
// Sdl.setRelativeMouseMode(true);
|
||||||
|
this.screenHeight = Sdl.getScreenHeight();
|
||||||
|
this.screenWidth = Sdl.getScreenWidth();
|
||||||
|
level.scene.camera.fovY = 60;
|
||||||
|
Cursor.show(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
function onEvent(e:Event) {
|
||||||
|
switch (e.kind) {
|
||||||
|
case EMove:
|
||||||
|
orbit(e.relX, e.relY);
|
||||||
|
Sdl.warpMouseGlobal(cast this.screenWidth / 2, cast this.screenHeight / 2);
|
||||||
|
default:
|
||||||
|
}
|
||||||
|
trace(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
function orbit(mouseX:Float, mouseY:Float) {
|
||||||
|
var window = Window.getInstance();
|
||||||
|
var deltaposX = (window.width / 2) - mouseX;
|
||||||
|
var deltaposY = (window.height / 2) - mouseY;
|
||||||
|
var rotX = deltaposX * 0.001 * CameraSensitivity * Math.PI * 2;
|
||||||
|
var rotY = deltaposY * 0.001 * CameraSensitivity * Math.PI * 2;
|
||||||
|
CameraYaw -= rotX;
|
||||||
|
CameraPitch += rotY;
|
||||||
|
// CameraYaw = Math.PI / 2;
|
||||||
|
// CameraPitch = Math.PI / 4;
|
||||||
|
|
||||||
|
if (CameraPitch > Math.PI)
|
||||||
|
CameraPitch = 3.141;
|
||||||
|
if (CameraPitch < 0)
|
||||||
|
CameraPitch = 0.001;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function update(dt:Float) {
|
||||||
|
var camera = level.scene.camera;
|
||||||
|
|
||||||
|
var x = CameraDistance * Math.sin(CameraPitch) * Math.cos(CameraYaw);
|
||||||
|
var y = CameraDistance * Math.sin(CameraPitch) * Math.sin(CameraYaw);
|
||||||
|
var z = CameraDistance * Math.cos(CameraPitch);
|
||||||
|
|
||||||
|
var targetpos = this.marble.getAbsPos().getPosition();
|
||||||
|
this.x = targetpos.x + x;
|
||||||
|
this.y = targetpos.y + y;
|
||||||
|
this.z = targetpos.z + z;
|
||||||
|
camera.follow = {pos: this, target: this.marble};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -17,11 +17,11 @@ import collision.CollisionWorld;
|
||||||
import h3d.col.ObjectCollider;
|
import h3d.col.ObjectCollider;
|
||||||
import h3d.col.Collider.GroupCollider;
|
import h3d.col.Collider.GroupCollider;
|
||||||
import h3d.Vector;
|
import h3d.Vector;
|
||||||
import h3d.scene.CameraController;
|
|
||||||
import h3d.mat.Material;
|
import h3d.mat.Material;
|
||||||
import h3d.scene.CustomObject;
|
import h3d.scene.CustomObject;
|
||||||
import h3d.prim.Sphere;
|
import h3d.prim.Sphere;
|
||||||
import h3d.scene.Object;
|
import h3d.scene.Object;
|
||||||
|
import src.CameraController;
|
||||||
|
|
||||||
class Move {
|
class Move {
|
||||||
public var d:Vector;
|
public var d:Vector;
|
||||||
|
|
@ -33,6 +33,7 @@ class Move {
|
||||||
|
|
||||||
class Marble extends Object {
|
class Marble extends Object {
|
||||||
public var camera:CameraController;
|
public var camera:CameraController;
|
||||||
|
public var cameraObject:Object;
|
||||||
public var controllable:Bool = false;
|
public var controllable:Bool = false;
|
||||||
|
|
||||||
public var collider:SphereCollisionEntity;
|
public var collider:SphereCollisionEntity;
|
||||||
|
|
@ -84,7 +85,7 @@ class Marble extends Object {
|
||||||
|
|
||||||
this.velocity = new Vector();
|
this.velocity = new Vector();
|
||||||
this.omega = new Vector();
|
this.omega = new Vector();
|
||||||
this.camera = new CameraController(20);
|
this.camera = new CameraController(cast this);
|
||||||
|
|
||||||
this.collider = new SphereCollisionEntity(cast this);
|
this.collider = new SphereCollisionEntity(cast this);
|
||||||
}
|
}
|
||||||
|
|
@ -102,9 +103,9 @@ class Marble extends Object {
|
||||||
function getMarbleAxis() {
|
function getMarbleAxis() {
|
||||||
var cammat = Matrix.I();
|
var cammat = Matrix.I();
|
||||||
var xrot = new Matrix();
|
var xrot = new Matrix();
|
||||||
xrot.initRotationX(this.camera.phi);
|
xrot.initRotationX(this.camera.CameraPitch);
|
||||||
var zrot = new Matrix();
|
var zrot = new Matrix();
|
||||||
zrot.initRotationZ(this.camera.theta);
|
zrot.initRotationZ(this.camera.CameraYaw);
|
||||||
cammat.multiply(xrot, zrot);
|
cammat.multiply(xrot, zrot);
|
||||||
var updir = gravityDir.multiply(-1);
|
var updir = gravityDir.multiply(-1);
|
||||||
var motiondir = new Vector(cammat._21, cammat._22, cammat._23);
|
var motiondir = new Vector(cammat._21, cammat._22, cammat._23);
|
||||||
|
|
@ -604,6 +605,10 @@ class Marble extends Object {
|
||||||
|
|
||||||
advancePhysics(currentTime, dt, move, collisionWorld, pathedInteriors);
|
advancePhysics(currentTime, dt, move, collisionWorld, pathedInteriors);
|
||||||
|
|
||||||
this.camera.target.load(this.getAbsPos().getPosition().toPoint());
|
if (this.controllable) {
|
||||||
|
this.camera.update(dt);
|
||||||
|
}
|
||||||
|
|
||||||
|
// this.camera.target.load(this.getAbsPos().getPosition().toPoint());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -75,6 +75,7 @@ class MarbleWorld {
|
||||||
this.marbles.push(marble);
|
this.marbles.push(marble);
|
||||||
marble.level = cast this;
|
marble.level = cast this;
|
||||||
if (marble.controllable) {
|
if (marble.controllable) {
|
||||||
|
marble.camera.init(cast this);
|
||||||
this.scene.addChild(marble.camera);
|
this.scene.addChild(marble.camera);
|
||||||
// Ugly hack
|
// Ugly hack
|
||||||
sky.follow = marble;
|
sky.follow = marble;
|
||||||
|
|
|
||||||
13
src/Sky.hx
13
src/Sky.hx
|
|
@ -1,5 +1,6 @@
|
||||||
package src;
|
package src;
|
||||||
|
|
||||||
|
import h3d.scene.pbr.Environment;
|
||||||
import src.Util;
|
import src.Util;
|
||||||
import src.MarbleWorld;
|
import src.MarbleWorld;
|
||||||
import hxd.BitmapData;
|
import hxd.BitmapData;
|
||||||
|
|
@ -19,13 +20,19 @@ class Sky extends Object {
|
||||||
|
|
||||||
public function init(level:MarbleWorld) {
|
public function init(level:MarbleWorld) {
|
||||||
var texture = createSkyboxCubeTextured(this.dmlPath);
|
var texture = createSkyboxCubeTextured(this.dmlPath);
|
||||||
var sky = new h3d.prim.Sphere(300, 128, 128);
|
var sky = new h3d.prim.Sphere(1, 128, 128);
|
||||||
sky.addNormals();
|
sky.addNormals();
|
||||||
|
sky.addUVs();
|
||||||
var skyMesh = new h3d.scene.Mesh(sky, this);
|
var skyMesh = new h3d.scene.Mesh(sky, this);
|
||||||
skyMesh.material.mainPass.culling = Front;
|
skyMesh.material.mainPass.culling = Front;
|
||||||
skyMesh.material.mainPass.addShader(new h3d.shader.CubeMap(texture));
|
skyMesh.material.mainPass.setPassName("overlay");
|
||||||
|
skyMesh.scale(200);
|
||||||
|
var env = new Environment(texture);
|
||||||
|
env.compute();
|
||||||
|
var renderer = cast(level.scene.renderer, h3d.scene.pbr.Renderer);
|
||||||
|
// renderer.env = env;
|
||||||
|
skyMesh.material.mainPass.addShader(new h3d.shader.pbr.CubeLod(texture));
|
||||||
skyMesh.material.shadows = false;
|
skyMesh.material.shadows = false;
|
||||||
skyMesh.culled = false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function createSkyboxCubeTextured(dmlPath:String) {
|
function createSkyboxCubeTextured(dmlPath:String) {
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue