entities: rework velocity

This commit is contained in:
Lukas 2021-04-15 16:46:01 +02:00
parent c8a56cbb78
commit c613aec539
8 changed files with 37 additions and 79 deletions

View File

@ -1,33 +0,0 @@
/*
* Minosoft
* Copyright (C) 2020 Moritz Zwerger
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* This software is not affiliated with Mojang AB, the original developer of Minecraft.
*/
package de.bixilon.minosoft.data;
import de.bixilon.minosoft.data.entities.Velocity;
import de.bixilon.minosoft.data.entities.entities.Entity;
import de.bixilon.minosoft.protocol.network.connection.PlayConnection;
import java.util.HashMap;
public class VelocityHandler {
private final PlayConnection connection;
private final HashMap<Entity, Velocity> entities = new HashMap<>();
public VelocityHandler(PlayConnection connection) {
this.connection = connection;
}
public void handleVelocity(Entity entity, Velocity velocity) {
// ToDo
}
}

View File

@ -1,20 +0,0 @@
/*
* Minosoft
* Copyright (C) 2020 Moritz Zwerger
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* This software is not affiliated with Mojang AB, the original developer of Minecraft.
*/
package de.bixilon.minosoft.data.entities
data class Velocity(val x: Short, val y: Short, val z: Short) {
override fun toString(): String {
return "($x $y $z)"
}
}

View File

@ -26,7 +26,9 @@ import de.bixilon.minosoft.data.mappings.entities.EntityType
import de.bixilon.minosoft.data.text.ChatComponent
import de.bixilon.minosoft.gui.rendering.chunk.VoxelShape
import de.bixilon.minosoft.gui.rendering.chunk.models.AABB
import de.bixilon.minosoft.gui.rendering.util.VecUtil
import de.bixilon.minosoft.protocol.network.connection.PlayConnection
import de.bixilon.minosoft.protocol.protocol.ProtocolDefinition
import glm_.vec3.Vec3
import java.lang.reflect.InvocationTargetException
import java.util.*
@ -46,6 +48,8 @@ abstract class Entity(
var entityMetaData: EntityMetaData = EntityMetaData(connection)
var velocity: Vec3? = null
protected open val hasCollisions = true
private val defaultAABB = AABB(
@ -230,6 +234,22 @@ abstract class Entity(
return delta
}
fun computeTimeStep(deltaTime: Float) {
if (! hasNoGravity) {
if (velocity == null) {
velocity = Vec3(0, deltaTime * ProtocolDefinition.GRAVITY)
} else {
velocity!!.y += deltaTime * ProtocolDefinition.GRAVITY;
}
}
if (velocity == VecUtil.EMPTY_VEC3) {
velocity = null
}
velocity?.let {
move(velocity!! * deltaTime)
}
}
private val aabb: AABB
get() = defaultAABB + position

View File

@ -15,7 +15,6 @@ package de.bixilon.minosoft.protocol.network.connection
import de.bixilon.minosoft.Minosoft
import de.bixilon.minosoft.config.StaticConfiguration
import de.bixilon.minosoft.data.VelocityHandler
import de.bixilon.minosoft.data.accounts.Account
import de.bixilon.minosoft.data.assets.MultiAssetsManager
import de.bixilon.minosoft.data.commands.CommandRootNode
@ -59,7 +58,6 @@ class PlayConnection(
val scoreboardManager = ScoreboardManager()
val mapping = VersionMapping()
val sender = PacketSender(this)
val velocityHandler = VelocityHandler(this)
lateinit var assetsManager: MultiAssetsManager
private set

View File

@ -12,20 +12,20 @@
*/
package de.bixilon.minosoft.protocol.packets.s2c.play
import de.bixilon.minosoft.data.entities.Velocity
import de.bixilon.minosoft.protocol.network.connection.PlayConnection
import de.bixilon.minosoft.protocol.packets.s2c.PlayS2CPacket
import de.bixilon.minosoft.protocol.protocol.PlayInByteBuffer
import de.bixilon.minosoft.protocol.protocol.ProtocolDefinition
import de.bixilon.minosoft.util.logging.Log
import glm_.vec3.Vec3
class EntityVelocityS2CP(buffer: PlayInByteBuffer) : PlayS2CPacket() {
val entityId: Int = buffer.readEntityId()
val velocity: Velocity = Velocity(buffer.readShort(), buffer.readShort(), buffer.readShort())
val velocity: Vec3 = Vec3(buffer.readShort(), buffer.readShort(), buffer.readShort()) * ProtocolDefinition.VELOCITY_CONSTANT
override fun handle(connection: PlayConnection) {
val entity = connection.world.getEntity(entityId) ?: return
connection.velocityHandler.handleVelocity(entity, velocity)
entity.velocity = velocity
}
override fun log() {

View File

@ -15,7 +15,6 @@ package de.bixilon.minosoft.protocol.packets.s2c.play;
import de.bixilon.minosoft.config.StaticConfiguration;
import de.bixilon.minosoft.data.entities.EntityRotation;
import de.bixilon.minosoft.data.entities.Velocity;
import de.bixilon.minosoft.data.entities.entities.Entity;
import de.bixilon.minosoft.data.entities.entities.UnknownEntityException;
import de.bixilon.minosoft.data.entities.meta.EntityMetaData;
@ -23,6 +22,7 @@ import de.bixilon.minosoft.modding.event.events.EntitySpawnEvent;
import de.bixilon.minosoft.protocol.network.connection.PlayConnection;
import de.bixilon.minosoft.protocol.packets.s2c.PlayS2CPacket;
import de.bixilon.minosoft.protocol.protocol.PlayInByteBuffer;
import de.bixilon.minosoft.protocol.protocol.ProtocolDefinition;
import de.bixilon.minosoft.util.logging.Log;
import glm_.vec3.Vec3;
@ -32,7 +32,7 @@ import static de.bixilon.minosoft.protocol.protocol.ProtocolVersions.*;
public class PacketSpawnMob extends PlayS2CPacket {
private final int entityId;
private final Velocity velocity;
private final Vec3 velocity;
private final Entity entity;
private UUID entityUUID;
@ -55,7 +55,7 @@ public class PacketSpawnMob extends PlayS2CPacket {
position = buffer.readEntityPosition();
}
EntityRotation rotation = new EntityRotation(buffer.readAngle(), buffer.readAngle(), buffer.readAngle());
this.velocity = new Velocity(buffer.readShort(), buffer.readShort(), buffer.readShort());
this.velocity = new Vec3(buffer.readShort(), buffer.readShort(), buffer.readShort()).times(ProtocolDefinition.VELOCITY_CONSTANT);
EntityMetaData metaData = null;
if (buffer.getVersionId() < V_19W34A) {
@ -82,9 +82,8 @@ public class PacketSpawnMob extends PlayS2CPacket {
@Override
public void handle(PlayConnection connection) {
connection.fireEvent(new EntitySpawnEvent(connection, this));
connection.getWorld().addEntity(this.entityId, this.entityUUID, getEntity());
connection.getVelocityHandler().handleVelocity(getEntity(), getVelocity());
entity.setVelocity(velocity);
}
@Override
@ -95,9 +94,4 @@ public class PacketSpawnMob extends PlayS2CPacket {
public Entity getEntity() {
return this.entity;
}
public Velocity getVelocity() {
return this.velocity;
}
}

View File

@ -14,13 +14,13 @@
package de.bixilon.minosoft.protocol.packets.s2c.play;
import de.bixilon.minosoft.data.entities.EntityRotation;
import de.bixilon.minosoft.data.entities.Velocity;
import de.bixilon.minosoft.data.entities.entities.Entity;
import de.bixilon.minosoft.data.mappings.DefaultRegistries;
import de.bixilon.minosoft.modding.event.events.EntitySpawnEvent;
import de.bixilon.minosoft.protocol.network.connection.PlayConnection;
import de.bixilon.minosoft.protocol.packets.s2c.PlayS2CPacket;
import de.bixilon.minosoft.protocol.protocol.PlayInByteBuffer;
import de.bixilon.minosoft.protocol.protocol.ProtocolDefinition;
import de.bixilon.minosoft.util.logging.Log;
import glm_.vec3.Vec3;
@ -32,7 +32,7 @@ public class PacketSpawnObject extends PlayS2CPacket {
private final int entityId;
private UUID entityUUID;
private final Entity entity;
private Velocity velocity;
private Vec3 velocity;
public PacketSpawnObject(PlayInByteBuffer buffer) throws Exception {
this.entityId = buffer.readEntityId();
@ -58,10 +58,10 @@ public class PacketSpawnObject extends PlayS2CPacket {
if (buffer.getVersionId() < V_15W31A) {
if (data != 0) {
this.velocity = new Velocity(buffer.readShort(), buffer.readShort(), buffer.readShort());
this.velocity = new Vec3(buffer.readShort(), buffer.readShort(), buffer.readShort()).times(ProtocolDefinition.VELOCITY_CONSTANT);
}
} else {
this.velocity = new Velocity(buffer.readShort(), buffer.readShort(), buffer.readShort());
this.velocity = new Vec3(buffer.readShort(), buffer.readShort(), buffer.readShort()).times(ProtocolDefinition.VELOCITY_CONSTANT);
}
if (buffer.getVersionId() < V_19W05A) {
@ -77,7 +77,7 @@ public class PacketSpawnObject extends PlayS2CPacket {
connection.fireEvent(new EntitySpawnEvent(connection, this));
connection.getWorld().addEntity(this.entityId, this.entityUUID, getEntity());
connection.getVelocityHandler().handleVelocity(getEntity(), getVelocity());
entity.setVelocity(velocity);
}
@Override
@ -88,9 +88,4 @@ public class PacketSpawnObject extends PlayS2CPacket {
public Entity getEntity() {
return this.entity;
}
public Velocity getVelocity() {
return this.velocity;
}
}

View File

@ -93,6 +93,10 @@ public final class ProtocolDefinition {
public static final int TICKS_PER_SECOND = 20;
public static final int TICK_TIME = 1000 / TICKS_PER_SECOND;
public static final float VELOCITY_CONSTANT = 1 / 8000f * TICKS_PER_SECOND;
public static final float GRAVITY = -10; // TODO: find the correct value
public static final int SEA_LEVEL_HEIGHT = 62;
public static final float HEIGHT_SEA_LEVEL_MODIFIER = 0.00166667f;