Skip to content

Commit 14960c7

Browse files
authored
Merge pull request #307 from LookUpGroup27/fix/sphere-not-facing-user
Fix Moon Rotation and Align Planets to Face the User
2 parents 0e03475 + f04440b commit 14960c7

File tree

2 files changed

+38
-9
lines changed
  • app/src/main/java/com/github/lookupgroup27/lookup/model/map/renderables

2 files changed

+38
-9
lines changed

app/src/main/java/com/github/lookupgroup27/lookup/model/map/renderables/Moon.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class Moon(
3333
numBands = numBands,
3434
stepsPerBand = stepsPerBand,
3535
scale = 0.05f,
36+
rotationSpeed = 0.0f,
3637
funFact = "The Moon is moving away from Earth !") {
3738
/** Companion object containing moon phase calculation and texture mapping logic. */
3839
companion object {

app/src/main/java/com/github/lookupgroup27/lookup/model/map/renderables/Planet.kt

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@ import com.github.lookupgroup27.lookup.util.opengl.TextureManager
2626
* @property name The name of the planet (e.g., "Earth"). Defaults to "Planet".
2727
* @property position The planet's position in 3D space, represented as a float array [x, y, z].
2828
* @property textureId The resource ID of the texture applied to the planet's surface.
29-
* @property vertexShaderCode The custom vertex shader code used for rendering the planet.
30-
* @property fragmentShaderCode The custom fragment shader code used for rendering the planet.
3129
* @property scale The scaling factor applied to the planet's geometry. Defaults to 0.3.
3230
*/
3331
open class Planet(
@@ -38,6 +36,7 @@ open class Planet(
3836
numBands: Int = SphereRenderer.DEFAULT_NUM_BANDS,
3937
stepsPerBand: Int = SphereRenderer.DEFAULT_STEPS_PER_BAND,
4038
private val scale: Float = 0.02f,
39+
private val rotationSpeed: Float = 50f,
4140
val funFact: String = ""
4241
) : Object() {
4342

@@ -52,7 +51,6 @@ open class Planet(
5251

5352
// New properties for rotation
5453
private var rotationAngle: Float = 0f // Current rotation angle in degrees
55-
private val rotationSpeed: Float = 50f // Rotation speed in degrees per second (constant for all)
5654

5755
/** Initializes the planet's geometry, shaders, and texture. */
5856
init {
@@ -94,7 +92,10 @@ open class Planet(
9492
fun draw(camera: Camera, transformMatrix: FloatArray? = null) {
9593
label.draw(camera)
9694
val modelMatrix = FloatArray(16)
95+
val billboardMatrix = FloatArray(16)
96+
val mvpMatrix = FloatArray(16)
9797
Matrix.setIdentityM(modelMatrix, 0)
98+
Matrix.setIdentityM(billboardMatrix, 0)
9899

99100
// Apply the provided transform matrix or use the default planet position
100101
if (transformMatrix != null) {
@@ -104,19 +105,46 @@ open class Planet(
104105
Matrix.scaleM(modelMatrix, 0, scale, scale, scale)
105106
}
106107

107-
val viewMatrix = camera.viewMatrix
108-
val projMatrix = camera.projMatrix
109-
val mvpMatrix = FloatArray(16)
108+
// Extract camera look direction from view matrix
109+
val lookX = -camera.viewMatrix[2] // Third column of view matrix
110+
val lookY = -camera.viewMatrix[6] // is the look direction
111+
val lookZ = -camera.viewMatrix[10]
112+
113+
// Create billboard rotation
114+
val upX = camera.viewMatrix[1] // Second column is up vector
115+
val upY = camera.viewMatrix[5]
116+
val upZ = camera.viewMatrix[9]
117+
118+
// Calculate right vector (cross product)
119+
val rightX = upY * lookZ - upZ * lookY
120+
val rightY = upZ * lookX - upX * lookZ
121+
val rightZ = upX * lookY - upY * lookX
122+
123+
// Set billboard matrix
124+
fun setMatrixRow(matrix: FloatArray, startIndex: Int, x: Float, y: Float, z: Float, w: Float) {
125+
matrix[startIndex] = x
126+
matrix[startIndex + 1] = y
127+
matrix[startIndex + 2] = z
128+
matrix[startIndex + 3] = w
129+
}
130+
131+
// Set billboard matrix
132+
setMatrixRow(billboardMatrix, 0, rightX, rightY, rightZ, 0f)
133+
setMatrixRow(billboardMatrix, 4, upX, upY, upZ, 0f)
134+
setMatrixRow(billboardMatrix, 8, lookX, lookY, lookZ, 0f)
135+
setMatrixRow(billboardMatrix, 12, 0f, 0f, 0f, 1f)
110136

111137
// Combine matrices: Projection * View * Model
112138

113139
// Apply rotation transformation
114-
Matrix.rotateM(modelMatrix, 0, rotationAngle, 0f, 1f, 0f) // Rotate around the Y-axis
140+
val rotatedMatrix = FloatArray(16)
141+
Matrix.rotateM(billboardMatrix, 0, rotationAngle, 0f, 1f, 0f)
142+
Matrix.multiplyMM(rotatedMatrix, 0, modelMatrix, 0, billboardMatrix, 0)
115143

116144
// Combine model, view, and projection matrices in correct order
117145
val viewModelMatrix = FloatArray(16)
118-
Matrix.multiplyMM(viewModelMatrix, 0, viewMatrix, 0, modelMatrix, 0)
119-
Matrix.multiplyMM(mvpMatrix, 0, projMatrix, 0, viewModelMatrix, 0)
146+
Matrix.multiplyMM(viewModelMatrix, 0, camera.viewMatrix, 0, rotatedMatrix, 0)
147+
Matrix.multiplyMM(mvpMatrix, 0, camera.projMatrix, 0, viewModelMatrix, 0)
120148

121149
// Pass final MVP matrix to the renderer
122150
sphereRenderer.bindShaderAttributes(mvpMatrix)

0 commit comments

Comments
 (0)