Firing Arrows

Software: Away3D 4.x

dmk, Jr. Member
Posted: 13 May 2014 09:37 AM   Total Posts: 32

I have this code which seems to work so far for sortof throwing a cylinder:

private function doFire() {
   
var cylinderShape:AWPCylinderShape;
   var 
body:AWPRigidBody;
   var 
impulse:Vector3D;
   var 
mesh Mesh;
   var 
geom:CylinderGeometry;
   var 
material:ColorMaterial;
   
   
   
geom = new CylinderGeometry(10,10);
   
   
material = new ColorMaterial(0xe28313);
   
material.lightPicker lightPicker;
   
   
mesh = new Mesh(geommaterial);
   
scene.addChild(mesh);
    
   
cylinderShape = new AWPCylinderShape(geom.topRadiusgeom.height);
   
body = new AWPRigidBody(cylinderShapemesh10);
   
body.rotationX 90;
   
   
body.friction .9;
   
body.position = new Vector3D(0,terrain.getHeightAt(0,0),0);
   
   
   
physicsWorld.addRigidBody(body);
   
   
impulse = new Vector3D(0,.3,1);
   
impulse.normalize();
   
impulse.scaleBy(200);
   
   
body.applyCentralImpulse(impulse);
  

Now I need to make it more like an arrow. I’m a newbie to all this, so a few questions:

1) What would be the best-practices way to use a real 3d model of an arrow? I would think that it’s enough just to use a cylinder for the body. But when I tried loading an AWD, and setting that to the mesh, it doesn’t travel with the body…

2) It seems that the object does travel across an arc like with gravity, but it does not rotate… how to make the arrow (cylinder) point up as it travels up and down as it travels down?

3) How to add in wind?

Thanks!

   

John Brookes, Moderator
Posted: 22 May 2014 12:39 PM   Total Posts: 732   [ # 1 ]

Never tried it.
Presume you could actually use a cylinder with two forces, one added to the arrow head and one on the flights.
Although think a sphere would be simpler.

Use a sphere shape to get the trajectory for the arrow path.
Use the previous position of the sphere as your arrow mesh position and then make your arrow lookAt the current position.

Wind is just a force, so add that force.

   

dmk, Jr. Member
Posted: 22 May 2014 01:20 PM   Total Posts: 32   [ # 2 ]

Fantastic, will try those approaches out, thanks!

   

dmk, Jr. Member
Posted: 26 May 2014 02:37 PM   Total Posts: 32   [ # 3 ]

using lookAt() while updating the AWPRigidBody position makes it always null… here’s my code. Containing class just extends AWPRigidBody and handles scene display etc. If I take out the lookAt it works okay (though not quite the same as though I passed it to the AWPRigidBody constructor, interestingly enough)

private var prevPosition null;
override public function set position(pos Vector3D) : void {
   arrowMesh
.position prevPosition == null pos prevPosition;
   
   
super.position pos;
   
   
trace('PREVIOUS POSITION:'prevPosition);
   
trace('NEW POSITION:'pos);
   
trace("ARROW POSITION BEFORE LOOKAT:"arrowMesh.position);
   
arrowMesh.lookAt(pos);
   
trace("ARROW POSITION AFTER LOOKAT:"arrowMesh.position);
   
trace('');
   
   
prevPosition pos.clone();

Output:

PREVIOUS POSITION: null
NEW POSITION: Vector3D(130.62252807617188, 201.1764678955078, -470.0707092285156)
ARROW POSITION BEFORE LOOKAT: Vector3D(130.62252807617188, 201.1764678955078, -470.0707092285156)
ARROW POSITION AFTER LOOKAT: Vector3D(NaN, NaN, NaN)

PREVIOUS POSITION: Vector3D(130.62252807617188, 201.1764678955078, -470.0707092285156)
NEW POSITION: Vector3D(130.6225299835205, 201.17647647857666, -470.0706958770752)
ARROW POSITION BEFORE LOOKAT: Vector3D(NaN, NaN, NaN)
ARROW POSITION AFTER LOOKAT: Vector3D(NaN, NaN, NaN)

PREVIOUS POSITION: Vector3D(130.6225299835205, 201.17647647857666, -470.0706958770752)
NEW POSITION: Vector3D(140.1988387107849, 204.3365478515625, -438.3280277252197)
ARROW POSITION BEFORE LOOKAT: Vector3D(NaN, NaN, NaN)
ARROW POSITION AFTER LOOKAT: Vector3D(NaN, NaN, NaN)

etc.

   

dmk, Jr. Member
Posted: 27 May 2014 07:46 AM   Total Posts: 32   [ # 4 ]

OK- using previous position doesn’t necessarily make sense since it needs to be behind the sphere at least the radius of the sphere.

When I tried just setting the z position of the arrow to current position - 100 for testing, it sortof works… but my 3d math is sucky. How would I place the arrow behind the RigidBody (relative to initial position/camera)

In other words, let’s say we have a FPS setup…so firing is calculated like:

arrow.position camera.position.clone();
var 
impulse:Vector3D camera.forwardVector.clone();
impulse.scaleBy(200);
arrow.applyCentralImpulse(impulse); 

where arrow is an AWPRigidBody using AWPSphereShape(). Now on every frame we want to place arrowMesh (just a AWD loaded mesh) right behind the sphere as it travels. Behind meaning relative to that initial camera position on x and z, same y as camera

 

   

John Brookes, Moderator
Posted: 28 May 2014 03:50 PM   Total Posts: 732   [ # 5 ]

A quick test
https://gist.github.com/JohnBrookes/357822f0df1f0dc9b0b8
double click to fire

   

dmk, Jr. Member
Posted: 29 May 2014 02:38 PM   Total Posts: 32   [ # 6 ]

Thanks for putting that together! Turns out we decided to go for spheres instead of arrows, but I’ll try to give your demo a whirl next week. Really appreciate it!

   
   

X

Away3D Forum

Member Login

Username

Password

Remember_me



X