3D view and 2D physics engine

Software: Away3D 4.x

Aymeric, Newbie
Posted: 19 September 2012 12:12 PM   Total Posts: 9

Some awesome games use a 2D physics engine to handle collision but display a 3D View. I’ve worked since a long time on a 2D engine, in which I would like to add this 3D View. However I’m not as familiar with 3D as I should. I know that we use the project / unproject to “exchange” coordinnates between 2D and 3D space, but I’m a bit lost.

So far I’ve been able to display Box2D debug view and my 3D objects at the same position, but if I add a camera to follow my object, I can’t have my 3D objects position identical to my Box2D debug view objects.

Here is my game state :

package {

 import com
.citrusengine.objects.platformer.box2d.Coin;
 
import away3d.debug.AwayStats;
 
import away3d.entities.Mesh;
 
import away3d.materials.ColorMaterial;
 
import away3d.primitives.CubeGeometry;

 
import com.citrusengine.core.State;
 
import com.citrusengine.math.MathVector;
 
import com.citrusengine.objects.platformer.box2d.Hero;
 
import com.citrusengine.objects.platformer.box2d.Platform;
 
import com.citrusengine.physics.Box2D;
 
import com.citrusengine.view.CitrusView;
 
import com.citrusengine.view.away3dview.Away3DView;

 
import flash.geom.Rectangle;

 public class 
Away3DGameState extends State {

  
public function Away3DGameState() {
   super
();
  
}
   
  override 
public function initialize():void {
   
   super
.initialize();
   
   
addChild(new AwayStats((view as Away3DView).viewRoot));
   
   var 
cube1:Mesh = new Mesh(new CubeGeometry(1001000), new ColorMaterial(0x0000FF));
   var 
cube2:Mesh = new Mesh(new CubeGeometry(30300), new ColorMaterial(0xFFFF00));
   var 
cube3:Mesh = new Mesh(new CubeGeometry(3000100), new ColorMaterial(0xFFFFFF));
   
   var 
box2D:Box2D = new Box2D("box2D");
   
box2D.visible true;
   
add(box2D);
   
   var 
platform:Platform = new Platform("platB"{x:stage.stageWidth 2y:stage.stageHeight 30width:3000height:10view:cube3});
   
add(platform);
   
   var 
hero:Hero = new Hero("hero"{x:150y:50width:100height:100view:cube1});
   
add(hero);
   
   var 
coin:Coin = new Coin("coin"{x:300y:150width:30height:30view:cube2});
   
add(coin);
   
   
view.setupCamera(hero, new MathVector(320240), new Rectangle(001550450), new MathVector(.25.05));
  
}
  
  
// Make sure and call this override to specify Away3D view.
  
override protected function createView():CitrusView {
   
   
return new Away3DView(this);
  
}
 }

My Away3D view :

package com.citrusengine.view.away3dview {

 import away3d
.cameras.Camera3D;
 
import away3d.cameras.lenses.OrthographicLens;
 
import away3d.containers.ObjectContainer3D;
 
import away3d.containers.Scene3D;
 
import away3d.containers.View3D;
 
import away3d.controllers.HoverController;

 
import com.citrusengine.view.CitrusView;
 
import com.citrusengine.view.ISpriteView;
 
import com.citrusengine.view.spriteview.Box2DDebugArt;

 
import flash.display.MovieClip;
 
import flash.display.Sprite;

 public class 
Away3DView extends CitrusView {

  
private var _viewRoot:View3D;
  private var 
_scene:Scene3D;
  private var 
_camera:Camera3D;
  private var 
_cameraController:HoverController;
  
  private var 
_container:ObjectContainer3D;

  public function 
Away3DView(root:Sprite{
   
   super
(rootISpriteView);
   
   
_scene = new Scene3D();
   
_container = new ObjectContainer3D();
   
_scene.addChild(_container);
   
   
_camera = new Camera3D();
   
_camera.lens = new OrthographicLens();

   
_viewRoot = new View3D(_scene_camera);
   
_viewRoot.antiAlias 4;
   
root.addChild(_viewRoot);
   
   
//_cameraController = new HoverController(_camera, null, 180, 0);
  
}

  
public function get viewRoot():View3D {
   
return _viewRoot;
  
}
  
  
public function get container():ObjectContainer3D {
   
return _container;
  
}

  
/**
   * @inherit 
   */
  
override public function update():void {
   
   super
.update();
   
   
_viewRoot.render();
   
   
//_container.rotationY++;
   //_container.rotationZ++;
   
   //_camera.lookAt(_container.position);
   
   // Update Camera
   
if (cameraTarget{
    
var diffX:Number = (-cameraTarget.cameraOffset.x) - _container.position.x;
    var 
diffY:Number = (-cameraTarget.cameraOffset.y) - _container.position.y;
    var 
velocityX:Number diffX cameraEasing.x;
    var 
velocityY:Number diffY cameraEasing.y;
    
    
_container.+= velocityX;
    
_container.+= velocityY;
   
    
// Constrain to camera bounds
    
if (cameraBounds{
     
     
if (-_container.<= cameraBounds.left || cameraBounds.width cameraLensWidth)
      
_container.= -cameraBounds.left;
     else if (-
_container.cameraLensWidth >= cameraBounds.right)
      
_container.= -cameraBounds.right cameraLensWidth;
   
     if (-
_container.<= cameraBounds.top || cameraBounds.height cameraLensHeight)
      
_container.= -cameraBounds.top;
     else if (-
_container.cameraLensHeight >= cameraBounds.bottom)
      
_container.= -cameraBounds.bottom cameraLensHeight;
    
}
   }

   
//Update art positions
   
for each (var sprite:Away3DArt in _viewObjects{
    
    
if (sprite.group != sprite.citrusObject.group)
     
updateGroupForSprite(sprite);
    
    
sprite.update(this);
   
}
  }
 }

And the Away3DArt update method :

public function update(stateView:Away3DView):void {
   
 
var pos3D:Vector3D;
 var 
posX:NumberposY:Number;
 
 if (
content is Box2DDebugArt{
  
  
(content as Box2DDebugArt).update();
  
  var 
box2dDebugArt:b2DebugDraw = ((CitrusEngine.getInstance().state as State).getChildAt(2) as b2DebugDraw);
  
  if (
stateView.cameraTarget{
   
   box2dDebugArt
.stateView.container.x;
   
box2dDebugArt.stateView.container.y;
  
}
  
  box2dDebugArt
.visible _citrusObject.visible;

 
else {
  
  posX 
_citrusObject.x;
  
posY _citrusObject.y;
  
  
pos3D stateView.viewRoot.unproject(posXposY);
  
  
pos3D.x;
  
pos3D.y;
 
}

Any help will be very much appreciated, I’ve added the file in attachment.

 

File Attachments
Citrus-Engine.zip  (File Size: 1818KB - Downloads: 203)
   

Aymeric, Newbie
Posted: 24 September 2012 11:55 AM   Total Posts: 9   [ # 1 ]

UP!
It seems that the unproject method depends of the Camera position. I’ve updated the camera to have the same position than my container, but it doesn’t resolved my issue.
Any ideas?

   
   

X

Away3D Forum

Member Login

Username

Password

Remember_me



X