Placing a cone

Software: Away3D 4.x

dmk, Jr. Member
Posted: 05 June 2014 02:03 PM   Total Posts: 32

So let’s say we have a cube that can be rotated in any direction along any axis. (FYI it’s a rectangular cube, which will eventually become a ship.)

Now we want to place a cone on any particular side such that the base of the cone attaches to the face of the cube and points outwards. (FYI this cone will eventually be a fire/booster.)

It must be a separate object, positioned rather than attached, since the same position vector will be used to give an impulse via awayphysics applyImpulse (not applyCentralImpulse)

I’m sure there is some simple method to calculate this for each side using backVector, forwardVector, etc… but I just couldn’t get it. Was starting to do my own calculations with trig functions but that’s a bit of a hassle and potentially wasteful if the data’s already there somewhere… Would post code but nothing I have really works wink

Thanks in advance!

   

Mr Margaret Scratcher, Sr. Member
Posted: 07 June 2014 11:22 AM   Total Posts: 344   [ # 1 ]

I’m about to do someing similar to apply decals to an object - check out the mouse interaction example, where you can draw on the head as a marker is drawn which sticks out perpendicular to the face your mouse is over..

   

dmk, Jr. Member
Posted: 08 June 2014 07:43 AM   Total Posts: 32   [ # 2 ]

Thanks for the reply. Seems like the example at https://github.com/away3d/away3d-tutorials-fp11/blob/master/tutorials/picking/overview/src/PickingTutorialListing05.as (live example: http://away3d.com/wikicontent/tutorials/picking/overview/bin/listing_05/Launcher.html) is pretty much along those lines (pun intended)

So this is really two problems:

1) Getting the location on the surface (location is mid-point on any particular face)
2) Getting the perpendicular vector out from that location

For problem #1, I’m still a bit stuck… with the mouse interaction examples, it seems like it’s just using MouseEvent3D (or in other examples, a ray cast from the camera I think?), so it doesn’t really do any calculation- it’s just wherever the user’s mouse is vs. camera

For problem #2 it seems like the answer is “get the normal” (i.e. in the example above, it’s a MouseEvent3D.sceneNormal, that does help.

Will continue to experiment unless someone has more guidance in the meantime… thanks

   

