Hi there,
I am creating a ball game, where a ball follows a way. The way is a AWPBvhTriangleMeshShape. But unfortunately, the ball jumps up and changes its direction when it is rolling over edges - even if the surface is flat.
I have created a project to reproduce this behaviour. There is a PlaneGeometry and a SphereGeometry. When moving the ball, it starts to jump at the edges of the plane surface.
I have uploaded a movie of the project, where you can see the ball jumps up: http://de.tinypic.com/r/2ibymh5/5
Is this a bug or is there a solution to get rid of this problem? I really need the AWPBvhTriangleMeshShape, as the way is not a straight one.
Here is the code, woul’d be very happy is someone coul’d help me out.
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.geom.Vector3D;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.ui.Keyboard;
import away3d.containers.View3D;
import away3d.debug.AwayStats;
import away3d.entities.Mesh;
import away3d.lights.PointLight;
import away3d.materials.ColorMaterial;
import away3d.materials.lightpickers.StaticLightPicker;
import away3d.primitives.PlaneGeometry;
import away3d.primitives.SphereGeometry;
import awayphysics.collision.shapes.AWPBvhTriangleMeshShape;
import awayphysics.collision.shapes.AWPSphereShape;
import awayphysics.debug.AWPDebugDraw;
import awayphysics.dynamics.AWPDynamicsWorld;
import awayphysics.dynamics.AWPRigidBody;
[SWF(backgroundColor="#000000", frameRate="60", width="600", height="400")]
public class BvhTriangleMesh extends Sprite
{
private var _view : View3D;
private var _light : PointLight;
private var _lightPicker:StaticLightPicker;
private var _physicsWorld : AWPDynamicsWorld;
private var _debugDraw:AWPDebugDraw;
private var _timeStep : Number = 1.0 / 60;
private var _bodySphere:AWPRigidBody;
private var _text:TextField;
private var _textFormat:TextFormat;
public function BvhTriangleMesh()
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init():void{
removeEventListener(Event.ADDED_TO_STAGE, init);
_view = new View3D();
this.addChild(_view);
this.addChild(new AwayStats(_view));
_light = new PointLight();
_light.y = 5000;
_view.scene.addChild(_light);
_lightPicker = new StaticLightPicker([_light]);
_view.camera.lens.far = 20000;
_view.camera.y = 2000;
_view.camera.z = -2000;
_view.camera.rotationX = 40;
// init the physics world
_physicsWorld = AWPDynamicsWorld.getInstance();
_physicsWorld.initWithDbvtBroadphase();
_debugDraw = new AWPDebugDraw(_view, _physicsWorld);
_debugDraw.debugMode = AWPDebugDraw.DBG_NoDebug;
_debugDraw.debugMode = AWPDebugDraw.DBG_DrawCollisionShapes;
addObjects();
stage.addEventListener(Event.ENTER_FRAME, onEnterFrame);
stage.addEventListener(KeyboardEvent.KEY_DOWN, onDownHandler);
}
private function addObjects():void{
var material : ColorMaterial = new ColorMaterial(0xfa6c16);
material.lightPicker = _lightPicker;
var mesh : Mesh;
//Create plane:
var iSeg:uint = 3;
mesh = new Mesh(new PlaneGeometry(20000, 20000, iSeg, iSeg), material);
_view.scene.addChild(mesh);
var triangleShape : AWPBvhTriangleMeshShape = new AWPBvhTriangleMeshShape(mesh.geometry);
var planeBody:AWPRigidBody = new AWPRigidBody(triangleShape, mesh, 0);
planeBody.position = new Vector3D(0, 0, 0);
planeBody.restitution = 0.5;
_physicsWorld.addRigidBody(planeBody);
//Creat shpere:
var sphereShape:AWPSphereShape = new AWPSphereShape(100);
mesh = new Mesh(new SphereGeometry(100), material);
_view.scene.addChild(mesh);
_bodySphere = new AWPRigidBody(sphereShape, mesh, 1);
_bodySphere.friction = .9;
_bodySphere.restitution = 0.9;
_bodySphere.position = new Vector3D(-1000, 600, 0);
_physicsWorld.addRigidBody(_bodySphere);
_textFormat = new TextFormat('Arial', 20, 0xffffff, true);
_text = new TextField();
_text.x = this.width/2;
_text.y = 20;
_text.width = 400;
this.addChild(_text);
}
private function onEnterFrame(e : Event):void{
_physicsWorld.step(_timeStep);
_debugDraw.debugDrawWorld();
_view.render();
_view.camera.position = _bodySphere.position.add(new Vector3D(0, 2000, -2500));
_view.camera.lookAt(_bodySphere.position);
_text.text = "ball.y: " + _bodySphere.position.y.toFixed(3);
_text.setTextFormat(_textFormat);
}
private function onDownHandler(event : KeyboardEvent):void{
var fForce:Number = 1;
if(event.keyCode==Keyboard.UP) _bodySphere.applyCentralImpulse(new Vector3D(0,0,fForce));
if(event.keyCode==Keyboard.DOWN) _bodySphere.applyCentralImpulse(new Vector3D(0,0,-fForce));
if(event.keyCode==Keyboard.LEFT) _bodySphere.applyCentralImpulse(new Vector3D(-fForce,0,0));
if(event.keyCode==Keyboard.RIGHT) _bodySphere.applyCentralImpulse(new Vector3D(fForce,0,0));
}
}
}