Rotate mesh to mouse direction. (SOLVED)

Software: Away3D 4.x

Gugis, Newbie
Posted: 11 July 2012 09:06 AM   Total Posts: 6

I’m making top-down billiard game with Away3D 4.0 and i need to get billiard stick rotated to mouse direction. It should be something like this: http://kirill-poletaev.blogspot.com/2010/07/rotate-object-to-mouse-using-as3.html just in 3D What’s the best way to do that?
Thank you for your answers.

   

Richard Olsson, Administrator
Posted: 11 July 2012 10:41 AM   Total Posts: 1192   [ # 1 ]

Same as in 2D, but rotate around the Y axis instead of the Z axis (which is the default and sometimes only rotation axis in Flash and other flat screen media.)

   

Gugis, Newbie
Posted: 11 July 2012 11:05 AM   Total Posts: 6   [ # 2 ]
Richard Olsson - 11 July 2012 10:41 AM

Same as in 2D, but rotate around the Y axis instead of the Z axis (which is the default and sometimes only rotation axis in Flash and other flat screen media.)

I tried like this:

private function Move(e:MouseEvent):void 
{
var radians:Number Math.atan2(mouseY-stick.ymouseX-stick.x);
var 
degrees:Number radians*180/Math.PI;
stick.rotationZ degrees;

But it works wrong. Mouse cursor closer to stick - lower rotation angle.
And there’s full code.

package  
{
 import away3d
.cameras.Camera3D;
 
import away3d.containers.Scene3D;
 
import away3d.containers.View3D;
 
import away3d.entities.Mesh;
 
import away3d.materials.ColorMaterial;
 
import away3d.primitives.CylinderGeometry;
 
import flash.display.Sprite;
 
import flash.events.Event;
 
import flash.events.MouseEvent;
 
/**
  * ...
  * @author Gugis
  */
 
public class Pool extends Sprite 
 {
  
var scene:Scene3D;
  var 
view:View3D;
  var 
camera:Camera3D;
  var 
stick:Mesh;
  
  public function 
Pool() 
  
{
   
//Events
   
addEventListener(Event.ENTER_FRAMEUpdate);
   
addEventListener(MouseEvent.MOUSE_MOVEMove);
   
   
//View
   
view = new View3D();
   
scene view.scene;
   
camera view.camera;
   
addChild(view);
   
   
//Stick
   
stick = new Mesh(new CylinderGeometry(107600), new ColorMaterial(0xFF0000));
   
scene.addChild(stick);
   
stick.rotationZ 90;
  
}
  
  
private function Move(e:MouseEvent):void 
  {
   
var radians:Number Math.atan2(e.stageY-stick.ye.stageX-stick.x);
   var 
degrees:Number radians*180/Math.PI;
   
stick.rotationZ degrees;
  
}

  
private function Update(e:Event):void 
  {
   view
.render(); 
  

 }
   

Richard Olsson, Administrator
Posted: 11 July 2012 11:15 AM   Total Posts: 1192   [ # 3 ]

First of all, if you’re doing a top down game you should probably be modifying rotationY, not rotationZ.

Second, what is the ball object? Is that a 3D object in your scene? You can’t compare objects’ coordinates in the scene with the coordinates of the mouse on the stage (unless you really know what you’re doing.) Either calculate the position of the ball on the stage (using Camera3D.project()) or the mouse cursors position in 3D space on the plane on which your ball sits (e.g. the table.) You can do the latter by listening for 3D mouse events on that plane instead of using regular mouse events, and using the sceneX, sceneY and sceneZ properties of the event.

   

Gugis, Newbie
Posted: 11 July 2012 11:24 AM   Total Posts: 6   [ # 4 ]

Thank you for tips Richard, I almost made it smile Now I need to move pivot point to the end of stick. It seems movePivot function doesn’t work.

Current code:

package  
{
 import away3d
.cameras.Camera3D;
 
import away3d.containers.Scene3D;
 
import away3d.containers.View3D;
 
import away3d.entities.Mesh;
 
import away3d.events.MouseEvent3D;
 
import away3d.materials.ColorMaterial;
 
import away3d.primitives.CylinderGeometry;
 
import away3d.primitives.PlaneGeometry;
 
import flash.display.Sprite;
 
import flash.events.Event;
 
import flash.events.MouseEvent;
 
/**
  * ...
  * @author Gugis
  */
 
public class Pool extends Sprite 
 {
  
var scene:Scene3D;
  var 
view:View3D;
  var 
camera:Camera3D;
  var 
stick:Mesh;
  var 
table:Mesh;
  public function 
Pool() 
  
{  
   
//View
   
view = new View3D();
   
scene view.scene;
   
camera view.camera;
   
camera.1000;
   
camera.0;
   
camera.rotationX 90;
   
addChild(view);
   
   
//Stick
   
stick = new Mesh(new CylinderGeometry(107600), new ColorMaterial(0xFF0000));
   
scene.addChild(stick);
   
stick.rotationZ 90;
   
stick.movePivot(05000);

   
//Table
   
table = new Mesh(new PlaneGeometry(1500800), new ColorMaterial(0xCCCCCC));
   
table.mouseEnabled true;
   
scene.addChild(table);
   
   
//Events
   
addEventListener(Event.ENTER_FRAMEUpdate);
   
table.addEventListener(MouseEvent3D.MOUSE_MOVEMove);
  
}
  
  
private function Move(e:MouseEvent3D):void 
  {
   
var radians:Number Math.atan2(e.sceneZ-stick.ze.sceneX-stick.x);
   var 
degrees:Number radians*180/Math.PI;
   
stick.rotationX = -degrees;
  
}

  
private function Update(e:Event):void 
  {
   view
.render(); 
  

 }

 

   

Richard Olsson, Administrator
Posted: 11 July 2012 01:09 PM   Total Posts: 1192   [ # 5 ]

There was a bug in movePivot() (and pivotPoint) which was fixed recently in the release branch, meaning that fix will be part of the final 4.0 release very soon. If you want to use movePivot() or the pivotPoint property, please use the release branch from GitHub for now.

   
   

X

Away3D Forum

Member Login

Username

Password

Remember_me



X