Aligning bullets/projectiles from player location

Started by K24A3, October 14, 2011, 06:52:42 PM

Previous topic - Next topic

K24A3

Is there an 'official' way of aligning/rotating projectiles shot from a 3D Object?

I've managed to whip up the following code:


SimpleVector sv = new SimpleVector();
sv = player.getZAxis();
bullet[n].clearTranslation();
bullet[n].clearRotation();
bullet[n].setRotationPivot(sv);
bullet[n].align(player);
sv = player.getTranslation();
sv.y += 6f; // Adjust offset
sv.x -= 2f; // Adjust offset
bullet[n].translate(sv);


Which works ok but the bullet is shot with a slight translation offset depending on the axis of the player.
I realize that sv.y+=6f and sv.x-=2f; is probably the cause, but I was wandering if there is a concrete solution for projecting projectiles from a 3D object.

EgonOlsen

I would try something like:


bullet.clearTranslation();
bullet.translate(obj.getTranslation()); // Or maybe getTransformedCenter()...it depends...
bullet.getRotationMatrix().setTo(obj.getRotationMatrix());


There's no official way...whatever works for you is fine.

K24A3

I tried that code and it helped centering the projectile to the players center, but the projectiles were all facing/shooting towards -X.
So I added "bullet[n].setRotationPivot(player.getZAxis())" to fix that, but the projectile is still offset depending on the YAxis of the player. (Same as before)

If I point the player towards:
+Y = OK
-Y = OK
+Z = OK
-Z = OK
+X = The projectiles are slightly offset to the left of the object
-X = The projectiles are slightly offset to the right of the object

Current code:

bullet[n].clearTranslation();
bullet[n].translate(player.getTransformedCenter()); // getTranslation doesn't work properly
bullet[n].getRotationMatrix().setTo(player.getRotationMatrix());
bullet[n].setRotationPivot(player.getZAxis()); // Added because the projectiles were pointing towards -X
//bullet[n].setRotationPivot(player.getRotationPivot()); // No good. All projectiles point to -Z



I am rotating the player per frame using just rotateAxis which is working well. (not using Object3D.rotateY etc..)

// Horz
player.rotateAxis(player.getYAxis(), (fSenseYaw*0.1f)*fDiv);
// Vert
player.rotateAxis(player.getXAxis(), (fSensePitch*0.1f)*fDiv);


I am moving the projectile per frame using:
// Move in its direction
SimpleVector sv = bullet[n].getRotationPivot(); //sv = bullet[n].getZAxis(); (getZ doesnt work properly)
sv.scalarMul(10.0f);
bullet[n].translate(sv);



Thinking the object was off center, I tried correcting it by translating the mesh (Z ways) and then calling translateMesh() but that didn't help. The object itself became way off the camera's center depending on the Rotational Axis.

I also tried replacing the object with a simple cube primitive (loading it with just two lines of code), with no pre translation or rotation:
player = Primitives.getCube(1);
world.addObject(player);
But the projectiles are still offset when point towards -X or +X

I can't figure out why that is happening. I guess I could correct it on the fly by translating Z depending on the XAxis.x value, but I would like to avoid that kind of correction if possible.

K24A3

Egon, I've sent you an example app via email. If you have a spare moment and figure out why there is an offset when facing X, that would be great. Thanks.

EgonOlsen

Setting the rotation pivot serves no prupose here. It's the point around which a rotation is done...you seem to use it rather as a storage for the initial direction. I've looked at your code and changed the shoot-method back to:


private void shoot() {
projectile.clearTranslation();
projectile.translate(player.getTransformedCenter());
projectile.getRotationMatrix().setTo(player.getRotationMatrix();
}


and the movement to


SimpleVector sv =  projectile.getZAxis();
sv.scalarMul(10.0f);
projectile.translate(sv);


I can't see anything wrong with the results (except that i had to disable that sensor-thing and replaced it with a static rotation, because with the sensor code active, it spins around like crazy on my phone)... ???

K24A3

Thanks, the code changes have fixed the problem, plus the projectile collision detection works much better now.

Appreciate the help, I'm still trying to get my head around all the 3D math and concepts :P

ps. The sensor orientation is set to Tablet landscape orientation so it spins incorrectly on a portrait phone. The xyz variables just need to be toggled I believe (I'll fix that up later).