Skip to content

Commit

Permalink
Vec3: add in-place methods for rotation and transformation
Browse files Browse the repository at this point in the history
  • Loading branch information
stephengold committed Feb 24, 2025
1 parent 12bde6e commit 15886d6
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions src/main/java/com/github/stephengold/joltjni/Vec3.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ of this software and associated documentation files (the "Software"), to deal
package com.github.stephengold.joltjni;

import com.github.stephengold.joltjni.operator.Op;
import com.github.stephengold.joltjni.readonly.Mat44Arg;
import com.github.stephengold.joltjni.readonly.QuatArg;
import com.github.stephengold.joltjni.readonly.RVec3Arg;
import com.github.stephengold.joltjni.readonly.UVec4Arg;
import com.github.stephengold.joltjni.readonly.Vec3Arg;
Expand Down Expand Up @@ -183,6 +185,35 @@ public void normalizeInPlace() {
this.z *= invLength;
}

/**
* Rotate the current vector by the specified quaternion.
*
* @param rotation the rotation to apply (not null, normalized, unaffected)
*/
public void rotateInPlace(QuatArg rotation) {
assert rotation.isNormalized();

float lw = rotation.getW();
float lx = rotation.getX();
float ly = rotation.getY();
float lz = rotation.getZ();

float rx = x;
float ry = y;
float rz = z;

// a = lhs x pure(rhs)
float aw = -lx * rx - ly * ry - lz * rz;
float ax = lw * rx + ly * rz - lz * ry;
float ay = lw * ry - lx * rz + lz * rx;
float az = lw * rz + lx * ry - ly * rx;

// result = vec3(a x conjugate(lhs))
this.x = -aw * lx + ax * lw - ay * lz + az * ly;
this.y = -aw * ly + ax * lz + ay * lw - az * lx;
this.z = -aw * lz - ax * ly + ay * lx + az * lw;
}

/**
* Return the bitwise AND of the specified vectors.
*
Expand Down Expand Up @@ -524,6 +555,16 @@ public static Vec3 sZero() {
Vec3 result = new Vec3();
return result;
}

/**
* Transform the current vector by the specified matrix.
*
* @param matrix the transformation to apply (not null, unaffected)
*/
public void transformInPlace(Mat44Arg matrix) {
Vec3Arg temp = matrix.multiply3x4(this); // TODO garbage
set(temp);
}
// *************************************************************************
// Vec3Arg methods

Expand Down

0 comments on commit 15886d6

Please sign in to comment.