Create standalone physics plugin
This commit is contained in:
53
plugin/physics/pom.xml
Normal file
53
plugin/physics/pom.xml
Normal file
@@ -0,0 +1,53 @@
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>com.aaaaahhhhhhh.bananapuncher714</groupId>
|
||||
<artifactId>mc-mesh-plugin</artifactId>
|
||||
<version>0.0.1</version>
|
||||
</parent>
|
||||
<artifactId>mc-mesh-plugin-physics</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.spigotmc</groupId>
|
||||
<artifactId>spigot</artifactId>
|
||||
<version>1.21.10-R0.1-SNAPSHOT</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.aaaaahhhhhhh.bananapuncher714</groupId>
|
||||
<artifactId>mc-mesh-plugin-environment</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-shade-plugin</artifactId>
|
||||
<version>3.1.0</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>package</phase>
|
||||
<goals>
|
||||
<goal>shade</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.14.1</version>
|
||||
<configuration>
|
||||
<release>21</release>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.aaaaahhhhhhh.bananapuncher714.mesh.physics;
|
||||
|
||||
import org.bukkit.entity.Display;
|
||||
import org.bukkit.util.Vector;
|
||||
|
||||
class AuxiliaryDisplayStruct {
|
||||
Display display;
|
||||
Vector offset;
|
||||
Vector scale;
|
||||
|
||||
AuxiliaryDisplayStruct( Display display, Vector offset ) {
|
||||
this.display = display;
|
||||
this.offset = offset.clone();
|
||||
this.scale = new Vector( 1, 1, 1 );
|
||||
}
|
||||
|
||||
AuxiliaryDisplayStruct setScale( Vector scale ) {
|
||||
this.scale = scale.clone();
|
||||
|
||||
return this;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,342 @@
|
||||
package com.aaaaahhhhhhh.bananapuncher714.mesh.physics;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.Set;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.block.data.BlockData;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.BlockDisplay;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
import org.bukkit.util.BoundingBox;
|
||||
import org.bukkit.util.Transformation;
|
||||
import org.bukkit.util.Vector;
|
||||
import org.joml.Matrix4f;
|
||||
import org.joml.Quaternionf;
|
||||
|
||||
import com.aaaaahhhhhhh.bananapuncher714.mesh.command.CommandParameters;
|
||||
import com.aaaaahhhhhhh.bananapuncher714.mesh.command.SubCommand;
|
||||
import com.aaaaahhhhhhh.bananapuncher714.mesh.command.executor.CommandExecutableMessage;
|
||||
import com.aaaaahhhhhhh.bananapuncher714.mesh.command.validator.InputValidatorDouble;
|
||||
import com.aaaaahhhhhhh.bananapuncher714.mesh.command.validator.InputValidatorInt;
|
||||
import com.aaaaahhhhhhh.bananapuncher714.mesh.command.validator.sender.SenderValidatorPlayer;
|
||||
import com.aaaaahhhhhhh.bananapuncher714.mesh.environment.EnvironmentHandler;
|
||||
import com.aaaaahhhhhhh.bananapuncher714.mesh.environment.MeshEnvironment;
|
||||
import com.aaaaahhhhhhh.bananapuncher714.mesh.environment.objects.ChunkLocation;
|
||||
import com.aaaaahhhhhhh.bananapuncher714.mesh.environment.objects.ChunkMesh;
|
||||
import com.aaaaahhhhhhh.bananapuncher714.mesh.environment.util.NmsUtil;
|
||||
import com.aaaaahhhhhhh.bananapuncher714.mesh.environment.util.NmsUtil.BlockShapeType;
|
||||
import com.jme3.bullet.PhysicsSpace;
|
||||
import com.jme3.bullet.PhysicsSpace.BroadphaseType;
|
||||
import com.jme3.bullet.collision.PhysicsCollisionEvent;
|
||||
import com.jme3.bullet.collision.PhysicsCollisionListener;
|
||||
import com.jme3.bullet.collision.PhysicsCollisionObject;
|
||||
import com.jme3.bullet.collision.shapes.BoxCollisionShape;
|
||||
import com.jme3.bullet.collision.shapes.CollisionShape;
|
||||
import com.jme3.bullet.collision.shapes.CompoundCollisionShape;
|
||||
import com.jme3.bullet.collision.shapes.MeshCollisionShape;
|
||||
import com.jme3.bullet.collision.shapes.SphereCollisionShape;
|
||||
import com.jme3.bullet.collision.shapes.infos.IndexedMesh;
|
||||
import com.jme3.bullet.objects.PhysicsBody;
|
||||
import com.jme3.bullet.objects.PhysicsRigidBody;
|
||||
import com.jme3.math.Quaternion;
|
||||
import com.jme3.math.Vector3f;
|
||||
|
||||
public class McMeshPhysicsPlugin extends JavaPlugin {
|
||||
private static final int TIME_STEPS = 5;
|
||||
private static final float SIMULATION_SPEED = 1f;
|
||||
|
||||
private MeshEnvironment environment;
|
||||
|
||||
private PhysicsSpace space;
|
||||
private boolean paused = false;
|
||||
private boolean teleportOnDeactivate = false;
|
||||
private float tick = 0;
|
||||
private Map< PhysicsCollisionObject, AuxiliaryDisplayStruct > linkedDisplays = new HashMap< PhysicsCollisionObject, AuxiliaryDisplayStruct >();
|
||||
private Set< PhysicsCollisionObject > active = new HashSet< PhysicsCollisionObject >();
|
||||
private Map< ChunkLocation, PhysicsRigidBody > chunks = new HashMap< ChunkLocation, PhysicsRigidBody >();
|
||||
|
||||
@Override
|
||||
public void onEnable() {
|
||||
registerCommands();
|
||||
|
||||
space = new PhysicsSpace( BroadphaseType.DBVT );
|
||||
space.setAccuracy( 1f / ( TIME_STEPS * 20f ) );
|
||||
|
||||
environment = new MeshEnvironment( this, new File( getDataFolder(), "environment" ) );
|
||||
environment.setHandler( new EnvironmentHandler() {
|
||||
@Override
|
||||
public void onMeshLoad( ChunkLocation location, ChunkMesh mesh ) {
|
||||
// Now, create a physics object from the chunk mesh
|
||||
final IndexedMesh nativeMesh = mesh.toIndexedMesh();
|
||||
// Enable quantized AABB compression... hopefully that doesn't mess with the accuracy or anything...
|
||||
final MeshCollisionShape meshCollision = new MeshCollisionShape( true, nativeMesh );
|
||||
|
||||
// Create a rigid body
|
||||
PhysicsRigidBody rigid = new PhysicsRigidBody( meshCollision, PhysicsBody.massForStatic );
|
||||
rigid.setPhysicsLocation( new Vector3f( location.getX() << 4, location.getWorld().getMinHeight(), location.getZ() << 4 ) );
|
||||
rigid.setPhysicsRotation( new Quaternion( 0, 0, 0, 1 ) );
|
||||
rigid.setKinematic( false );
|
||||
|
||||
Bukkit.getScheduler().runTask( McMeshPhysicsPlugin.this, () -> {
|
||||
space.addCollisionObject( rigid );
|
||||
|
||||
PhysicsRigidBody old = chunks.put( location, rigid );
|
||||
if ( old != null ) {
|
||||
space.remove( old );
|
||||
}
|
||||
} );
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onMeshUnload( ChunkLocation location, ChunkMesh mesh ) {
|
||||
space.removeCollisionObject( chunks.get( location ) );
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onMeshUpdate( ChunkLocation location, ChunkMesh mesh ) {
|
||||
// Now, create a physics object from the chunk mesh
|
||||
final IndexedMesh nativeMesh = mesh.toIndexedMesh();
|
||||
// Enable quantized AABB compression... hopefully that doesn't mess with the accuracy or anything...
|
||||
final MeshCollisionShape meshCollision = new MeshCollisionShape( true, nativeMesh );
|
||||
|
||||
// Create a rigid body
|
||||
PhysicsRigidBody rigid = new PhysicsRigidBody( meshCollision, PhysicsBody.massForStatic );
|
||||
rigid.setPhysicsLocation( new Vector3f( location.getX() << 4, location.getWorld().getMinHeight(), location.getZ() << 4 ) );
|
||||
rigid.setPhysicsRotation( new Quaternion( 0, 0, 0, 1 ) );
|
||||
rigid.setKinematic( false );
|
||||
|
||||
Bukkit.getScheduler().runTask( McMeshPhysicsPlugin.this, () -> {
|
||||
space.addCollisionObject( rigid );
|
||||
|
||||
PhysicsRigidBody old = chunks.put( location, rigid );
|
||||
if ( old != null ) {
|
||||
space.remove( old );
|
||||
}
|
||||
} );
|
||||
|
||||
space.activateAll( true );
|
||||
}
|
||||
} );
|
||||
|
||||
Bukkit.getScheduler().runTaskTimer( this, () -> {
|
||||
if ( !paused ) {
|
||||
// Update the real world with the physics space objects
|
||||
space.distributeEvents();
|
||||
space.update( tick, TIME_STEPS );
|
||||
|
||||
for ( Iterator< Entry< PhysicsCollisionObject, AuxiliaryDisplayStruct > > it = linkedDisplays.entrySet().iterator(); it.hasNext(); ) {
|
||||
Entry< PhysicsCollisionObject, AuxiliaryDisplayStruct > entry = it.next();
|
||||
PhysicsCollisionObject obj = entry.getKey();
|
||||
AuxiliaryDisplayStruct disp = entry.getValue();
|
||||
|
||||
if ( !disp.display.isValid() ) {
|
||||
it.remove();
|
||||
space.removeCollisionObject( obj );
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
Location displayLocation = disp.display.getLocation();
|
||||
|
||||
// Do not calculate for this object if it is inactive
|
||||
if ( !obj.isActive() ) {
|
||||
if ( teleportOnDeactivate && active.remove( obj ) ) {
|
||||
// Newly deactivated object, teleport the display to the actual position to prevent the translation from getting too large
|
||||
|
||||
Transformation currentTransformation = disp.display.getTransformation();
|
||||
org.joml.Vector3f trans = currentTransformation.getTranslation();
|
||||
|
||||
displayLocation.add( trans.x(), trans.y(), trans.z() );
|
||||
disp.display.teleport( displayLocation );
|
||||
|
||||
Transformation newTrans = new Transformation( new org.joml.Vector3f( 0, 0, 0 ), currentTransformation.getLeftRotation(), currentTransformation.getScale(), currentTransformation.getRightRotation() );
|
||||
disp.display.setInterpolationDelay( 0 );
|
||||
disp.display.setInterpolationDuration( 0 );
|
||||
disp.display.setTransformation( newTrans );
|
||||
}
|
||||
|
||||
continue;
|
||||
} else {
|
||||
active.add( obj );
|
||||
}
|
||||
|
||||
Vector3f location = obj.getPhysicsLocation();
|
||||
Quaternion rotation = new Quaternion();
|
||||
obj.getPhysicsRotation( rotation );
|
||||
|
||||
Vector offsetVec = disp.offset;
|
||||
Vector scale = disp.scale;
|
||||
Matrix4f transformRot = new Matrix4f();
|
||||
transformRot.set( new Quaternionf( rotation.getX(), rotation.getY(), rotation.getZ(), rotation.getW() ) );
|
||||
|
||||
transformRot.mul( 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, - ( float ) offsetVec.getX(), - ( float ) offsetVec.getY(), - ( float ) offsetVec.getZ(), 1 );
|
||||
transformRot.mul( ( float ) scale.getX(), 0, 0, 0, 0, ( float ) scale.getY(), 0, 0, 0, 0, ( float ) scale.getZ(), 0, 0, 0, 0, 1 );
|
||||
Matrix4f transMat = new Matrix4f( 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, ( float ) ( location.getX() - displayLocation.getX() ), ( float ) ( location.getY() - displayLocation.getY() ), ( float ) ( location.getZ() - displayLocation.getZ() ), 1 );
|
||||
|
||||
disp.display.setInterpolationDelay( 0 );
|
||||
disp.display.setInterpolationDuration( 2 );
|
||||
disp.display.setTransformationMatrix( transMat.mul( transformRot ) );
|
||||
}
|
||||
|
||||
tick += SIMULATION_SPEED / 20f;
|
||||
}
|
||||
}, 0, 1 );
|
||||
}
|
||||
|
||||
private void registerCommands() {
|
||||
new SubCommand( "minie" )
|
||||
.addSenderValidator( new SenderValidatorPlayer() )
|
||||
.add( new SubCommand( "spawn" )
|
||||
.add( new SubCommand( new InputValidatorDouble( 0.01, 100 ) )
|
||||
.defaultTo( this::spawnBlock ) )
|
||||
.defaultTo( this::spawnBlock ) )
|
||||
.add( new SubCommand( "toggle" )
|
||||
.defaultTo( ( sender, args, params ) -> {
|
||||
paused = !paused;
|
||||
} ) )
|
||||
.add( new SubCommand( "teleport" )
|
||||
.defaultTo( ( sender, args, params ) -> {
|
||||
teleportOnDeactivate = !teleportOnDeactivate;
|
||||
} ) )
|
||||
.add( new SubCommand( "impulse" )
|
||||
.add( new SubCommand( new InputValidatorInt() )
|
||||
.defaultTo( ( sender, args, params ) -> {
|
||||
Player player = ( Player ) sender;
|
||||
Location loc = player.getLocation();
|
||||
|
||||
impulse( new Vector3f( ( float ) loc.getX(), ( float ) loc.getY(), ( float ) loc.getZ() ), params.getLast( int.class ) );
|
||||
} ) )
|
||||
.defaultTo( ( sender, args, params ) -> {
|
||||
Player player = ( Player ) sender;
|
||||
Location loc = player.getLocation();
|
||||
|
||||
impulse( new Vector3f( ( float ) loc.getX(), ( float ) loc.getY(), ( float ) loc.getZ() ), 15f );
|
||||
} ) )
|
||||
.defaultTo( new CommandExecutableMessage( "An argument must be provided" ) )
|
||||
.whenUnknown( new CommandExecutableMessage( "Unknown argument" ) )
|
||||
.applyTo( getCommand( "minie" ) );
|
||||
}
|
||||
|
||||
private void impulse( Vector3f location, float power ) {
|
||||
PhysicsRigidBody rigid = new PhysicsRigidBody( new SphereCollisionShape( 20 ), 1f );
|
||||
rigid.setPhysicsLocation( location );
|
||||
space.contactTest( rigid, new PhysicsCollisionListener() {
|
||||
@Override
|
||||
public void collision( PhysicsCollisionEvent event ) {
|
||||
if ( event.getObjectB() instanceof PhysicsRigidBody ) {
|
||||
PhysicsRigidBody objB = ( PhysicsRigidBody ) event.getObjectB();
|
||||
|
||||
Vector3f rLoc = objB.getPhysicsLocation();
|
||||
Vector3f to = rLoc.subtract( ( float ) location.getX(), ( float ) location.getY(), ( float ) location.getZ() );
|
||||
|
||||
float distSq = to.lengthSquared();
|
||||
|
||||
if ( distSq != 0 ) {
|
||||
if ( power / distSq > 1 ) {
|
||||
objB.applyCentralImpulse( to.normalize().mult( power / distSq ) );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
} );
|
||||
}
|
||||
|
||||
private void spawnBlock( final CommandSender sender, final String[] args, final CommandParameters params ) {
|
||||
Player player = ( Player ) sender;
|
||||
Location loc = player.getLocation();
|
||||
|
||||
// Optional scale
|
||||
double scale = 1;
|
||||
if ( params.size() > 2 ) {
|
||||
scale = params.getFirst( double.class );
|
||||
}
|
||||
|
||||
loc.setPitch( 0 );
|
||||
loc.setDirection( new Vector( 0, 0, 0 ) );
|
||||
|
||||
BlockData displayData;
|
||||
ItemStack item = player.getInventory().getItemInMainHand();
|
||||
if ( item != null && item.getType() != Material.AIR && item.getType().isBlock() ) {
|
||||
displayData = item.getType().createBlockData();
|
||||
} else {
|
||||
displayData = Material.TNT.createBlockData();
|
||||
}
|
||||
|
||||
BoundingBox[] boxes = NmsUtil.convertFrom( NmsUtil.getShape( displayData, null, BlockShapeType.SHAPE ) );
|
||||
Vector blockCenter = calculateCenter( boxes );
|
||||
|
||||
CollisionShape box;
|
||||
if ( boxes.length == 0 ) {
|
||||
player.sendMessage( "No bounding box detected! Wrong method?" );
|
||||
return;
|
||||
}
|
||||
|
||||
if ( boxes.length == 1 ) {
|
||||
Vector min = boxes[ 0 ].getMin();
|
||||
Vector center = boxes[ 0 ].getCenter().subtract( min );
|
||||
box = new BoxCollisionShape( ( float ) center.getX(), ( float ) center.getY(), ( float ) center.getZ() );
|
||||
} else {
|
||||
CompoundCollisionShape compound = new CompoundCollisionShape();
|
||||
for ( BoundingBox aabb : boxes ) {
|
||||
Vector min = aabb.getMin();
|
||||
Vector center = aabb.getCenter();
|
||||
Vector half = center.clone().subtract( min );
|
||||
center.subtract( blockCenter );
|
||||
CollisionShape subShape = new BoxCollisionShape( ( float ) half.getX(), ( float ) half.getY(), ( float ) half.getZ() );
|
||||
compound.addChildShape( subShape, ( float ) center.getX(), ( float ) center.getY(), ( float ) center.getZ() );
|
||||
}
|
||||
box = compound;
|
||||
}
|
||||
|
||||
box.setScale( ( float ) scale );
|
||||
|
||||
PhysicsRigidBody rigid = new PhysicsRigidBody( box, 1f );
|
||||
|
||||
// Since the center of the rigid body and the box shape are different, we need to offset the location
|
||||
rigid.setPhysicsLocation( new Vector3f( ( float ) ( loc.getX() + blockCenter.getX() ), ( float ) ( loc.getY() + blockCenter.getY() ), ( float ) ( loc.getZ() + blockCenter.getZ() ) ) );
|
||||
|
||||
// Convert the display direction vector to a quaternion
|
||||
rigid.setPhysicsRotation( new Quaternion( 0, 0, 0, 1 ) );
|
||||
|
||||
rigid.setMass( rigid.getMass() * ( float ) ( scale * scale ) );
|
||||
|
||||
space.addCollisionObject( rigid );
|
||||
|
||||
// Create the display
|
||||
BlockDisplay display = loc.getWorld().spawn( loc, BlockDisplay.class, d -> {
|
||||
d.setBlock( displayData );
|
||||
d.setViewRange( 1000 );
|
||||
} );
|
||||
|
||||
linkedDisplays.put( rigid, new AuxiliaryDisplayStruct( display, blockCenter.multiply( scale ) ).setScale( new Vector( scale, scale, scale ) ) );
|
||||
|
||||
player.sendMessage( "Created display" );
|
||||
}
|
||||
|
||||
private static Vector calculateCenter( BoundingBox[] boxes ) {
|
||||
Vector center = new Vector( 0, 0, 0 );
|
||||
// TODO Throw error, probably
|
||||
if ( boxes.length == 0 ) {
|
||||
return center;
|
||||
}
|
||||
|
||||
double totalVolume = 0;
|
||||
for ( BoundingBox box : boxes ) {
|
||||
Vector mid = box.getCenter();
|
||||
center.add( mid.multiply( box.getVolume() ) );
|
||||
totalVolume += box.getVolume();
|
||||
}
|
||||
return center.multiply( 1 / totalVolume );
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
15
plugin/physics/src/main/resources/plugin.yml
Normal file
15
plugin/physics/src/main/resources/plugin.yml
Normal file
@@ -0,0 +1,15 @@
|
||||
name: MC-Mesh-Physics
|
||||
main: com.aaaaahhhhhhh.bananapuncher714.mesh.physics.McMeshPhysicsPlugin
|
||||
version: 0.0.1
|
||||
description: BulletJME and MC-Mesh integration
|
||||
author: BananaPuncher714
|
||||
api-version: 1.13
|
||||
depend: [ "Mc-Mesh-Environment" ]
|
||||
#libraries:
|
||||
#- com.github.stephengold:Minie:7.5.0
|
||||
|
||||
commands:
|
||||
minie:
|
||||
description: Main Minie command
|
||||
aliases: []
|
||||
permission: minie
|
||||
Reference in New Issue
Block a user