Skip to content

Commit

Permalink
Adding Beginning of Physics and Physics example
Browse files Browse the repository at this point in the history
  • Loading branch information
Broch Stilley committed Apr 30, 2016
1 parent 82b5d45 commit b162e1d
Show file tree
Hide file tree
Showing 15 changed files with 20,654 additions and 1,010 deletions.
11,818 changes: 10,846 additions & 972 deletions akkad/build/bundle.js

Large diffs are not rendered by default.

9,600 changes: 9,600 additions & 0 deletions akkad/lib/Oimo.js

Large diffs are not rendered by default.

22 changes: 22 additions & 0 deletions akkad/src/actions/PhysicsActions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// import Immutable from "immutable";
import {hasEntity, getEntity} from '../classes/Helpers';
import Babylon from "babylonjs";
// Importing here to make a global var, as needed by Babylon.
import Oimo from '../../lib/Oimo'; //eslint-disable-line

const physicsActions = {
enablePhysicsOnScene(state, actions, sceneID, gravity) {
if (hasEntity(state(), sceneID)) {
const scene = getEntity(state(), sceneID);
window.BABYLON = Babylon;

scene.enablePhysics(new Babylon.Vector3(...gravity));

return state().updateIn(['entities', sceneID], s => s.set('physicsEnabled', true));
}

return state();
}
};

