-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding Beginning of Physics and Physics example
- Loading branch information
Broch Stilley
committed
Apr 30, 2016
1 parent
82b5d45
commit b162e1d
Showing
15 changed files
with
20,654 additions
and
1,010 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters