This repository has been archived by the owner on May 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 137
Shape Component #90
Merged
Merged
Shape Component #90
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
790d5a3
Add a new shape "compound". Requires specification of 1 or more "comp…
InfiniteLee f66ef26
Added `shape` components.
InfiniteLee fec4fda
eof newline
InfiniteLee c8add50
don't call this._pause() when this.shouldUpdateBody
InfiniteLee da2908d
remove *-shape helper components
InfiniteLee ee9ff27
cleanup
InfiniteLee 28819b8
update default 'numSegments' to match three.js'
InfiniteLee a4a3776
some fixes/cleanup from review
InfiniteLee 5a12e10
can't do clone() on a cannonjs quaternion
InfiniteLee be3e43f
move the creation of the shape object into the shape component. Handl…
InfiniteLee File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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,105 @@ | ||
var CANNON = require('cannon'); | ||
|
||
var Shape = { | ||
schema: { | ||
shape: {default: 'box', oneOf: ['box', 'sphere', 'cylinder']}, | ||
radius: {type: 'number', default: 1, if: {shape: ['sphere']}}, | ||
offset: {type: 'vec3', default: {x: 0, y: 0, z: 0}}, | ||
halfExtents: {type: 'vec3', default: {x: 1, y: 1, z: 1}, if: {shape: ['box']}}, | ||
orientation: {type: 'vec4', default: {x: 0, y: 0, z: 0, w: 1}, if: {shape: ['box', 'cylinder']}}, | ||
radiusTop: {type: 'number', default: 1, if: {shape: ['cylinder']}}, | ||
radiusBottom: {type: 'number', default: 1, if: {shape: ['cylinder']}}, | ||
height: {type: 'number', default: 1, if: {shape: ['cylinder']}}, | ||
numSegments: {type: 'int', default: 8, if: {shape: ['cylinder']}} | ||
}, | ||
|
||
multiple: true, | ||
|
||
init: function() { | ||
if (this.el.sceneEl.hasLoaded) { | ||
this.initShape(); | ||
} else { | ||
this.el.sceneEl.addEventListener('loaded', this.initShape.bind(this)); | ||
} | ||
}, | ||
|
||
initShape: function() { | ||
this.bodyEl = this.el; | ||
var bodyType = this._findType(this.bodyEl); | ||
var data = this.data; | ||
|
||
while (!bodyType && this.bodyEl.parentNode) { | ||
this.bodyEl = this.bodyEl.parentNode; | ||
bodyType = this._findType(this.bodyEl); | ||
} | ||
|
||
var scale = new THREE.Vector3(); | ||
this.bodyEl.object3D.getWorldScale(scale); | ||
var shape, offset, orientation; | ||
|
||
if (data.hasOwnProperty('offset')) { | ||
offset = new CANNON.Vec3( | ||
data.offset.x * scale.x, | ||
data.offset.y * scale.y, | ||
data.offset.z * scale.z | ||
); | ||
} | ||
|
||
if (data.hasOwnProperty('orientation')) { | ||
orientation = new CANNON.Quaternion(); | ||
orientation.copy(data.orientation); | ||
} | ||
|
||
switch(data.shape) { | ||
case 'sphere': | ||
shape = new CANNON.Sphere(data.radius * scale.x); | ||
break; | ||
case 'box': | ||
var halfExtents = new CANNON.Vec3( | ||
data.halfExtents.x * scale.x, | ||
data.halfExtents.y * scale.y, | ||
data.halfExtents.z * scale.z | ||
); | ||
shape = new CANNON.Box(halfExtents); | ||
break; | ||
case 'cylinder': | ||
shape = new CANNON.Cylinder( | ||
data.radiusTop * scale.x, | ||
data.radiusBottom * scale.x, | ||
data.height * scale.y, | ||
data.numSegments | ||
); | ||
|
||
//rotate by 90 degrees similar to mesh2shape:createCylinderShape | ||
var quat = new CANNON.Quaternion(); | ||
quat.setFromEuler(-90 * THREE.Math.DEG2RAD, 0, 0, 'XYZ').normalize(); | ||
orientation.mult(quat, orientation); | ||
break; | ||
default: | ||
console.warn(data.shape + ' shape not supported'); | ||
return; | ||
} | ||
|
||
this.bodyEl.components[bodyType].addShape(shape, offset, orientation); | ||
}, | ||
|
||
_findType: function(el) { | ||
if (el.hasAttribute('body')) { | ||
return 'body'; | ||
} else if (el.hasAttribute('dynamic-body')) { | ||
return 'dynamic-body'; | ||
} else if (el.hasAttribute('static-body')) { | ||
return'static-body'; | ||
} | ||
return null; | ||
}, | ||
|
||
remove: function() { | ||
if (this.bodyEl.parentNode) { | ||
console.warn('removing shape component not currently supported'); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a case where There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If the parent is removed first we don't need to warn about the shape being removed. |
||
} | ||
}; | ||
|
||
module.exports.definition = Shape; | ||
module.exports.Component = AFRAME.registerComponent('shape', Shape); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a reference to a quaternion still in
body.shapeOrientations
, so I think we want to make a copy before inverting.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
will add .clone() where
orientation
is set.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh nevermind that's a threejs method, not a cannonjs one.