Gravity problem

Software: Away3D 4.x

wajerrr, Newbie
Posted: 28 November 2011 11:39 AM   Total Posts: 5

Hi guys,
I came across something strange
please find example
below http://www.wajerrr.com/physicsTest/Main.html
in the nutshell:
I make a floor made of cube (statics)

var material : ColorMaterial = new ColorMaterial(0xdddddd);
 
 
var ground : Cube = new Cube(material,BOARD_HEIGHT,SIDE_WALL_DEPTH,BOARD_WIDTH);
  var groundShape :AWPBoxShape = new AWPBoxShape(BOARD_HEIGHT,SIDE_WALL_DEPTH,BOARD_WIDTH);
  groundRigidbody = new AWPRigidBody(groundShape, ground, 0);
  groundRigidbody.position= new Vector3D(BOARD_HEIGHT/2,-SIDE_WALL_DEPTH/2,BOARD_WIDTH/2) 
  groundRigidbody.friction=.2
  groundRigidbody.restitution=0
  groundRigidbody.collisionFlags= AWPCollisionFlags.CF_STATIC_OBJECT
  _physicsWorld.addRigidBody(groundRigidbody);
  _view.scene.addChild(ground);

and a ball made of sphere

var _sphereShape:AWPSphereShape = new AWPSphereShape(BALL_RADIUS);
  var material : ColorMaterial = new ColorMaterial(0xeedd00);
  var sphere : Sphere = new Sphere(material, BALL_RADIUS);
 
  var _ballbody:AWPRigidBody = new AWPRigidBody(_sphereShape, sphere, 11.1);
  _ballbody.position = new Vector3D(BOARD_WIDTH/2,BALL_RADIUS+11,BOARD_HEIGHT/2+30)
 
  _view.scene.addChild(sphere);
  _physicsWorld.addRigidBody(_ballbody);


as gravity is set to standard <0,-10,0> I thought the ball should stay still
(at least that’s what I would like it to do grin)
however it is moving towards one of the edges. I am not sure If I am missing something oblivious…

many thanks
Przemek

 

   

