-
-
Save JohnBrookes/f12de24f2e6d907fe436 to your computer and use it in GitHub Desktop.
Physics collision issue.
Now fixed.
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
package | |
{ | |
import away3d.cameras.Camera3D; | |
import away3d.containers.ObjectContainer3D; | |
import away3d.containers.Scene3D; | |
import away3d.containers.View3D; | |
import away3d.controllers.HoverController; | |
import away3d.debug.AwayStats; | |
import away3d.entities.Mesh; | |
import away3d.library.AssetLibrary; | |
import away3d.lights.DirectionalLight; | |
import away3d.materials.ColorMaterial; | |
import away3d.materials.lightpickers.StaticLightPicker; | |
import away3d.primitives.CubeGeometry; | |
import away3d.primitives.PlaneGeometry; | |
import away3d.primitives.SphereGeometry; | |
import awayphysics.collision.dispatch.AWPCollisionObject; | |
import awayphysics.collision.shapes.AWPBoxShape; | |
import awayphysics.collision.shapes.AWPSphereShape; | |
import awayphysics.collision.shapes.AWPStaticPlaneShape; | |
import awayphysics.debug.AWPDebugDraw; | |
import awayphysics.dynamics.AWPDynamicsWorld; | |
import awayphysics.dynamics.AWPRigidBody; | |
import awayphysics.events.AWPEvent; | |
import flash.display.Sprite; | |
import flash.display.StageAlign; | |
import flash.display.StageScaleMode; | |
import flash.events.Event; | |
import flash.events.MouseEvent; | |
import flash.geom.Vector3D; | |
import flash.text.TextField; | |
import flash.text.TextFormat; | |
import flash.utils.getTimer; | |
public class AW4PhysDropTest2 extends Sprite | |
{ | |
private var view:View3D; | |
private var scene:Scene3D; | |
private var camera:Camera3D; | |
//lights | |
private var slp:StaticLightPicker; | |
private var dlight:DirectionalLight; | |
// | |
private var physics:AWPDynamicsWorld; | |
private var camController:HoverController; | |
private var ballRb:AWPRigidBody; | |
private var tf:TextField; | |
private var dropHeight:Number = 2000; | |
private var ballSize:Number = 50; | |
private var count:uint; | |
public function AW4PhysDropTest2() | |
{ | |
if (stage) init(); | |
else addEventListener(Event.ADDED_TO_STAGE, init); | |
} | |
private function init(e:Event = null):void | |
{ | |
removeEventListener(Event.ADDED_TO_STAGE, init); | |
// entry point | |
stage.scaleMode = StageScaleMode.NO_SCALE; | |
stage.align = StageAlign.TOP_LEFT; | |
init3D(); | |
initLights(); | |
initPhysics(); | |
initObjects(); | |
initListeners(); | |
tf = new TextField(); | |
tf.x = 20; | |
tf.width = stage.stageWidth; | |
tf.y = stage.stageHeight -40; | |
var tfm:TextFormat = new TextFormat("Verdana", 32, 0x0); | |
tf.defaultTextFormat = tfm; | |
tf.text = "DOUBLE CLICK TO START - count the bounces"; | |
stage.addChild(tf); | |
} | |
private function init3D():void | |
{ | |
view = new View3D(); | |
view.backgroundColor = 0x0; | |
view.antiAlias = 8; | |
scene = view.scene; | |
camera = view.camera; | |
addChild(view); | |
camera.y = 750; | |
camera.lens.far = 18000; | |
addChild(new AwayStats(view)); | |
} | |
private function initLights():void | |
{ | |
dlight = new DirectionalLight(); | |
scene.addChild(dlight); | |
slp = new StaticLightPicker([dlight]); | |
} | |
private function initPhysics():void | |
{ | |
physics = AWPDynamicsWorld.getInstance(); | |
physics.initWithDbvtBroadphase(); | |
physics.collisionCallbackOn = true; | |
} | |
private function initObjects():void | |
{ | |
//var groundshape:AWPStaticPlaneShape = new AWPStaticPlaneShape(); | |
var groundshape:AWPBoxShape = new AWPBoxShape(10000,10,10000); | |
var groundCO:AWPCollisionObject = new AWPCollisionObject(groundshape, null); | |
groundCO.restitution = 0.8; | |
physics.addCollisionObject(groundCO); | |
var ground:Mesh = new Mesh(new PlaneGeometry(10000, 10000), new ColorMaterial()); | |
scene.addChild(ground); | |
var sphereGeom:SphereGeometry = new SphereGeometry(ballSize); | |
var ballMat:ColorMaterial = new ColorMaterial(0xff0000); | |
ballMat.lightPicker = slp; | |
var ballMesh:Mesh = new Mesh(sphereGeom, ballMat); | |
scene.addChild(ballMesh); | |
var ballShape:AWPSphereShape = new AWPSphereShape(sphereGeom.radius); | |
ballRb = new AWPRigidBody(ballShape, ballMesh, 0); | |
ballRb.restitution = 0.97; | |
physics.addRigidBody(ballRb); | |
var ruler:Mesh = new Mesh(new CubeGeometry(100, dropHeight, 10), new ColorMaterial(0x333333) ) | |
ruler.z = ballSize / 2; | |
ruler.y = dropHeight / 2; | |
scene.addChild(ruler); | |
view.camera.lookAt(new Vector3D(0, sphereGeom.radius, 0)); | |
} | |
private function dropBall(e:MouseEvent):void | |
{ | |
ballRb.addEventListener(AWPEvent.COLLISION_ADDED, sphereCollisionAdded); | |
ballRb.position = new Vector3D(0, dropHeight+ballSize, 0); | |
ballRb.mass = 1; | |
} | |
private function sphereCollisionAdded(e:AWPEvent):void | |
{ | |
tf.text = String("Collision " + ++count); | |
//stop demo | |
if (count > 10) | |
{ | |
ballRb.removeEventListener(AWPEvent.COLLISION_ADDED, sphereCollisionAdded); | |
tf.text = String("Double click to restart"); | |
count = 0; | |
} | |
} | |
private function handleEnterFrame(e:Event):void | |
{ | |
//misses bounce collision | |
physics.step(view.deltaTime/1000, 6, 1.0/240); | |
//physics.step(view.deltaTime/1000, 6, 1.0/60); | |
view.render(); | |
} | |
private function onStageResize(event : Event) : void | |
{ | |
view.width = stage.stageWidth; | |
view.height = stage.stageHeight; | |
} | |
private function initListeners():void | |
{ | |
addEventListener(Event.ENTER_FRAME, handleEnterFrame); | |
stage.addEventListener(Event.RESIZE, onStageResize); | |
stage.addEventListener(MouseEvent.DOUBLE_CLICK, dropBall); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment