actor now moves like before, but still has a bug where keep 'pushing' the wall while suspended, gravity only accumulates (veloY increases) and not exert any force (hitbox does not move where it should).

Former-commit-id: 8844902b3395acf645b769d299f9459b27810675
Former-commit-id: 55a72ed87a1c56e4a8367bd3a0bc301b12edae2a
This commit is contained in:
Song Minjae
2016-05-03 14:05:27 +09:00
parent 6171b44170
commit 1350ee0baf
3 changed files with 97 additions and 68 deletions

View File

@@ -12,10 +12,10 @@
"speed" : 3.0, "speed" : 3.0,
"speedmult" : [100,100,100,100,100,100,100], "speedmult" : [100,100,100,100,100,100,100],
"jumppower" : 5, "jumppower" : 4.3,
"jumppowermult" : [100,100,100,100,100,100,100], "jumppowermult" : [100,100,100,100,100,100,100],
"scale" : 1, "scale" : 1.0,
"scalemult" : [100,100,100,100,100,100,100], "scalemult" : [100,100,100,100,100,100,100],
"encumbrance" : 1000, "encumbrance" : 1000,

View File

@@ -74,10 +74,19 @@ open class ActorWithBody constructor() : Actor(), Visible {
var scale: Double var scale: Double
get() = actorValue.getAsDouble(AVKey.SCALE) ?: 1.0 get() = actorValue.getAsDouble(AVKey.SCALE) ?: 1.0
set(value) = actorValue.set(AVKey.SCALE, value) set(value) = actorValue.set(AVKey.SCALE, value)
@Transient val MASS_LOWEST = 0.1 // Kilograms
var mass: Double var mass: Double
get() = actorValue.getAsDouble(AVKey.BASEMASS) ?: MASS_DEFAULT * Math.pow(scale, 3.0) get() = actorValue.getAsDouble(AVKey.BASEMASS) ?: MASS_DEFAULT * Math.pow(scale, 3.0)
set(value) = actorValue.set(AVKey.BASEMASS, value) set(value) {
@Transient private val MASS_LOWEST = 2.0 if (value <= 0)
throw IllegalArgumentException("mass cannot be less than or equal to zero.")
else if (value < MASS_LOWEST) {
println("[ActorWithBody] input too low; assigning 0.1 instead.")
actorValue[AVKey.BASEMASS] = MASS_LOWEST
}
actorValue[AVKey.BASEMASS] = value
}
/** Valid range: [0, 1] */ /** Valid range: [0, 1] */
var elasticity = 0.0 var elasticity = 0.0
set(value) { set(value) {
@@ -155,7 +164,9 @@ open class ActorWithBody constructor() : Actor(), Visible {
@Transient val DYNAMIC = 2 @Transient val DYNAMIC = 2
@Transient val STATIC = 3 @Transient val STATIC = 3
private val SLEEP_THRE = 0.05 private val SLEEP_THRE = 0.125
private val CCD_THRE = 1.0
private val CCD_TICK = 0.125
init { init {
@@ -237,7 +248,7 @@ open class ActorWithBody constructor() : Actor(), Visible {
if (!physSleep) { if (!physSleep) {
// Set 'next' position (hitbox) to fiddle with // Set 'next' position (hitbox) to fiddle with
updateNextHitboxFromVelo() setNewNextHitbox()
// if not horizontally moving then ... // if not horizontally moving then ...
//if (Math.abs(veloX) < 0.5) { // fix for special situations (see fig. 1 at the bottom of the source) //if (Math.abs(veloX) < 0.5) { // fix for special situations (see fig. 1 at the bottom of the source)
@@ -248,8 +259,8 @@ open class ActorWithBody constructor() : Actor(), Visible {
// compensate for colliding // compensate for colliding
//updateHorizontalCollision() //updateHorizontalCollision()
//updateVerticalCollision() //updateVerticalCollision()
adjustHit()
applyNormalForce() applyNormalForce()
adjustHit()
setHorizontalFriction() setHorizontalFriction()
if (isPlayerNoClip) setVerticalFriction() if (isPlayerNoClip) setVerticalFriction()
@@ -268,11 +279,10 @@ open class ActorWithBody constructor() : Actor(), Visible {
/** /**
* Apply gravitation to the every falling body (unless not levitating) * Apply gravitation to the every falling body (unless not levitating)
*
* Apply only if not grounded; normal force is not implemented (and redundant) * Apply only if not grounded; normal force is not implemented (and redundant)
* so we manually reset G to zero (not applying G. force) if grounded. * so we manually reset G to zero (not applying G. force) if grounded.
*/ */
// FIXME abnormal jump behaviour if mass < 2, same thing happens if mass == 0 (but zero mass is invalid anyway).
private fun applyGravitation() { private fun applyGravitation() {
if (!grounded) { if (!grounded) {
/** /**
@@ -476,59 +486,81 @@ open class ActorWithBody constructor() : Actor(), Visible {
* nextHitbox must NOT altered before this method is called! * nextHitbox must NOT altered before this method is called!
*/ */
private fun adjustHit() { private fun adjustHit() {
val delta: Vector2 = Vector2(hitbox.toVector() - nextHitbox.toVector()) // we need to traverse back, so may as well negate at the first place if (!isNoCollideWorld){
delta *= SLEEP_THRE // CCD delta val delta: Vector2 = Vector2(hitbox.toVector() - nextHitbox.toVector()) // we need to traverse back, so may as well negate at the first place
val ccdDelta = delta.setMagnitude(CCD_TICK)
while (isColliding(CONTACT_AREA_LEFT) || isColliding(CONTACT_AREA_RIGHT) while (isColliding(CONTACT_AREA_LEFT) || isColliding(CONTACT_AREA_RIGHT)
|| isColliding(CONTACT_AREA_TOP) || isColliding(CONTACT_AREA_BOTTOM) || isColliding(CONTACT_AREA_TOP) || isColliding(CONTACT_AREA_BOTTOM)
) { ) {
// CCD to the 'delta' // while still colliding, CCD to the 'delta'
nextHitbox.translate(delta) nextHitbox.translate(ccdDelta)
}
} }
} }
private fun applyNormalForce() { private fun applyNormalForce() {
if (!isNoCollideWorld) { if (!isNoCollideWorld) {
if (gravitation.y != 0.0) { // axis Y
if (veloY > SLEEP_THRE && isColliding(CONTACT_AREA_BOTTOM, 0, 1)) { if (veloY >= 0) { // check downward
veloY = 0.0 if (isColliding(CONTACT_AREA_BOTTOM)) { // the ground has dug into the body
grounded = (gravitation.y > 0) veloY = 0.0 // reset veloY, simulating normal force
elasticReflectY()
grounded = true
} }
else if (veloY < -SLEEP_THRE && isColliding(CONTACT_AREA_TOP, 0, -1)) { else if (isColliding(CONTACT_AREA_BOTTOM, 0, 1)) { // the actor is standing ON the ground
veloY = 0.0 veloY = 0.0 // reset veloY, simulating normal force
grounded = (gravitation.y < 0) elasticReflectY()
grounded = true
} }
else { else { // the actor is not grounded at all
grounded = false grounded = false
} }
} }
else { else if (veloY < 0) { // check upward
if ((veloY > SLEEP_THRE && isColliding(CONTACT_AREA_BOTTOM, 0, 1)) grounded = false
|| (veloY < -SLEEP_THRE && isColliding(CONTACT_AREA_TOP, 0, -1)) if (isColliding(CONTACT_AREA_TOP)) { // the ceiling has dug into the body
) { veloY = 0.0 // reset veloY, simulating normal force
veloY = 0.0 elasticReflectY()
}
else if (isColliding(CONTACT_AREA_TOP, 0, -1)) { // the actor is touching the ceiling
veloY = 0.0 // reset veloY, simulating normal force
elasticReflectY() // reflect on ceiling, for reversed gravity
}
else { // the actor is not grounded at all
} }
} }
// axis X
if (gravitation.x != 0.0) { if (veloX >= 0.5) { // check right
if (veloX > SLEEP_THRE && isColliding(CONTACT_AREA_BOTTOM, 1, 0)) { if (isColliding(CONTACT_AREA_RIGHT) && !isColliding(CONTACT_AREA_LEFT)) {
veloX = 0.0 // the actor is embedded to the wall
grounded = (gravitation.x > 0) veloX = 0.0 // reset veloX, simulating normal force
elasticReflectX()
} }
else if (veloX < -SLEEP_THRE && isColliding(CONTACT_AREA_TOP, -1, 0)) { else if (isColliding(CONTACT_AREA_RIGHT, 2, 0) && !isColliding(CONTACT_AREA_LEFT, 0, 0)) { // offset by +1, to fix directional quarks
veloY = 0.0 // the actor is touching the wall
grounded = (gravitation.x < 0) veloX = 0.0 // reset veloX, simulating normal force
elasticReflectX()
}
else {
}
}
else if (veloX <= -0.5) { // check left
// System.out.println("collidingleft");
if (isColliding(CONTACT_AREA_LEFT) && !isColliding(CONTACT_AREA_RIGHT)) {
// the actor is embedded to the wall
veloX = 0.0 // reset veloX, simulating normal force
elasticReflectX()
}
else if (isColliding(CONTACT_AREA_LEFT, -1, 0) && !isColliding(CONTACT_AREA_RIGHT, 1, 0)) {
// the actor is touching the wall
veloX = 0.0 // reset veloX, simulating normal force
elasticReflectX()
} }
else { else {
grounded = false
} }
} }
else { else {
if ((veloX > SLEEP_THRE && isColliding(CONTACT_AREA_RIGHT, 1, 0))
|| (veloX < -SLEEP_THRE && isColliding(CONTACT_AREA_LEFT, -1, 0))
) {
veloX = 0.0
}
} }
} }
} }
@@ -711,7 +743,7 @@ open class ActorWithBody constructor() : Actor(), Visible {
clampW(nextHitbox.pointedX), clampH(nextHitbox.pointedY)) clampW(nextHitbox.pointedX), clampH(nextHitbox.pointedY))
} }
private fun updateNextHitboxFromVelo() { private fun setNewNextHitbox() {
nextHitbox.set( nextHitbox.set(
(hitbox.posX + veloX) (hitbox.posX + veloX)
@@ -724,13 +756,15 @@ open class ActorWithBody constructor() : Actor(), Visible {
private fun updateHitboxX() { private fun updateHitboxX() {
hitbox.setDimension( hitbox.setDimension(
nextHitbox.width, nextHitbox.height) nextHitbox.width, nextHitbox.height)
hitbox.setPositionX(nextHitbox.posX) //if (nextHitbox.posX - hitbox.posX > SLEEP_THRE.abs())
hitbox.setPositionX(nextHitbox.posX)
} }
private fun updateHitboxY() { private fun updateHitboxY() {
hitbox.setDimension( hitbox.setDimension(
nextHitbox.width, nextHitbox.height) nextHitbox.width, nextHitbox.height)
hitbox.setPositionY(nextHitbox.posY) //if (nextHitbox.posY - hitbox.posY > SLEEP_THRE.abs())
hitbox.setPositionY(nextHitbox.posY)
} }
override fun drawGlow(gc: GameContainer, g: Graphics) { override fun drawGlow(gc: GameContainer, g: Graphics) {

View File

@@ -267,10 +267,9 @@ class Vector2 {
* * * *
* @return [Vector2] this vector * @return [Vector2] this vector
*/ */
fun set(vector: Vector2): Vector2 { fun set(vector: Vector2) {
this.x = vector.x this.x = vector.x
this.y = vector.y this.y = vector.y
return this
} }
/** /**
@@ -281,10 +280,9 @@ class Vector2 {
* * * *
* @return [Vector2] this vector * @return [Vector2] this vector
*/ */
fun set(x: Double, y: Double): Vector2 { fun set(x: Double, y: Double) {
this.x = x this.x = x
this.y = y this.y = y
return this
} }
/** /**
@@ -325,21 +323,19 @@ class Vector2 {
fun setMagnitude(magnitude: Double): Vector2 { fun setMagnitude(magnitude: Double): Vector2 {
// check the given magnitude // check the given magnitude
if (Math.abs(magnitude) <= Epsilon.E) { if (Math.abs(magnitude) <= Epsilon.E) {
this.x = 0.0 return Vector2(0.0, 0.0)
this.y = 0.0
return this
} }
// is this vector a zero vector? // is this vector a zero vector?
if (this.isZero) { if (this.isZero) {
return this return Vector2(0.0, 0.0)
} }
// get the magnitude // get the magnitude
var mag = Math.sqrt(this.x * this.x + this.y * this.y) var mag = Math.sqrt(this.x * this.x + this.y * this.y)
// normalize and multiply by the new magnitude // normalize and multiply by the new magnitude
mag = magnitude / mag mag = magnitude / mag
this.x *= mag val newX = this.x * mag
this.y *= mag val newY = this.y * mag
return this return Vector2(newX, newY)
} }
/** /**
@@ -359,9 +355,9 @@ class Vector2 {
fun setDirection(angle: Double): Vector2 { fun setDirection(angle: Double): Vector2 {
//double magnitude = Math.hypot(this.x, this.y); //double magnitude = Math.hypot(this.x, this.y);
val magnitude = Math.sqrt(this.x * this.x + this.y * this.y) val magnitude = Math.sqrt(this.x * this.x + this.y * this.y)
this.x = magnitude * Math.cos(angle) val newX = magnitude * Math.cos(angle)
this.y = magnitude * Math.sin(angle) val newY = magnitude * Math.sin(angle)
return this return Vector2(newX, newY)
} }
/** /**
@@ -565,10 +561,9 @@ class Vector2 {
* Sets the [Vector2] to the zero [Vector2] * Sets the [Vector2] to the zero [Vector2]
* @return [Vector2] this vector * @return [Vector2] this vector
*/ */
fun zero(): Vector2 { fun zero() {
this.x = 0.0 this.x = 0.0
this.y = 0.0 this.y = 0.0
return this
} }
/** /**
@@ -616,9 +611,9 @@ class Vector2 {
*/ */
fun right(): Vector2 { fun right(): Vector2 {
val temp = this.x val temp = this.x
this.x = -this.y val newX = -this.y
this.y = temp val newY = temp
return this return Vector2(newX, newY)
} }
/** /**
@@ -636,9 +631,9 @@ class Vector2 {
*/ */
fun left(): Vector2 { fun left(): Vector2 {
val temp = this.x val temp = this.x
this.x = this.y val newX = this.y
this.y = -temp val newY = -temp
return this return Vector2(newX, newY)
} }
/** /**