Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve camera shaking #15

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,44 +20,55 @@ 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 var previous_velocity = 0.0f
turtiustrek marked this conversation as resolved.
Show resolved Hide resolved
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

override fun draw() {
this.strength += 1.0f
val strength = this.strength.avg * profile.amplifier // strength affects how far it goes
fun update(): Boolean {
if(!this.physics.onGround){
this.speed += 0.0f
this.rotation = 0.0f
return false
}
val time = millis()
this.rotation = bobbing(time,10f,this.strength)
turtiustrek marked this conversation as resolved.
Show resolved Hide resolved
this.strength = 0.02f
return true
}

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
turtiustrek marked this conversation as resolved.
Show resolved Hide resolved
this.speed += velocity
} else {
this.speed += 0.0f // TODO: remove this, kutil 1.21
}else{
turtiustrek marked this conversation as resolved.
Show resolved Hide resolved
this.speed += 0.0f
}
val time = (millis() % 100L).toFloat() / 100.0f

this.rotation = sin(time * minOf(this.speed.avg, 0.5f) / 3.0f) * strength * 0.03f
this.previous_velocity = velocity
var seconds = (time/1000.0).toDouble()
turtiustrek marked this conversation as resolved.
Show resolved Hide resolved
val minimum_speed = 0.14f
turtiustrek marked this conversation as resolved.
Show resolved Hide resolved
return (sin(seconds * frequency).toFloat() * minOf(this.speed.avg, 0.1f) * intensity) / minimum_speed
}

fun onDamage() {
strength += 1000.0f
speed += 0.05f
//TODO: verify this properly, frequency may need to be changed as well
this.strength += 1f
turtiustrek marked this conversation as resolved.
Show resolved Hide resolved
}

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))
}
}