-
-
Save McJty/9df1b0e3aa9a48442bf4eb57f3556630 to your computer and use it in GitHub Desktop.
Physics door test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
lateinit var bulletAppState: BulletAppState | |
lateinit var playerControl: PlayerControl | |
// For grabbing | |
private var joint: Constraint? = null | |
private var handle: RigidBodyControl? = null | |
private var grabDistance = 0f | |
private var highlighted: Geometry? = null | |
private var grabbing: Geometry? = null | |
private fun startGame() { | |
bulletAppState = BulletAppState() | |
stateManager.attach(bulletAppState) | |
bulletAppState.getPhysicsSpace().accuracy = 1f/120f | |
playerControl = PlayerControl(camera, bulletAppState) | |
} | |
override fun onAction(binding: String, isPressed: Boolean, tpf: Float) { | |
when (binding) { | |
"Grab" -> grab(isPressed) | |
... | |
} | |
} | |
private fun grab(pressed: Boolean) { | |
if (pressed) { | |
startGrab() | |
} else { | |
releaseGrab() | |
} | |
} | |
private fun releaseGrab() { | |
if (joint != null) { | |
joint?.bodyA?.removeJoint(joint) | |
joint?.bodyB?.removeJoint(joint) | |
bulletAppState.physicsSpace.remove(joint) | |
bulletAppState.physicsSpace.remove(handle) | |
joint = null | |
grabbing = null | |
} | |
} | |
private fun startGrab() { | |
val pair = findHovering() | |
if (pair != null) { | |
val (result, gob) = pair | |
if (gob.mesh?.flags?.mass ?: 0f > 0f) { | |
grabDistance = result.distance | |
val contactPoint = gob.getGeom().worldToLocal(result.contactPoint, null) | |
grabbing = result.geometry | |
if (handle == null) { | |
handle = RigidBodyControl(EmptyShape(true), 4.0f).also { | |
it.isKinematic = true | |
it.rollingFriction = 1000.0f | |
it.contactDamping = 0.0f | |
} | |
} | |
bulletAppState.physicsSpace.add(handle) | |
if (joint == null) { | |
joint = New6Dof(handle, gob.control, Vector3f.ZERO, contactPoint, Matrix3f.IDENTITY, Matrix3f.IDENTITY, RotationOrder.XYZ).also { | |
it.setStiffness(PhysicsSpace.AXIS_X + 3, 0.3f, true) | |
it.setStiffness(PhysicsSpace.AXIS_Y + 3, 0.3f, true) | |
it.setStiffness(PhysicsSpace.AXIS_Z + 3, 0.3f, true) | |
bulletAppState.physicsSpace.add(it) | |
} | |
} | |
updateGrab() | |
} | |
} | |
} | |
override fun simpleUpdate(tpf: Float) { | |
... | |
if (joint != null && handle != null) { | |
updateGrab() | |
} | |
... | |
} | |
private fun updateGrab() { | |
val pos = cam.location.add(cam.getDirection().mult(grabDistance)) | |
handle?.physicsLocation = pos | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment