Hi,
i have little frustrating problem. I know linear velocity of the ship and its direction. i would like to move spatial inside ship’s local space by same amount as linear velocity. but spatial have local translation and linear velocity is world translation, i cannot figure out, how to move spatial in local space in same direction as linear velocity in world space
i have particle effect around ship to simulate some debris (particle emitter attached directly to ship’s node). and if ship accelerates, i want to move particle emitter in that direction by some amount, so more debris is created in front of the linear velocity
Converting a velocity vector from world coordinates to local coordinates can involve both rotation and scaling.
You can obtain the scaling factor from:
[java]
Vector3f worldScale = ship.getWorldScale();
[/java]
and the rotation quaternion from:
[java]
Quaternion worldRotation = ship.getWorldRotation();
[/java]
Assuming uniform scaling (worldScale.x == worldScale.y && worldScale.y == worldScale.z) , you would then divide world velocity by both quantities. For instance:
[java]
float scale = worldScale.x;
Vector3f scaledVelocity = worldVelocity.divide(scale);
Vector3f localVeclocity = worldRotation.inverse().mult(scaledVelocity);
[/java]
i workaround it this way: i attached emitter directly into the scene at origin, not ship, and then simply
[java]spatial.setLocalTranslation(aligner.getPhysicsLocation().add(aligner.getLinearVelocity()));[/java]
solution by sgold worked like charm, thanks a lot man im ignoring scaling for now, so i made it like this:
[java]
public class AlignByVelocityControl extends ControlAdapter {
solution by pspeed didnt work for me, at line 3, worldtolocal wants 2 args, and spatial.worldToLocal(rel, null); nor spatial.worldToLocal(rel, new Vector3f()); didnt make desired results
@Ascaria said:
solution by pspeed didnt work for me, at line 3, worldtolocal wants 2 args, and spatial.worldToLocal(rel, null); nor spatial.worldToLocal(rel, new Vector3f()); didnt make desired results :(
Well, yes the second arg could be null or new Vector3f(). I’m glad you got things working but something else must have been wrong because sgold was essentially manually doing exactly what my code did. Anyway, if you don’t have to worry about scaling then the inverse rotation is simpler.
@pspeed said:
Well, yes the second arg could be null or new Vector3f(). I'm glad you got things working but something else must have been wrong because sgold was essentially manually doing exactly what my code did. Anyway, if you don't have to worry about scaling then the inverse rotation is simpler.
tried again, i tried to get spatial’s world translation instead of local on your 2. line and it works
so final version is:
[java]public class AlignByVelocityControl extends ControlAdapter {
name of method worldToLocal seems to be good straight forward for me, it doing what i expect. also it is good to know, what is behind the scenes and you said it is essentially the same. i had same idea as sgold, use inverted (also negated, or opposite) world rotation so cancelling it out, but cannot figure out what method and in what order to use. i feel smarter now