export default physicsActions;
3 changes: 2 additions & 1 deletion akkad/src/actions/ShapeActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@ const ShapeActions = {
const meshObj = Immutable.Map({
id: entityID,
entity: shape,
type: "mesh"
type: "mesh",
shape: type
});

return state().setIn(["entities", entityID], meshObj);
Expand Down
6 changes: 4 additions & 2 deletions akkad/src/actions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import animationActions from "./AnimationActions";
import triggerActions from "./TriggerActions";
import meshActions from "./MeshActions";
import particlesActions from "./ParticlesActions";
import physicsActions from './PhysicsActions';

export default {
...appActions,
Expand All @@ -19,5 +20,6 @@ export default {
...animationActions,
...triggerActions,
...meshActions,
...particlesActions
};
...particlesActions,
...physicsActions
};
4 changes: 3 additions & 1 deletion akkad/src/components/AbstractSystemComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class AbstractSystemComponent extends React.Component {
super(props, context);

this.id = Math.floor((1 + Math.random()) * 10000000000).toString(16);
this._propsChanged = true;
}

static abstractType = "AbstractSystemComponent";
Expand Down Expand Up @@ -60,7 +61,8 @@ class AbstractSystemComponent extends React.Component {
// break;
// }
// }
return !_.isEqual(nextProps, this.props);
this._propsChanged = !_.isEqual(nextProps, this.props);
return this._propsChanged;
}
/* eslint-enable */

Expand Down
25 changes: 25 additions & 0 deletions akkad/src/components/systems/Physics.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import {PropTypes} from "react";
import AbstractSystemComponent from "../AbstractSystemComponent";

class Physics extends AbstractSystemComponent {
static contextTypes = {
entityID: PropTypes.string,
appState: PropTypes.object,
actions: PropTypes.object
};

static propTypes = {
gravity: PropTypes.arrayOf(PropTypes.number).isRequired, // Vector3
targets: PropTypes.string
};

componentWillMount() {
const {entityID: sceneID, actions} = this.context;
const {gravity, targets} = this.props;

actions._internal.enablePhysicsOnScene(sceneID, gravity, targets);

}
}

export default Physics;
50 changes: 50 additions & 0 deletions akkad/src/components/systems/PhysicsState.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import {PropTypes} from "react";
import Immutable from 'immutable';
import {PhysicsEngine} from "babylonjs";
import AbstractSystemComponent from "../AbstractSystemComponent";
import {hasEntity} from "../../classes/Helpers";

const shapeImposters = {
box: (opts = {mass: 4, friction: 0.1, restitution: 0.999}) => [PhysicsEngine.BoxImpostor, opts],
sphere: (opts = {mass: 4}) => [PhysicsEngine.SphereImpostor, opts],
ground: (opts = {mass: 0, restitution: 0.001, friction: 0.1}) => [PhysicsEngine.BoxImpostor, opts]
};

class PhysicsState extends AbstractSystemComponent {
static contextTypes = {
entityID: PropTypes.string,
appState: PropTypes.object,
actions: PropTypes.object
};

static propTypes = {
mass: PropTypes.number,
friction: PropTypes.number,
restitution: PropTypes.number
};

setPhysicsState = (props, context) => {
const {entityID, appState} = context;

if (hasEntity(appState, entityID)) {
const entityObj = appState.getIn(['entities', entityID], Immutable.Map());
const shapeType = entityObj.get('shape', null);

if (shapeType && shapeImposters[shapeType]) {
entityObj.get('entity').setPhysicsState(...shapeImposters[shapeType](props));
}
}
}

componentDidMount() {
this.setPhysicsState(this.props, this.context);
}

componentWillUpdate(nextProps, nextState, nextContext) {
if (this.propsChanged(nextProps)){
this.setPhysicsState(nextProps, nextContext);
}
}
}

export default PhysicsState;
8 changes: 7 additions & 1 deletion akkad/src/components/systems/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import importGravity from "./Gravity";
import importApplyGravity from "./ApplyGravity";
import importCheckCollisions from "./CheckCollisions";
import importCollisionsEnabled from "./CollisionsEnabled";
import importPhysics from './Physics';
import importPhysicsState from './PhysicsState';

// Components mostly used internally
import importGenericProperty from "./GenericProperty";
Expand Down Expand Up @@ -47,7 +49,9 @@ export default {
CollisionsEnabled: importCollisionsEnabled,
GenericProperty: importGenericProperty,
SetEntityAsProperty: importSetEntityAsProperty,
CallMethodOnEntity: importCallMethodOnEntity
CallMethodOnEntity: importCallMethodOnEntity,
Physics: importPhysics,
PhysicsState: importPhysicsState
};

export const RenderShape = importRenderShape;
Expand All @@ -72,3 +76,5 @@ export const CollisionsEnable = importCollisionsEnabled;
export const GenericProperty = importGenericProperty;
export const SetEntityAsProperty = importSetEntityAsProperty;
export const CallMethodOnEntity = importCallMethodOnEntity;
export const Physics = importPhysics;
export const PhysicsState = importPhysicsState;
4 changes: 3 additions & 1 deletion example/src/Routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ const {
TriggersPage,
ClickTheBox,
HeightMapPage,
WebVRPage
WebVRPage,
PhysicsPage
} = pages;

const Routes = (
Expand All @@ -25,6 +26,7 @@ const Routes = (
<Route path="clickthebox" component={ClickTheBox} />
<Route path="heightmap" component={HeightMapPage} />
<Route path="webvr" component={WebVRPage} />
<Route path="physics" component={PhysicsPage} />
</Route>
</Router>
);
Expand Down
33 changes: 3 additions & 30 deletions example/src/pages/Landing.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,6 @@
import React, {Component, PropTypes} from 'react';
import {Link} from "react-router";
import {Akkad} from "akkad";
import triggerActions from "../actions/triggerActions";
import Scene1 from "../scenes/Scene1";

class TestComponent extends Component {
constructor() {
super();
this.id = Math.random() * 100000;
}
componentWillMount() {
console.log("componentWillMount", this.id, this.props.myID);
}

componentDidMount() {
console.log('componentDidMount', this.id, this.props.myID);
}

render() {
return (
<div>
{this.props.children}
</div>
)
}
};

class Landing extends Component {
render() {
Expand Down Expand Up @@ -59,14 +35,11 @@ class Landing extends Component {
<li>
<Link to="/webvr">Web VR</Link>
</li>
<li>
<Link to="/physics">physics</Link>
</li>
</ul>
{children}
{/*<TestComponent myID="1">
<TestComponent myID="2">
<TestComponent myID="3"/>
</TestComponent>
</TestComponent>
<TestComponent myID="4"/>*/}
</div>
);
}
Expand Down
23 changes: 23 additions & 0 deletions example/src/pages/PhysicsPage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import React, {Component, PropTypes} from 'react';
import {Akkad, Scene, shapes, cameras, lights, systems} from "akkad";
import PhysicsScene from '../scenes/PhysicsScene';
const {ArcRotateCamera} = cameras;
const {HemisphericLight} = lights;
const {Mesh, Position, Rotate} = systems;

class PhysicsPage extends Component {
render() {
return (
<div>
<h2>
Physics Example
</h2>
<Akkad>
<PhysicsScene />
</Akkad>
</div>
);
}
}

export default PhysicsPage;
4 changes: 3 additions & 1 deletion example/src/pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import TriggersPage from "./TriggersPage";
import ClickTheBox from "./ClickTheBox";
import HeightMapPage from "./HeightMapPage";
import WebVRPage from "./WebVRPage";
import PhysicsPage from './PhysicsPage';

export default {
Landing,
Expand All @@ -17,5 +18,6 @@ export default {
TriggersPage,
ClickTheBox,
HeightMapPage,
WebVRPage
WebVRPage,
PhysicsPage
};
63 changes: 63 additions & 0 deletions example/src/scenes/PhysicsScene.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import React, {Component, PropTypes} from 'react';
import Immutable, {fromJS} from 'immutable';
import {Akkad, Scene, Material, cameras, lights, shapes, systems} from 'akkad';

const {ArcRotateCamera} = cameras;
const {HemisphericLight} = lights;
const {Box, Ground} = shapes;
const {
Position,
Rotate,
Physics,
Color,
CheckCollisions,
CollisionsEnabled,
PhysicsState
} = systems;

class PhysicsScene extends Component {
static contextTypes = {
actions: PropTypes.object,
appState: PropTypes.object
}

render() {
return (
<Scene>
<CollisionsEnabled />
<Physics gravity={[0, -10, 0]} />
<ArcRotateCamera
initialPosition={[-11, 6, -13]}
target={[0, 1, 0]}
/>
<HemisphericLight />
<Box>
<CheckCollisions />
<Position vector={[0, 5, 0]} />
<Rotate
axis={[1, 1.7, 0]}
amount={60}
space="LOCAL"
/>
<Material>
<Color color={[0.2, 0.5, 0.9]} />
</Material>
<PhysicsState mass={3} />
</Box>
<Ground
height={300}
width={300}
>
<CheckCollisions />
<Position vector={[0, -10, 0]} />
<Material>
<Color color={[0, 1, 1]} />
</Material>
<PhysicsState mass={0} />
</Ground>
</Scene>
);
}
}

export default PhysicsScene;
1 change: 0 additions & 1 deletion example/src/scenes/TriggersScene.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ class TriggersScene extends Component {

render() {
const {testKeyDown} = this.context.actions;
console.log(this.context.appState.toJS());
return (
<div>
<div>
Expand Down

0 comments on commit b162e1d

Please sign in to comment.