优化obb命中判定

This commit is contained in:
Atsuishio 2025-06-16 12:05:17 +08:00 committed by Light_Quanta
parent 7a96a131fc
commit a1b062504d
No known key found for this signature in database
GPG key ID: 11A39A1B8C890959
2 changed files with 24 additions and 10 deletions

View file

@ -1,6 +1,7 @@
package com.atsuishio.superbwarfare.mixins;
import com.atsuishio.superbwarfare.entity.OBBEntity;
import com.atsuishio.superbwarfare.entity.projectile.ProjectileEntity;
import com.atsuishio.superbwarfare.tools.OBB;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.level.Level;
@ -24,6 +25,19 @@ public abstract class LevelMixin {
@Inject(method = "getEntities(Lnet/minecraft/world/entity/Entity;Lnet/minecraft/world/phys/AABB;Ljava/util/function/Predicate;)Ljava/util/List;",
at = @At("RETURN"))
public void getEntities(Entity pEntity, AABB pBoundingBox, Predicate<? super Entity> pPredicate, CallbackInfoReturnable<List<Entity>> cir) {
if (pEntity instanceof ProjectileEntity) {
this.getEntities().get(pBoundingBox.inflate(2), entity -> {
if (entity instanceof OBBEntity obbEntity) {
for (OBB obb : obbEntity.getOBBs()) {
if (OBB.isColliding(obb, pBoundingBox)) {
if (!cir.getReturnValue().contains(entity)) {
cir.getReturnValue().add(entity);
}
}
}
}
});
} else {
this.getEntities().get(pBoundingBox, entity -> {
if (entity instanceof OBBEntity obbEntity) {
for (OBB obb : obbEntity.getOBBs()) {
@ -36,4 +50,5 @@ public abstract class LevelMixin {
}
});
}
}
}

View file

@ -23,7 +23,7 @@ public class ProjectileUtilMixin {
@Inject(method = "getEntityHitResult(Lnet/minecraft/world/level/Level;Lnet/minecraft/world/entity/Entity;Lnet/minecraft/world/phys/Vec3;Lnet/minecraft/world/phys/Vec3;Lnet/minecraft/world/phys/AABB;Ljava/util/function/Predicate;F)Lnet/minecraft/world/phys/EntityHitResult;",
at = @At("HEAD"), cancellable = true)
private static void getEntityHitResult(Level pLevel, Entity pProjectile, Vec3 pStartVec, Vec3 pEndVec, AABB pBoundingBox, Predicate<Entity> pFilter, float pInflationAmount, CallbackInfoReturnable<EntityHitResult> cir) {
for (var entity : pLevel.getEntities(pProjectile, pBoundingBox, pFilter)) {
for (var entity : pLevel.getEntities(pProjectile, pBoundingBox.inflate(2), pFilter)) {
if (entity instanceof OBBEntity obbEntity) {
if (pProjectile instanceof Projectile projectile &&
(projectile.getOwner() == entity || entity.getPassengers().contains(projectile.getOwner()))) {
@ -31,7 +31,6 @@ public class ProjectileUtilMixin {
}
var obbList = obbEntity.getOBBs();
for (var obb : obbList) {
obb = obb.inflate(6);
Optional<Vector3f> optional = obb.clip(pStartVec.toVector3f(), pEndVec.toVector3f());
if (optional.isPresent()) {
double d1 = pStartVec.distanceToSqr(new Vec3(optional.get()));