Yang Li, Administrator
Posted: 28 November 2011 02:39 PM   Total Posts: 80   [ # 1 ]

can you paste all code?

   

wajerrr, Newbie
Posted: 28 November 2011 02:41 PM   Total Posts: 5   [ # 2 ]

Thanks a lot,
please find the code below

package com.view.game
{
import away3d.containers.ObjectContainer3D;
import away3d.containers.View3D;
import away3d.debug.AwayStats;
import away3d.entities.SegmentSet;
import away3d.lights.PointLight;
import away3d.loaders.parsers.Parsers;
import away3d.materials.ColorMaterial;
import away3d.primitives.Cube;
import away3d.primitives.LineSegment;
import away3d.primitives.Plane;
import away3d.primitives.Sphere;

import awayphysics.collision.shapes.AWPBoxShape;
import awayphysics.collision.shapes.AWPSphereShape;
import awayphysics.collision.shapes.AWPStaticPlaneShape;
import awayphysics.data.AWPCollisionFlags;
import awayphysics.dynamics.AWPDynamicsWorld;
import awayphysics.dynamics.AWPRigidBody;
import awayphysics.events.AWPEvent;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.geom.Vector3D;

public class GameView extends Sprite
{
  private static const BOARD_WIDTH:Number=180
  private static const BOARD_HEIGHT:Number=150
  private static const BOARD_DEPTH:Number=10
  private static const SIDE_WALL_DEPTH:Number=2;
 
  private static const BALL_RADIUS:int=6;
  private static const BALL_FRICTION:Number=0.4;
  private static const BALL_RESTITUTION:Number=0.8; 
 
  private var _timeStep : Number = 1.0 / 60;
 
  private var _viewPortWidth:int;
  private var _viewPortHeight:int;
 
  private var _view:View3D;
 
  private var _physicsWorld : AWPDynamicsWorld;
  private var groundRigidbody:AWPRigidBody;
 
 
 
 
  public function GameView(viewPortWidth:int,viewPortHeight:int)
  {
  super();
  _viewPortWidth=viewPortWidth
  _viewPortHeight=viewPortHeight
  initGame();
  }
 
  private function initGame():void
  {
  _view = new View3D();
  _view.backgroundColor = 0x666666;
  _view.antiAlias = 4;
  _view.width=_viewPortWidth
  _view.height=_viewPortHeight
  this.addChild(_view);

  Parsers.enableAllBundled();
 
  _physicsWorld = AWPDynamicsWorld.getInstance();
  _physicsWorld.initWithDbvtBroadphase();
  _physicsWorld.collisionCallbackOn=true
  _physicsWorld.gravity= new Vector3D(0,-10,0)
 
  debug_info();
  createFloor();
  createBall();
  setCamera()
  this.addEventListener(Event.ENTER_FRAME, onEnterFrame);
  }
 
 
 
  private function debug_info():void
  {
  var sgm:SegmentSet=new SegmentSet();
  _view.scene.addChild(sgm);
  sgm.addSegment(new LineSegment(new Vector3D(0, 0, 0), new Vector3D(1000, 0, 0),0xff0000,0xFF0000,3));
  sgm.addSegment(new LineSegment(new Vector3D(0, 0, 0), new Vector3D(0, 1000, 0),0x00ff00,0x00FF00,3));
  sgm.addSegment(new LineSegment(new Vector3D(0, 0, 0), new Vector3D(0, 0, 1000),0x0000ff,0x0000ff,3));
  var avS:AwayStats= new AwayStats(_view);
  addChild(avS)
  }
 
  private function createFloor():void
  {
  var material : ColorMaterial = new ColorMaterial(0xdddddd);
  var __material : ColorMaterial = new ColorMaterial(0x00dddd);
 
  var ground : Cube = new Cube(material,BOARD_HEIGHT,SIDE_WALL_DEPTH,BOARD_WIDTH);
  var groundShape :AWPBoxShape = new AWPBoxShape(BOARD_HEIGHT,SIDE_WALL_DEPTH,BOARD_WIDTH);
  groundRigidbody = new AWPRigidBody(groundShape, ground, 0);
  groundRigidbody.position= new Vector3D(BOARD_HEIGHT/2,-SIDE_WALL_DEPTH/2,BOARD_WIDTH/2) 
  groundRigidbody.friction=.2
  groundRigidbody.restitution=0
  groundRigidbody.collisionFlags= AWPCollisionFlags.CF_STATIC_OBJECT
  _physicsWorld.addRigidBody(groundRigidbody);
  _view.scene.addChild(ground); 
  }
 
  private function createBall():void
  {
  var _sphereShape:AWPSphereShape = new AWPSphereShape(BALL_RADIUS);
  var material : ColorMaterial = new ColorMaterial(0xeedd00);
  var sphere : Sphere = new Sphere(material, BALL_RADIUS);
  //sphere.showBounds=true
  var _ballbody:AWPRigidBody = new AWPRigidBody(_sphereShape, sphere, 11.1);
  _ballbody.position = new Vector3D(BOARD_WIDTH/2,BALL_RADIUS+11,BOARD_HEIGHT/2+30)
  //_ballbody.friction=BALL_FRICTION
  //_ballbody.restitution=BALL_RESTITUTION
  //_ballbody.contactProcessingThreshold = 0.1;
  //_ballbody.linearSleepingThreshold=0.1
  _view.scene.addChild(sphere);
  _physicsWorld.addRigidBody(_ballbody);
 
  }
 
  private function setCamera():void
  {
  _view.camera.z=BOARD_WIDTH+BOARD_WIDTH*0.35
  _view.camera.x=BOARD_HEIGHT/2
  _view.camera.y=BOARD_WIDTH*0.6
  _view.camera.lookAt(new Vector3D(75,0,90));
  }
 
 
 
  protected function onEnterFrame(event:Event):void
  {
  _physicsWorld.step(_timeStep, 1, _timeStep);
  _view.render();
  }
}
}

many thanks
Przemek

   

Yang Li, Administrator
Posted: 28 November 2011 03:18 PM   Total Posts: 80   [ # 3 ]

the floor cube is too thin, get inaccurate simulation, try to set SIDE_WALL_DEPTH to 10 or more.

   

wajerrr, Newbie
Posted: 28 November 2011 03:51 PM   Total Posts: 5   [ # 4 ]

Thank you a lot , it works now. Really appreciate your help. AwayPhysics is really really great piece of work.

thanks a lot
Przemek

   
   
‹‹ cloth and hair dynamics

X

Away3D Forum

Member Login

Username

Password

Remember_me



X