From 5cebc1a7efffa9344c4a5c6ee36efa630ddbd14b Mon Sep 17 00:00:00 2001 From: Bixilon Date: Thu, 25 May 2023 01:39:07 +0200 Subject: [PATCH 01/12] wip improved camera shaking --- .../gui/rendering/camera/MatrixHandler.kt | 4 +- .../rendering/camera/shaking/CameraShaking.kt | 44 +++++++++++++++---- 2 files changed, 37 insertions(+), 11 deletions(-) diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/camera/MatrixHandler.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/camera/MatrixHandler.kt index 7cf128de42..d931bcdfe6 100644 --- a/src/main/java/de/bixilon/minosoft/gui/rendering/camera/MatrixHandler.kt +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/camera/MatrixHandler.kt @@ -109,12 +109,12 @@ class MatrixHandler( fun draw() { dynamicFOV += camera.view.view.fovMultiplier - shaking.draw() + val update = shaking.update() val fov = calculateFOV() val view = camera.view.view val eyePosition = view.eyePosition val front = view.front - if (upToDate && eyePosition == this.eyePosition && front == this.front && fov == previousFOV && shaking.isEmpty) { + if (!update && upToDate && eyePosition == this.eyePosition && front == this.front && fov == previousFOV && shaking.isEmpty) { return } this.eyePosition = eyePosition diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/camera/shaking/CameraShaking.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/camera/shaking/CameraShaking.kt index b3133412dd..fc9b37c403 100644 --- a/src/main/java/de/bixilon/minosoft/gui/rendering/camera/shaking/CameraShaking.kt +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/camera/shaking/CameraShaking.kt @@ -20,24 +20,33 @@ import de.bixilon.kutil.avg.FloatAverage import de.bixilon.kutil.time.TimeUtil.millis import de.bixilon.minosoft.config.profile.profiles.rendering.camera.shaking.ShakingC import de.bixilon.minosoft.gui.rendering.camera.Camera -import de.bixilon.minosoft.gui.rendering.renderer.drawable.Drawable import de.bixilon.minosoft.protocol.protocol.ProtocolDefinition +import kotlin.math.PI import kotlin.math.sin class CameraShaking( private val camera: Camera, private val profile: ShakingC, -) : Drawable { +) { private var rotation = 0.0f private var strength = FloatAverage(5 * ProtocolDefinition.TICK_TIME * 1_000_000L, 1.0f) - private val speed = FloatAverage(5 * ProtocolDefinition.TICK_TIME * 1_000_000L, 0.0f) + private val speed = FloatAverage(10 * ProtocolDefinition.TICK_TIME * 1_000_000L, 0.0f) val isEmpty: Boolean get() = rotation == 0.0f - override fun draw() { + fun update(): Boolean { this.strength += 1.0f - val strength = this.strength.avg * profile.amplifier // strength affects how far it goes + val time = millis() + val walking = bobbing(time) + val damage = damage(time) + + val previous = this.rotation + this.rotation = walking + damage + + return this.rotation != previous + } + private fun bobbing(time: Long): Float { val physics = camera.context.connection.camera.entity.physics val velocity = physics.velocity.xz.length2().toFloat() // velocity affects how quick it goes if (velocity > 0.003 && physics.onGround) { @@ -45,14 +54,31 @@ class CameraShaking( } else { this.speed += 0.0f // TODO: remove this, kutil 1.21 } - val time = (millis() % 100L).toFloat() / 100.0f - this.rotation = sin(time * minOf(this.speed.avg, 0.5f) / 3.0f) * strength * 0.03f + val speed = minOf(this.speed.avg, 0.5f) + + if (speed == 0.0f) return 0.0f + + val intensity = speed * 100 + val time = sin(millis().toDouble() / 1000.0) + val timeIntensity = sin(time / intensity) + + println(timeIntensity) + return sin(timeIntensity * PI * 2).toFloat() / 10 + } + + private fun damage(time: Long): Float { + val strength = this.strength.avg * profile.amplifier // strength affects how far it goes + + + // this.rotation = sin(time * minOf(this.speed.avg, 0.5f) / 3.0f) * strength * 0.03f + + // TODO + return 0.0f } fun onDamage() { - strength += 1000.0f - speed += 0.05f + strength += 10.0f } fun transform(): Mat4? { From ae605fbdaf6d32cbd1798a0a8a341b6dbc8b7a2b Mon Sep 17 00:00:00 2001 From: turtiustrek <12457839+turtiustrek@users.noreply.github.com> Date: Fri, 26 May 2023 18:41:55 +0500 Subject: [PATCH 02/12] fix camera shaking jitter --- .../rendering/camera/shaking/CameraShaking.kt | 65 +++++++------------ 1 file changed, 25 insertions(+), 40 deletions(-) diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/camera/shaking/CameraShaking.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/camera/shaking/CameraShaking.kt index fc9b37c403..15471c927f 100644 --- a/src/main/java/de/bixilon/minosoft/gui/rendering/camera/shaking/CameraShaking.kt +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/camera/shaking/CameraShaking.kt @@ -29,61 +29,46 @@ class CameraShaking( private val profile: ShakingC, ) { private var rotation = 0.0f - private var strength = FloatAverage(5 * ProtocolDefinition.TICK_TIME * 1_000_000L, 1.0f) + private var previous_velocity = 0.0f + private var strength = 0.02f private val speed = FloatAverage(10 * ProtocolDefinition.TICK_TIME * 1_000_000L, 0.0f) + private val physics = camera.context.connection.camera.entity.physics val isEmpty: Boolean get() = rotation == 0.0f fun update(): Boolean { - this.strength += 1.0f + if(!this.physics.onGround){ + this.speed += 0.0f + this.rotation = 0.0f + return false + } val time = millis() - val walking = bobbing(time) - val damage = damage(time) - - val previous = this.rotation - this.rotation = walking + damage - - return this.rotation != previous + this.rotation = bobbing(time,10f,this.strength) + this.strength = 0.02f + return true } - private fun bobbing(time: Long): Float { - val physics = camera.context.connection.camera.entity.physics - val velocity = physics.velocity.xz.length2().toFloat() // velocity affects how quick it goes - if (velocity > 0.003 && physics.onGround) { + private fun bobbing(time: Long,frequency: Float, intensity: Float): Float { + var velocity = this.physics.velocity.xz.length2().toFloat() // velocity affects how quick it goes + if(this.previous_velocity != 0.0f){ + velocity = this.previous_velocity + (velocity - this.previous_velocity) * 0.25f // interpolate this.speed += velocity - } else { - this.speed += 0.0f // TODO: remove this, kutil 1.21 + }else{ + this.speed += 0.0f } - - val speed = minOf(this.speed.avg, 0.5f) - - if (speed == 0.0f) return 0.0f - - val intensity = speed * 100 - val time = sin(millis().toDouble() / 1000.0) - val timeIntensity = sin(time / intensity) - - println(timeIntensity) - return sin(timeIntensity * PI * 2).toFloat() / 10 + this.previous_velocity = velocity + var seconds = (time/1000.0).toDouble() + val minimum_speed = 0.14f + return (sin(seconds * frequency).toFloat() * minOf(this.speed.avg, 0.1f) * intensity) / minimum_speed } - - private fun damage(time: Long): Float { - val strength = this.strength.avg * profile.amplifier // strength affects how far it goes - - - // this.rotation = sin(time * minOf(this.speed.avg, 0.5f) / 3.0f) * strength * 0.03f - - // TODO - return 0.0f - } - fun onDamage() { - strength += 10.0f + //TODO: verify this properly, frequency may need to be changed as well + this.strength += 1f } fun transform(): Mat4? { - if (rotation == 0.0f) return null + if (this.rotation == 0.0f) return null return Mat4() - .rotateAssign(rotation, Vec3(0, 0, 1)) + .rotateAssign(this.rotation, Vec3(0, 0, 1)) } } From 680d0a5660b0adbc7a6ec2a5d848cf0fb650f95d Mon Sep 17 00:00:00 2001 From: turtiustrek <12457839+turtiustrek@users.noreply.github.com> Date: Fri, 26 May 2023 18:41:55 +0500 Subject: [PATCH 03/12] fix camera shaking jitter --- .../rendering/camera/shaking/CameraShaking.kt | 64 +++++++------------ 1 file changed, 22 insertions(+), 42 deletions(-) diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/camera/shaking/CameraShaking.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/camera/shaking/CameraShaking.kt index fc9b37c403..c74557b22d 100644 --- a/src/main/java/de/bixilon/minosoft/gui/rendering/camera/shaking/CameraShaking.kt +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/camera/shaking/CameraShaking.kt @@ -21,7 +21,6 @@ import de.bixilon.kutil.time.TimeUtil.millis import de.bixilon.minosoft.config.profile.profiles.rendering.camera.shaking.ShakingC import de.bixilon.minosoft.gui.rendering.camera.Camera import de.bixilon.minosoft.protocol.protocol.ProtocolDefinition -import kotlin.math.PI import kotlin.math.sin class CameraShaking( @@ -29,61 +28,42 @@ class CameraShaking( private val profile: ShakingC, ) { private var rotation = 0.0f - private var strength = FloatAverage(5 * ProtocolDefinition.TICK_TIME * 1_000_000L, 1.0f) + private var strength = 0.02f private val speed = FloatAverage(10 * ProtocolDefinition.TICK_TIME * 1_000_000L, 0.0f) + private val physics = camera.context.connection.camera.entity.physics val isEmpty: Boolean get() = rotation == 0.0f fun update(): Boolean { - this.strength += 1.0f - val time = millis() - val walking = bobbing(time) - val damage = damage(time) - - val previous = this.rotation - this.rotation = walking + damage - - return this.rotation != previous - } - - private fun bobbing(time: Long): Float { - val physics = camera.context.connection.camera.entity.physics - val velocity = physics.velocity.xz.length2().toFloat() // velocity affects how quick it goes - if (velocity > 0.003 && physics.onGround) { - this.speed += velocity - } else { - this.speed += 0.0f // TODO: remove this, kutil 1.21 + if (!this.physics.onGround) { + this.speed += 0.0f + this.rotation = 0.0f + return false } - - val speed = minOf(this.speed.avg, 0.5f) - - if (speed == 0.0f) return 0.0f - - val intensity = speed * 100 - val time = sin(millis().toDouble() / 1000.0) - val timeIntensity = sin(time / intensity) - - println(timeIntensity) - return sin(timeIntensity * PI * 2).toFloat() / 10 + val time = millis() + this.rotation = bobbing(time, 10f, this.strength) + this.strength = 0.02f + return true } - private fun damage(time: Long): Float { - val strength = this.strength.avg * profile.amplifier // strength affects how far it goes - - - // this.rotation = sin(time * minOf(this.speed.avg, 0.5f) / 3.0f) * strength * 0.03f - - // TODO - return 0.0f + private fun bobbing(time: Long, frequency: Float, intensity: Float): Float { + this.speed += this.physics.velocity.xz.length2().toFloat() // velocity affects how quick it goes + val seconds = time / 1000.0 + return (sin(seconds * frequency).toFloat() * minOf(this.speed.avg, 0.25f) * intensity) / MINIMUM_SPEED } fun onDamage() { - strength += 10.0f + //TODO: verify this properly, frequency may need to be changed as well + this.strength += 1.0f } fun transform(): Mat4? { - if (rotation == 0.0f) return null + if (this.rotation == 0.0f) return null return Mat4() - .rotateAssign(rotation, Vec3(0, 0, 1)) + .rotateAssign(this.rotation, Vec3(0, 0, 1)) + } + + companion object { + private const val MINIMUM_SPEED = 0.1f } } From eaf24743fc20b173268c4ae7c9c49bcc4c51ac86 Mon Sep 17 00:00:00 2001 From: Bixilon Date: Sun, 28 May 2023 13:58:26 +0200 Subject: [PATCH 04/12] fix build --- .../de/bixilon/minosoft/gui/rendering/camera/MatrixHandler.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/camera/MatrixHandler.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/camera/MatrixHandler.kt index 8bb2253a3c..189e5133a7 100644 --- a/src/main/java/de/bixilon/minosoft/gui/rendering/camera/MatrixHandler.kt +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/camera/MatrixHandler.kt @@ -109,14 +109,14 @@ class MatrixHandler( fun draw() { dynamicFOV += camera.view.view.fovMultiplier - val update = shaking.update() + val shaking = shaking.update() val fov = calculateFOV() val view = camera.view.view val eyePosition = view.eyePosition context.camera.offset.draw() val matrixPosition = Vec3(eyePosition - context.camera.offset.offset) val front = view.front - if (!update && upToDate && eyePosition == this.eyePosition && front == this.front && fov == previousFOV && shaking.isEmpty) { + if (!shaking && upToDate && matrixPosition == this.matrixPosition && front == this.front && fov == previousFOV && this.shaking.isEmpty) { return } this.matrixPosition = matrixPosition From c15f4fca4a31f29086b126439dc76c09be9ee514 Mon Sep 17 00:00:00 2001 From: Bixilon Date: Sun, 28 May 2023 14:24:44 +0200 Subject: [PATCH 05/12] camera shaking: lower shaking rate, refactor a bit --- .../gui/rendering/camera/shaking/CameraShaking.kt | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/camera/shaking/CameraShaking.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/camera/shaking/CameraShaking.kt index c74557b22d..012875360d 100644 --- a/src/main/java/de/bixilon/minosoft/gui/rendering/camera/shaking/CameraShaking.kt +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/camera/shaking/CameraShaking.kt @@ -28,7 +28,7 @@ class CameraShaking( private val profile: ShakingC, ) { private var rotation = 0.0f - private var strength = 0.02f + private var strength = 0.0f private val speed = FloatAverage(10 * ProtocolDefinition.TICK_TIME * 1_000_000L, 0.0f) private val physics = camera.context.connection.camera.entity.physics @@ -41,15 +41,18 @@ class CameraShaking( return false } val time = millis() - this.rotation = bobbing(time, 10f, this.strength) - this.strength = 0.02f + this.rotation = bobbing(time, 10.0f, this.strength) + this.strength = STRENGTH return true } private fun bobbing(time: Long, frequency: Float, intensity: Float): Float { this.speed += this.physics.velocity.xz.length2().toFloat() // velocity affects how quick it goes val seconds = time / 1000.0 - return (sin(seconds * frequency).toFloat() * minOf(this.speed.avg, 0.25f) * intensity) / MINIMUM_SPEED + + val sin = sin(seconds * frequency).toFloat() + val speed = minOf(this.speed.avg, 0.25f) + return (sin * speed * intensity) / MINIMUM_SPEED } fun onDamage() { @@ -64,6 +67,7 @@ class CameraShaking( } companion object { + private const val STRENGTH = 0.001f private const val MINIMUM_SPEED = 0.1f } } From 664791821b938afaf1cfc0f627a8a6cdf73088fd Mon Sep 17 00:00:00 2001 From: Bixilon Date: Sun, 28 May 2023 14:26:17 +0200 Subject: [PATCH 06/12] camera shaking: config amplifier --- .../minosoft/gui/rendering/camera/shaking/CameraShaking.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/camera/shaking/CameraShaking.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/camera/shaking/CameraShaking.kt index 012875360d..27a216597e 100644 --- a/src/main/java/de/bixilon/minosoft/gui/rendering/camera/shaking/CameraShaking.kt +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/camera/shaking/CameraShaking.kt @@ -42,7 +42,7 @@ class CameraShaking( } val time = millis() this.rotation = bobbing(time, 10.0f, this.strength) - this.strength = STRENGTH + this.strength = STRENGTH * profile.amplifier return true } From 17c48d9ff7d44156b48974e4f181b78c6d57b9f0 Mon Sep 17 00:00:00 2001 From: Bixilon Date: Sun, 28 May 2023 14:33:35 +0200 Subject: [PATCH 07/12] remove damage shaking Need to be reimplemented --- .../rendering/camera/shaking/ShakingC.kt | 3 +- .../rendering/camera/shaking/CameraShaking.kt | 39 ++++++++++++++----- 2 files changed, 31 insertions(+), 11 deletions(-) diff --git a/src/main/java/de/bixilon/minosoft/config/profile/profiles/rendering/camera/shaking/ShakingC.kt b/src/main/java/de/bixilon/minosoft/config/profile/profiles/rendering/camera/shaking/ShakingC.kt index 9240c52df8..8774a41229 100644 --- a/src/main/java/de/bixilon/minosoft/config/profile/profiles/rendering/camera/shaking/ShakingC.kt +++ b/src/main/java/de/bixilon/minosoft/config/profile/profiles/rendering/camera/shaking/ShakingC.kt @@ -23,7 +23,8 @@ class ShakingC(profile: RenderingProfile) { */ var enabled by BooleanDelegate(profile, true) - var amplifier by FloatDelegate(profile, 1.0f, "", ranges = arrayOf(0.1f..2.0f)) + var speed by FloatDelegate(profile, 1.0f, "", ranges = arrayOf(0.1f..2.0f)) + var intensity by FloatDelegate(profile, 1.0f, "", ranges = arrayOf(0.1f..2.0f)) /** * Shake the camera while walking diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/camera/shaking/CameraShaking.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/camera/shaking/CameraShaking.kt index 27a216597e..1a0d7276d6 100644 --- a/src/main/java/de/bixilon/minosoft/gui/rendering/camera/shaking/CameraShaking.kt +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/camera/shaking/CameraShaking.kt @@ -28,26 +28,40 @@ class CameraShaking( private val profile: ShakingC, ) { private var rotation = 0.0f - private var strength = 0.0f private val speed = FloatAverage(10 * ProtocolDefinition.TICK_TIME * 1_000_000L, 0.0f) private val physics = camera.context.connection.camera.entity.physics val isEmpty: Boolean get() = rotation == 0.0f fun update(): Boolean { + var bobbing = 0.0f + if (profile.walking) { + bobbing = updateBobbing() + } + var damage = 0.0f + if (profile.damage) { + damage = updateDamage() + } + + val rotation = bobbing + damage + + if (rotation == this.rotation) return false + this.rotation = rotation + return true + } + + private fun updateBobbing(): Float { if (!this.physics.onGround) { this.speed += 0.0f - this.rotation = 0.0f - return false + return 0.0f } val time = millis() - this.rotation = bobbing(time, 10.0f, this.strength) - this.strength = STRENGTH * profile.amplifier - return true + this.speed += this.physics.velocity.xz.length2().toFloat() // velocity affects how quick it goes + val speed = this.speed.avg + return bobbing(time, speed, FREQUENCY * profile.speed, STRENGTH * profile.intensity) } - private fun bobbing(time: Long, frequency: Float, intensity: Float): Float { - this.speed += this.physics.velocity.xz.length2().toFloat() // velocity affects how quick it goes + private fun bobbing(time: Long, speed: Float, frequency: Float, intensity: Float): Float { val seconds = time / 1000.0 val sin = sin(seconds * frequency).toFloat() @@ -55,9 +69,13 @@ class CameraShaking( return (sin * speed * intensity) / MINIMUM_SPEED } + private fun updateDamage(): Float { + //TODO + return 0.0f + } + fun onDamage() { - //TODO: verify this properly, frequency may need to be changed as well - this.strength += 1.0f + //TODO } fun transform(): Mat4? { @@ -68,6 +86,7 @@ class CameraShaking( companion object { private const val STRENGTH = 0.001f + private const val FREQUENCY = 10.0f private const val MINIMUM_SPEED = 0.1f } } From dc5320915084406ef2dbd8b372a0da90bb4b7b7a Mon Sep 17 00:00:00 2001 From: Bixilon Date: Sun, 28 May 2023 14:49:26 +0200 Subject: [PATCH 08/12] camera shaking: fine tune more --- .../gui/rendering/camera/shaking/CameraShaking.kt | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/camera/shaking/CameraShaking.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/camera/shaking/CameraShaking.kt index 1a0d7276d6..104573d720 100644 --- a/src/main/java/de/bixilon/minosoft/gui/rendering/camera/shaking/CameraShaking.kt +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/camera/shaking/CameraShaking.kt @@ -28,7 +28,7 @@ class CameraShaking( private val profile: ShakingC, ) { private var rotation = 0.0f - private val speed = FloatAverage(10 * ProtocolDefinition.TICK_TIME * 1_000_000L, 0.0f) + private val speed = FloatAverage(5 * ProtocolDefinition.TICK_TIME * 1_000_000L, 0.0f) private val physics = camera.context.connection.camera.entity.physics val isEmpty: Boolean get() = rotation == 0.0f @@ -57,7 +57,7 @@ class CameraShaking( } val time = millis() this.speed += this.physics.velocity.xz.length2().toFloat() // velocity affects how quick it goes - val speed = this.speed.avg + val speed = minOf(this.speed.avg, 0.25f) return bobbing(time, speed, FREQUENCY * profile.speed, STRENGTH * profile.intensity) } @@ -65,7 +65,6 @@ class CameraShaking( val seconds = time / 1000.0 val sin = sin(seconds * frequency).toFloat() - val speed = minOf(this.speed.avg, 0.25f) return (sin * speed * intensity) / MINIMUM_SPEED } @@ -81,11 +80,12 @@ class CameraShaking( fun transform(): Mat4? { if (this.rotation == 0.0f) return null return Mat4() - .rotateAssign(this.rotation, Vec3(0, 0, 1)) + .rotateAssign(this.rotation, ROTATION) } companion object { - private const val STRENGTH = 0.001f + private val ROTATION = Vec3(0, 0, 1) + private const val STRENGTH = 0.002f private const val FREQUENCY = 10.0f private const val MINIMUM_SPEED = 0.1f } From 4ec8fcd6ed949aadd960a2914f66aa0e6925374a Mon Sep 17 00:00:00 2001 From: vimpop <12457839+turtiustrek@users.noreply.github.com> Date: Thu, 15 Jun 2023 23:23:23 +0500 Subject: [PATCH 09/12] Add in vertical translation --- .../rendering/camera/shaking/CameraShaking.kt | 36 +++++++++---------- 1 file changed, 17 insertions(+), 19 deletions(-) diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/camera/shaking/CameraShaking.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/camera/shaking/CameraShaking.kt index 104573d720..9a53de7c27 100644 --- a/src/main/java/de/bixilon/minosoft/gui/rendering/camera/shaking/CameraShaking.kt +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/camera/shaking/CameraShaking.kt @@ -28,37 +28,36 @@ class CameraShaking( private val profile: ShakingC, ) { private var rotation = 0.0f + private var cameraTransform: Mat4? = Mat4() private val speed = FloatAverage(5 * ProtocolDefinition.TICK_TIME * 1_000_000L, 0.0f) private val physics = camera.context.connection.camera.entity.physics val isEmpty: Boolean get() = rotation == 0.0f fun update(): Boolean { - var bobbing = 0.0f if (profile.walking) { - bobbing = updateBobbing() + this.cameraTransform = updateBobbing() } - var damage = 0.0f if (profile.damage) { - damage = updateDamage() + updateDamage() } - - val rotation = bobbing + damage - - if (rotation == this.rotation) return false - this.rotation = rotation return true } - private fun updateBobbing(): Float { + private fun updateBobbing(): Mat4? { if (!this.physics.onGround) { this.speed += 0.0f - return 0.0f + return null } + val transform = Mat4() val time = millis() this.speed += this.physics.velocity.xz.length2().toFloat() // velocity affects how quick it goes val speed = minOf(this.speed.avg, 0.25f) - return bobbing(time, speed, FREQUENCY * profile.speed, STRENGTH * profile.intensity) + val translation = bobbing(time, speed, TRANSLATION_FREQUENCY * profile.speed, TRANSLATION_STRENGTH * profile.intensity) + val rotation = bobbing(time, speed, ROTATION_FREQUENCY * profile.speed, ROTATION_STRENGTH * profile.intensity) + transform.translateAssign(Vec3(0, translation, 0)) + transform.rotateAssign(rotation, Vec3(0, 0, 1)) + return transform } private fun bobbing(time: Long, speed: Float, frequency: Float, intensity: Float): Float { @@ -78,15 +77,14 @@ class CameraShaking( } fun transform(): Mat4? { - if (this.rotation == 0.0f) return null - return Mat4() - .rotateAssign(this.rotation, ROTATION) + return cameraTransform } companion object { - private val ROTATION = Vec3(0, 0, 1) - private const val STRENGTH = 0.002f - private const val FREQUENCY = 10.0f + private const val ROTATION_STRENGTH = 0.002f + private const val ROTATION_FREQUENCY = 10.0f + private const val TRANSLATION_STRENGTH = 0.1f + private const val TRANSLATION_FREQUENCY = 15.0f private const val MINIMUM_SPEED = 0.1f } -} +} \ No newline at end of file From 844aa80128f4d4c9c77c50e7a6812d46007f53eb Mon Sep 17 00:00:00 2001 From: vimpop <12457839+turtiustrek@users.noreply.github.com> Date: Thu, 15 Jun 2023 23:24:35 +0500 Subject: [PATCH 10/12] whitespace --- .../minosoft/gui/rendering/camera/shaking/CameraShaking.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/camera/shaking/CameraShaking.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/camera/shaking/CameraShaking.kt index 9a53de7c27..082ca66798 100644 --- a/src/main/java/de/bixilon/minosoft/gui/rendering/camera/shaking/CameraShaking.kt +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/camera/shaking/CameraShaking.kt @@ -87,4 +87,4 @@ class CameraShaking( private const val TRANSLATION_FREQUENCY = 15.0f private const val MINIMUM_SPEED = 0.1f } -} \ No newline at end of file +} From 9995270e579dd69099d96ffaadc5e841f4c7affd Mon Sep 17 00:00:00 2001 From: vimpop <12457839+turtiustrek@users.noreply.github.com> Date: Fri, 16 Jun 2023 02:05:08 +0500 Subject: [PATCH 11/12] remove redundant variable --- .../de/bixilon/minosoft/gui/rendering/camera/MatrixHandler.kt | 2 +- .../minosoft/gui/rendering/camera/shaking/CameraShaking.kt | 2 -- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/camera/MatrixHandler.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/camera/MatrixHandler.kt index 189e5133a7..9e88784b02 100644 --- a/src/main/java/de/bixilon/minosoft/gui/rendering/camera/MatrixHandler.kt +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/camera/MatrixHandler.kt @@ -116,7 +116,7 @@ class MatrixHandler( context.camera.offset.draw() val matrixPosition = Vec3(eyePosition - context.camera.offset.offset) val front = view.front - if (!shaking && upToDate && matrixPosition == this.matrixPosition && front == this.front && fov == previousFOV && this.shaking.isEmpty) { + if (!shaking && upToDate && matrixPosition == this.matrixPosition && front == this.front && fov == previousFOV) { return } this.matrixPosition = matrixPosition diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/camera/shaking/CameraShaking.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/camera/shaking/CameraShaking.kt index 082ca66798..ac6941493e 100644 --- a/src/main/java/de/bixilon/minosoft/gui/rendering/camera/shaking/CameraShaking.kt +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/camera/shaking/CameraShaking.kt @@ -27,12 +27,10 @@ class CameraShaking( private val camera: Camera, private val profile: ShakingC, ) { - private var rotation = 0.0f private var cameraTransform: Mat4? = Mat4() private val speed = FloatAverage(5 * ProtocolDefinition.TICK_TIME * 1_000_000L, 0.0f) private val physics = camera.context.connection.camera.entity.physics - val isEmpty: Boolean get() = rotation == 0.0f fun update(): Boolean { if (profile.walking) { From 681ccae1967e09891633db5e0af1b66d1cbfa9d1 Mon Sep 17 00:00:00 2001 From: vimpop <12457839+turtiustrek@users.noreply.github.com> Date: Fri, 16 Jun 2023 02:07:17 +0500 Subject: [PATCH 12/12] code for shaking camera when taking damage --- .../rendering/camera/shaking/CameraShaking.kt | 28 +++++++++++++------ 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/camera/shaking/CameraShaking.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/camera/shaking/CameraShaking.kt index ac6941493e..a00a1b4d0f 100644 --- a/src/main/java/de/bixilon/minosoft/gui/rendering/camera/shaking/CameraShaking.kt +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/camera/shaking/CameraShaking.kt @@ -28,18 +28,25 @@ class CameraShaking( private val profile: ShakingC, ) { private var cameraTransform: Mat4? = Mat4() + private var damaged: Boolean = false private val speed = FloatAverage(5 * ProtocolDefinition.TICK_TIME * 1_000_000L, 0.0f) private val physics = camera.context.connection.camera.entity.physics fun update(): Boolean { + if (this.damaged) { + if (profile.damage) { + this.cameraTransform = updateDamage() + this.damaged = false + return true + } + } if (profile.walking) { this.cameraTransform = updateBobbing() + return true + } - if (profile.damage) { - updateDamage() - } - return true + return false } private fun updateBobbing(): Mat4? { @@ -65,13 +72,16 @@ class CameraShaking( return (sin * speed * intensity) / MINIMUM_SPEED } - private fun updateDamage(): Float { - //TODO - return 0.0f + private fun updateDamage(): Mat4 { + val transform = Mat4() + val time = millis() + val rotation = bobbing(time, 0.5f, DAMAGE_FREQUENCY * profile.speed, DAMAGE_STRENGTH * profile.intensity) + transform.rotateAssign(rotation, Vec3(0, 0, 1)) + return transform } fun onDamage() { - //TODO + this.damaged = true } fun transform(): Mat4? { @@ -79,6 +89,8 @@ class CameraShaking( } companion object { + private const val DAMAGE_FREQUENCY = 40.0f + private const val DAMAGE_STRENGTH = 0.004f private const val ROTATION_STRENGTH = 0.002f private const val ROTATION_FREQUENCY = 10.0f private const val TRANSLATION_STRENGTH = 0.1f