Skip to content

Commit

Permalink
Fix: Knockback calculation
Browse files Browse the repository at this point in the history
Now only our yaw rotation is broken but not the knockback calculation
  • Loading branch information
Snowiiii committed Aug 18, 2024
1 parent c7b3ce6 commit 4c34a98
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
8 changes: 5 additions & 3 deletions pumpkin/src/client/player_packet.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::f32::consts::PI;

use num_traits::FromPrimitive;
use pumpkin_entity::EntityId;
use pumpkin_protocol::{
Expand Down Expand Up @@ -284,12 +286,12 @@ impl Client {
return;
}
if config.knockback {
let pitch = attacker_player.entity.pitch;
let yaw = attacker_player.entity.yaw;
let strength = 1.0;
player.knockback(
strength * 0.5,
(pitch * 0.017453292).sin() as f64,
-(pitch * 0.017453292).cos() as f64,
(yaw * (PI / 180.0)).sin() as f64,
-(yaw * (PI / 180.0)).cos() as f64,
);
let packet = &CEntityVelocity::new(
&entity_id,
Expand Down
8 changes: 4 additions & 4 deletions pumpkin/src/entity/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,21 +53,21 @@ impl Player {
self.entity.entity_id
}

pub fn knockback(&mut self, y: f64, x: f64, z: f64) {
pub fn knockback(&mut self, strength: f64, x: f64, z: f64) {
// This has some vanilla magic
let mut x = x;
let mut z = z;
while x * x + z * z < 9.999999747378752E-6 {
while x * x + z * z < 1.0E-5 {
x = (rand::random::<f64>() - rand::random::<f64>()) * 0.01;
z = (rand::random::<f64>() - rand::random::<f64>()) * 0.01;
}

let var8 = Vector3::new(x, 0.0, z).normalize() * y;
let var8 = Vector3::new(x, 0.0, z).normalize() * strength;
let var7 = self.velocity;
self.velocity = Vector3::new(
var7.x / 2.0 - var8.x,
if self.on_ground {
(var7.y / 2.0 + x).min(0.4)
(var7.y / 2.0 + strength).min(0.4)
} else {
var7.y
},
Expand Down

0 comments on commit 4c34a98

Please sign in to comment.