Physics fix

This commit is contained in:
austin 2023-12-02 22:19:17 -05:00
parent dc0a61c0dc
commit 174126912e
8 changed files with 17 additions and 14 deletions

View File

@ -267,7 +267,7 @@
"collidable" : { "collidable" : {
"type" : "CYLINDER", "type" : "CYLINDER",
"dimension1" : 0.1, "dimension1" : 0.1,
"dimension2" : 0.9, "dimension2" : 0.8,
"dimension3" : 0.1, "dimension3" : 0.1,
"rotX": 0, "rotX": 0,
"rotY": 0, "rotY": 0,

View File

@ -26,6 +26,7 @@ The big case for this engine is the main physics system. The goal is to provide
- All geometries are aligned along z by default in the library (ie your cylinders will be on their sides) - All geometries are aligned along z by default in the library (ie your cylinders will be on their sides)
- All functions that transform the scale of a DBody only transform the first shape within the rigid body. This should likely eventually be updated to support multiple shapes in a body. - All functions that transform the scale of a DBody only transform the first shape within the rigid body. This should likely eventually be updated to support multiple shapes in a body.
- Furthermore, these transform functions require the CollidableTemplate to modify scale. Ode4J doesn't provide a "scale" function, instead it lets you change the core params. To scale, must know the original size.

View File

@ -10,7 +10,7 @@ TODO
## Major Usage Notes ## Major Usage Notes
- Global pretransforms are applied to the global inverse bind pose matrix

View File

@ -65,7 +65,7 @@ public class CollisionBodyCreation {
public static DBody createCylinderBody(CollisionEngine collisionEngine, double radius, double length){ public static DBody createCylinderBody(CollisionEngine collisionEngine, double radius, double length){
DCylinder geom = collisionEngine.createCylinderGeom(radius,length); DCylinder geom = collisionEngine.createCylinderGeom(radius,length);
DBody returnBody = collisionEngine.createDBody(geom); DBody returnBody = collisionEngine.createDBody(geom);
geom.setOffsetRotation(AXIS_CORRECTION_MATRIX); geom.setOffsetRotation(AXIS_CORRECTION_MATRIX); //ode4j required geom to already be on body before rotating for some reason
return returnBody; return returnBody;
} }

View File

@ -52,6 +52,7 @@ import electrosphere.entity.EntityDataStrings;
import electrosphere.entity.EntityUtils; import electrosphere.entity.EntityUtils;
import electrosphere.entity.state.collidable.Impulse; import electrosphere.entity.state.collidable.Impulse;
import electrosphere.entity.types.hitbox.HitboxData; import electrosphere.entity.types.hitbox.HitboxData;
import electrosphere.game.data.collidable.CollidableTemplate;
import electrosphere.logger.LoggerInterface; import electrosphere.logger.LoggerInterface;
/** /**
@ -647,17 +648,17 @@ public class CollisionEngine {
* @param rotation The rotation * @param rotation The rotation
* @param scale The scale * @param scale The scale
*/ */
protected void setBodyTransform(DBody body, Vector3d position, Quaterniond rotation, Vector3d scale){ protected void setBodyTransform(DBody body, CollidableTemplate template, Vector3d position, Quaterniond rotation, Vector3d scale){
spaceLock.acquireUninterruptibly(); spaceLock.acquireUninterruptibly();
body.setPosition(position.x, position.y, position.z); body.setPosition(position.x, position.y, position.z);
body.setQuaternion(PhysicsUtils.jomlQuatToOdeQuat(rotation)); body.setQuaternion(PhysicsUtils.jomlQuatToOdeQuat(rotation));
DGeom firstGeom = body.getFirstGeom(); DGeom firstGeom = body.getFirstGeom();
if(firstGeom instanceof DCylinder){ if(firstGeom instanceof DCylinder){
((DCylinder)firstGeom).setParams(scale.x,scale.y); ((DCylinder)firstGeom).setParams(template.getDimension1() * scale.x,template.getDimension2() * scale.y);
} else if(firstGeom instanceof DBox){ } else if(firstGeom instanceof DBox){
((DBox)firstGeom).setLengths(scale.x,scale.y,scale.z); ((DBox)firstGeom).setLengths(template.getDimension1() * scale.x,template.getDimension2() * scale.y,template.getDimension3() * scale.z);
} else if(firstGeom instanceof DCapsule){ } else if(firstGeom instanceof DCapsule){
((DCapsule)firstGeom).setParams(scale.x,scale.y); ((DCapsule)firstGeom).setParams(template.getDimension1() * scale.x,template.getDimension2() * scale.y);
} }
spaceLock.release(); spaceLock.release();
} }

View File

@ -46,10 +46,9 @@ public class PhysicsEntityUtils {
collidable = new Collidable(rVal, Collidable.TYPE_CREATURE); collidable = new Collidable(rVal, Collidable.TYPE_CREATURE);
ClientCollidableTree tree = new ClientCollidableTree(rVal,collidable,rigidBody); ClientCollidableTree tree = new ClientCollidableTree(rVal,collidable,rigidBody);
rVal.putData(EntityDataStrings.PHYSICS_COLLISION_BODY, rigidBody); rVal.putData(EntityDataStrings.PHYSICS_COLLISION_BODY, rigidBody);
Matrix4d offsetTransform = new Matrix4d().translationRotateScale( Matrix4d offsetTransform = new Matrix4d().translationRotate(
physicsTemplate.getOffsetX(), physicsTemplate.getOffsetY(), physicsTemplate.getOffsetZ(), //translate physicsTemplate.getOffsetX(), physicsTemplate.getOffsetY(), physicsTemplate.getOffsetZ(), //translate
physicsTemplate.getRotX(), physicsTemplate.getRotY(), physicsTemplate.getRotZ(), physicsTemplate.getRotW(), //rotate physicsTemplate.getRotX(), physicsTemplate.getRotY(), physicsTemplate.getRotZ(), physicsTemplate.getRotW() //rotate
1, 1, 1 //scale
); );
rVal.putData(EntityDataStrings.PHYSICS_COLLISION_BODY_OFFSET, new Vector3f(physicsTemplate.getOffsetX(),physicsTemplate.getOffsetY(),physicsTemplate.getOffsetZ())); rVal.putData(EntityDataStrings.PHYSICS_COLLISION_BODY_OFFSET, new Vector3f(physicsTemplate.getOffsetX(),physicsTemplate.getOffsetY(),physicsTemplate.getOffsetZ()));
rVal.putData(EntityDataStrings.PHYSICS_COLLISION_BODY_TRANSFORM, offsetTransform); rVal.putData(EntityDataStrings.PHYSICS_COLLISION_BODY_TRANSFORM, offsetTransform);

View File

@ -7,6 +7,8 @@ import org.ode4j.math.DQuaternionC;
import org.ode4j.ode.DBody; import org.ode4j.ode.DBody;
import org.ode4j.ode.DMass; import org.ode4j.ode.DMass;
import electrosphere.game.data.collidable.CollidableTemplate;
/** /**
* Utilities for leveraging the collision system to perform physics * Utilities for leveraging the collision system to perform physics
*/ */
@ -76,8 +78,8 @@ public class PhysicsUtils {
* @param scale The scale * @param scale The scale
* @param body The body * @param body The body
*/ */
public static void setRigidBodyTransform(CollisionEngine collisionEngine, Vector3d position, Quaterniond rotation, Vector3d scale, DBody body){ public static void setRigidBodyTransform(CollisionEngine collisionEngine, CollidableTemplate template, Vector3d position, Quaterniond rotation, Vector3d scale, DBody body){
collisionEngine.setBodyTransform(body, position, rotation, scale); collisionEngine.setBodyTransform(body, template, position, rotation, scale);
} }
/** /**

View File

@ -217,10 +217,10 @@ public class ClientCollidableTree implements BehaviorTree {
Vector4d rigidBodyPositionRaw = parentTransform.transform(new Vector4d(0,0,0,1)); Vector4d rigidBodyPositionRaw = parentTransform.transform(new Vector4d(0,0,0,1));
Vector3d rigidBodyPosition = new Vector3d(rigidBodyPositionRaw.x,rigidBodyPositionRaw.y,rigidBodyPositionRaw.z); Vector3d rigidBodyPosition = new Vector3d(rigidBodyPositionRaw.x,rigidBodyPositionRaw.y,rigidBodyPositionRaw.z);
Quaterniond rigidBodyRotation = parentTransform.getUnnormalizedRotation(new Quaterniond()).normalize(); Quaterniond rigidBodyRotation = parentTransform.getUnnormalizedRotation(new Quaterniond()).normalize();
Vector3d rigidBodyScaleRaw = parentTransform.getScale(new Vector3d()); Vector3d rigidBodyScale = new Vector3d(parentScale);
Vector3d rigidBodyScale = new Vector3d(rigidBodyScaleRaw.x,rigidBodyScaleRaw.y,rigidBodyScaleRaw.z);
PhysicsUtils.setRigidBodyTransform( PhysicsUtils.setRigidBodyTransform(
Globals.clientSceneWrapper.getCollisionEngine(), Globals.clientSceneWrapper.getCollisionEngine(),
template,
rigidBodyPosition, rigidBodyPosition,
rigidBodyRotation, rigidBodyRotation,
rigidBodyScale, rigidBodyScale,