dmk, Jr. Member
Posted: 08 June 2014 09:14 AM   Total Posts: 32   [ # 3 ]

OK- I think I figured it out smile

Now to make the fire look good and tweak the values… wink

Though now I realize that having boosters on the side makes it kindof pointless to have tilting, or vice-versa…

package
{
 
 import flash
.geom.Vector3D;
 
 
import away3d.containers.ObjectContainer3D;
 
import away3d.containers.Scene3D;
 
import away3d.entities.Mesh;
 
import away3d.materials.ColorMaterial;
 
import away3d.materials.lightpickers.StaticLightPicker;
 
import away3d.primitives.ConeGeometry;
 
import away3d.primitives.CubeGeometry;
 
 
import awayphysics.collision.shapes.AWPBoxShape;
 
import awayphysics.dynamics.AWPDynamicsWorld;
 
import awayphysics.dynamics.AWPRigidBody;

 public class 
Ship extends AWPRigidBody
 {
  
  
public static const FORWARD:String 'forward';
  public static const 
LEFT:String 'left';
  public static const 
RIGHT:String 'right';
  public static const 
BACK:String 'back';
  public static const 
UP:String 'up';
  public static const 
DOWN:String 'down';
  
  
  
  private var 
scene:Scene3D;
  private var 
physicsWorldAWPDynamicsWorld;
  private var 
lightPicker:StaticLightPicker;
  
  private var 
shipMesh:Mesh;
  private var 
shipBody:AWPBoxShape;
  private var 
shipGeometry:CubeGeometry;
  
  private var 
fireContainer:ObjectContainer3D;
  private var 
fireGeometry:ConeGeometry;
  
  private var 
fireMeshPosition:Vector3D;
  private var 
fireForce:Vector3D;
  
  private var 
jetStrength:Number 100;
  private var 
tiltStrength:Number 2;
  
  private var 
fireMesh:Mesh;
  private var 
jetDirection:String BACK;
  
  public function 
Ship(_scene:Scene3D_lightPicker:StaticLightPicker_physicsWorld:AWPDynamicsWorld)
  
{
   
   scene 
_scene;
   
lightPicker _lightPicker;
   
physicsWorld _physicsWorld;
   
   
   
createShip();
   
scene.addChild(shipMesh);
   
   
   
   
super(shipBodyshipMesh10);
   
physicsWorld.addRigidBody(this);
   
   
this.gravity = new Vector3D();
   
   
createFire();
  
}
  
  
private function createShip() {
   
   
var mat:ColorMaterial = new ColorMaterial(0x00FF00);
  
   
shipGeometry = new CubeGeometry(50,50,200);
   
mat.lightPicker lightPicker;
   
shipMesh = new Mesh(shipGeometrymat);
   
shipBody = new AWPBoxShape(shipGeometry.widthshipGeometry.heightshipGeometry.depth);
   
   
  
}
  
  
private function createFire() {
   
   
var mat:ColorMaterial = new ColorMaterial(0xFF0000);
   
mat.lightPicker lightPicker;
   
   
fireGeometry = new ConeGeometry(25);
   
fireMesh = new Mesh(fireGeometrymat);
   
   
fireMesh.rotationX = -90;
   
fireMesh.= -fireGeometry.height/2;
   
   
fireContainer = new ObjectContainer3D();
   
fireContainer.addChild(fireMesh);
   
   
scene.addChild(fireContainer);
   
   
fireMeshPosition = new Vector3D();
   
fireForce = new Vector3D();
   
fireMesh.visible false;
   
  
}
  
  
//Triggered via Starling container
  
public function everyFrame(passedTime:Number{
   
   
if(fireMesh.visible{
    updateJetPosition
();
   
}
   
  }
  
  
private function updateJetPosition() {
   
var halfShipDepth:Number shipGeometry.depth/2;
   var 
halfShipWidth:Number shipGeometry.width/2;
   
   
   switch(
jetDirection{
    
case FORWARD:   fireMeshPosition.copyFrom(shipMesh.backVector);
     
fireMeshPosition.scaleBy(halfShipDepth);
     break;
    
    case 
LEFT:   fireMeshPosition.copyFrom(shipMesh.leftVector);
     
fireMeshPosition.scaleBy(halfShipWidth);
     break;
    
    case 
RIGHT:  fireMeshPosition.copyFrom(shipMesh.rightVector);
     
fireMeshPosition.scaleBy(halfShipWidth);
     break;
    
    case 
BACK:   fireMeshPosition.copyFrom(shipMesh.forwardVector);
     
fireMeshPosition.scaleBy(halfShipDepth);
     break;
    default:  break;
   
}
   
   fireMeshPosition
.incrementBy(shipMesh.position);
   
fireContainer.position fireMeshPosition;
   
   
fireContainer.lookAt(shipMesh.position);
  
}
  
  
public function jetsTo(dir:String{
   jetDirection 
dir;
   
fireMesh.visible true;
   
updateJetPosition();
   
   switch(
jetDirection{
    
case FORWARD:   fireForce.copyFrom(shipMesh.forwardVector);
         break;
    
    case 
LEFT:    fireForce.copyFrom(shipMesh.rightVector);
         break;
    
    case 
RIGHT:   fireForce.copyFrom(shipMesh.leftVector);
         break;
    
    case 
BACK:    fireForce.copyFrom(shipMesh.backVector);
         break;
    
    default:   break;
   
}
   
   fireForce
.scaleBy(jetStrength);
   
this.applyCentralForce(fireForce);
   
   
  
}
  
  
public function jetsOff(dir:String{
   fireMesh
.visible false;
  
}
  
//triggered external controller that listens for keypresses, etc.
  
public function tiltTo(dir:String{
   
//Note- need to make this smoother, based on timestep in everyFrame, etc. 
   
switch(dir{
    
case UP:  this.rotationX -= tiltStrength; break;
    case 
DOWN:  this.rotationX += tiltStrength; break;
    case 
LEFT:  this.rotationY -= tiltStrength; break;
    case 
RIGHT:  this.rotationY += tiltStrength; break;
    
   
}
   
  }
  
 }
   
   

X

Away3D Forum

Member Login

Username

Password

Remember_me



X