Knockback System
Knockback System
Section titled “Knockback System”The knockback system applies velocity impulses to entities, typically from combat or environmental interactions.
Package Location
Section titled “Package Location”com.hypixel.hytale.server.core.entity.knockback
Overview
Section titled “Overview”Knockback is managed via the KnockbackComponent attached to entities. It stores velocity, duration, and modifiers that control how the knockback is applied over time.
Core Classes
Section titled “Core Classes”KnockbackComponent
Section titled “KnockbackComponent”Component managing knockback state on an entity:
package com.hypixel.hytale.server.core.entity.knockback;
public class KnockbackComponent implements Component<EntityStore> { // Velocity public Vector3d getVelocity(); public void setVelocity(Vector3d velocity);
// Velocity application type public ChangeVelocityType getVelocityType(); public void setVelocityType(ChangeVelocityType velocityType);
// Velocity configuration public VelocityConfig getVelocityConfig(); public void setVelocityConfig(VelocityConfig velocityConfig);
// Modifiers (velocity multipliers) public void addModifier(double modifier); public void applyModifiers();
// Duration and timing public float getDuration(); public void setDuration(float duration); public float getTimer(); public void setTimer(float time); public void incrementTimer(float time);
// Get component type public static ComponentType<EntityStore, KnockbackComponent> getComponentType();}ChangeVelocityType
Section titled “ChangeVelocityType”How knockback velocity is applied:
package com.hypixel.hytale.protocol;
public enum ChangeVelocityType { ADD, // Add to existing velocity SET, // Replace existing velocity SET_X, // Replace only X component SET_Y, // Replace only Y component SET_Z // Replace only Z component}VelocityConfig
Section titled “VelocityConfig”Configuration for velocity application:
public class VelocityConfig { // Velocity limits and behavior double maxSpeed; double drag; double gravity;}Usage Examples
Section titled “Usage Examples”Applying Basic Knockback
Section titled “Applying Basic Knockback”import com.hypixel.hytale.server.core.entity.knockback.KnockbackComponent;import com.hypixel.hytale.math.vector.Vector3d;import com.hypixel.hytale.protocol.ChangeVelocityType;
// Get knockback component from entityRef<EntityStore> entityRef = /* entity reference */;KnockbackComponent knockback = componentAccessor.getComponent( entityRef, KnockbackComponent.getComponentType());
if (knockback != null) { // Calculate knockback direction (away from attacker) Vector3d direction = new Vector3d( targetPos.x - attackerPos.x, 0.5, // Upward component targetPos.z - attackerPos.z ); direction.normalize(); direction.scale(8.0); // Knockback strength
// Apply knockback knockback.setVelocity(direction); knockback.setVelocityType(ChangeVelocityType.ADD); knockback.setDuration(0.3f); // 0.3 second knockback knockback.setTimer(0.0f);}Knockback with Modifiers
Section titled “Knockback with Modifiers”// Apply knockback with modifiersknockback.setVelocity(new Vector3d(10, 3, 0));knockback.setVelocityType(ChangeVelocityType.SET);knockback.setDuration(0.5f);knockback.setTimer(0.0f);
// Add velocity modifiers (multipliers)knockback.addModifier(0.9); // 90% - slight reductionknockback.addModifier(1.2); // 120% - strength buff
// Apply all modifiers (multiplies velocity by 0.9 * 1.2 = 1.08)knockback.applyModifiers();Directional Knockback
Section titled “Directional Knockback”// Horizontal knockback only (no Y change)knockback.setVelocity(new Vector3d(5, 0, 5));knockback.setVelocityType(ChangeVelocityType.ADD);
// Vertical knockback only (launch upward)knockback.setVelocity(new Vector3d(0, 15, 0));knockback.setVelocityType(ChangeVelocityType.SET_Y);
// Replace horizontal, preserve verticalknockback.setVelocity(new Vector3d(8, 0, 0));knockback.setVelocityType(ChangeVelocityType.SET_X);Checking Knockback State
Section titled “Checking Knockback State”// Check if entity is being knocked backfloat timer = knockback.getTimer();float duration = knockback.getDuration();
if (timer < duration) { // Entity is still in knockback float progress = timer / duration; // 0.0 to 1.0}
// Manually increment timer (usually done by physics system)knockback.incrementTimer(deltaTime);Custom Velocity Configuration
Section titled “Custom Velocity Configuration”// Set custom velocity configVelocityConfig config = new VelocityConfig();config.maxSpeed = 20.0;config.drag = 0.98;config.gravity = 0.08;
knockback.setVelocityConfig(config);Knockback Calculations
Section titled “Knockback Calculations”Combat Knockback
Section titled “Combat Knockback”Standard combat knockback formula:
// Calculate knockback from attackpublic Vector3d calculateCombatKnockback( Vector3d attackerPos, Vector3d targetPos, float knockbackPower) { // Direction away from attacker Vector3d direction = new Vector3d( targetPos.x - attackerPos.x, 0, targetPos.z - attackerPos.z );
// Normalize and scale if (direction.squaredLength() > 0.0001) { direction.normalize(); } else { // Default direction if positions overlap direction = new Vector3d(1, 0, 0); }
// Apply power direction.scale(knockbackPower);
// Add upward component direction.y = knockbackPower * 0.4;
return direction;}Explosion Knockback
Section titled “Explosion Knockback”Knockback from explosions:
public Vector3d calculateExplosionKnockback( Vector3d explosionPos, Vector3d targetPos, float power, float radius) { Vector3d direction = new Vector3d( targetPos.x - explosionPos.x, targetPos.y - explosionPos.y, targetPos.z - explosionPos.z );
double distance = direction.length(); if (distance == 0) return Vector3d.ZERO;
// Falloff based on distance double falloff = 1.0 - (distance / radius); falloff = Math.max(0, falloff);
direction.normalize(); direction.scale(power * falloff);
return direction;}Integration with Physics
Section titled “Integration with Physics”The knockback system integrates with the physics system:
- KnockbackComponent stores pending knockback
- Physics system reads knockback each tick
- Velocity applied according to ChangeVelocityType
- Timer incremented until duration reached
- Knockback cleared when timer >= duration
Frame 0: Knockback applied (timer=0, duration=0.3)Frame 1: Physics applies velocity, timer=0.016Frame 2: Physics applies velocity, timer=0.033...Frame 18: timer=0.3, knockback completeKnockback Resistance
Section titled “Knockback Resistance”Entities can resist knockback through:
- Stat modifiers - Reduce knockback velocity
- Effects - Immunity or reduction
- Entity properties - Heavy entities resist more
// Apply knockback with resistance checkdouble resistance = entity.getStatValue(Stats.KNOCKBACK_RESISTANCE);double multiplier = 1.0 - resistance; // resistance 0.5 = 50% knockback
Vector3d adjustedVelocity = velocity.clone();adjustedVelocity.scale(multiplier);
knockback.setVelocity(adjustedVelocity);Related
Section titled “Related”- Effects System - Status effects
- Physics System - Velocity and movement
- Entity System - Entity management