Skip to content

Commit

Permalink
Updated rebounding from walls in both Chaotic and Maxwell's Demon eng…
Browse files Browse the repository at this point in the history
…ines.

git-svn-id: http://codeswarm.googlecode.com/svn/trunk@234 eda9c206-d64f-0410-a2f2-67f3fa0499ed
  • Loading branch information
nawglan committed Aug 4, 2008
1 parent dc3f49a commit fb9feea
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 21 deletions.
4 changes: 2 additions & 2 deletions src/PhysicsEngineChaotic.java
Original file line number Diff line number Diff line change
Expand Up @@ -420,14 +420,14 @@ public void onUpdatePerson(code_swarm.PersonNode pNode) {
// ensure coherent resulting position
pNode.mPosition.set(constrain(pNode.mPosition.x, 0.0f, (float)code_swarm.width),constrain(pNode.mPosition.y, 0.0f, (float)code_swarm.height));

if(pNode.mPosition.x < pNode.mass || pNode.mPosition.x > (code_swarm.width - pNode.mass)) {
if ((pNode.mPosition.x < pNode.mass && pNode.mSpeed.x < 0.0f) || (pNode.mPosition.x > (code_swarm.width - pNode.mass) && pNode.mSpeed.x > 0.0f)) {
// we hit a vertical wall
pNode.mSpeed.x = -pNode.mSpeed.x;
while (pNode.mPosition.x < pNode.mass || pNode.mPosition.x > (code_swarm.width - pNode.mass)) {
pNode.mPosition.x += pNode.mSpeed.x;
}
}
if(pNode.mPosition.y < pNode.mass || pNode.mPosition.y > (code_swarm.height - pNode.mass)) {
if ((pNode.mPosition.y < pNode.mass && pNode.mSpeed.y < 0.0f) || (pNode.mPosition.y > (code_swarm.height - pNode.mass) && pNode.mSpeed.y > 0.0f)) {
// we hit a horizontal wall
pNode.mSpeed.y = -pNode.mSpeed.y;
while (pNode.mPosition.y < pNode.mass || pNode.mPosition.y > (code_swarm.height - pNode.mass)) {
Expand Down
30 changes: 11 additions & 19 deletions src/PhysicsEngineMaxwellsDemon.java
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,8 @@ public void onRelaxNode(code_swarm.FileNode fNode ) {
if (fNode.life <= 0) {
return;
}

boolean mySide = whichSide(fNode);

Vector2f forceBetweenFiles = new Vector2f();
Vector2f forceSummation = new Vector2f();
Expand All @@ -440,7 +442,7 @@ public void onRelaxNode(code_swarm.FileNode fNode ) {
if (n.life <= 0)
continue;

if (n != fNode) {
if (n != fNode && mySide == whichSide(n)) {
// elemental force calculation, and summation
forceBetweenFiles = calculateForceBetweenfNodes(fNode, n);
forceSummation.add(forceBetweenFiles);
Expand Down Expand Up @@ -538,29 +540,24 @@ public void onUpdatePerson(code_swarm.PersonNode pNode) {
// | | |
if (pNode.mPosition.y < startDoorY || pNode.mPosition.y > midWayY) { // Above the door, and below the door.
if (rightSide) {
if (pNode.mPosition.x < (midWayX + pNode.mass) || pNode.mPosition.x > (code_swarm.width - pNode.mass)) {
if ((pNode.mPosition.x < (midWayX + pNode.mass) && pNode.mSpeed.x < 0.0f) || (pNode.mPosition.x > (code_swarm.width - pNode.mass) && pNode.mSpeed.x > 0.0f)) {
pNode.mSpeed.x = -pNode.mSpeed.x;
int i = 0;
while (pNode.mPosition.x < (midWayX + pNode.mass) || pNode.mPosition.x > (code_swarm.width - pNode.mass)) {
pNode.mPosition.x += pNode.mSpeed.x * (i++ % 10);
}
}
} else { // left side
if (pNode.mPosition.x < pNode.mass || (pNode.mPosition.x > (midWayX - pNode.mass) && pNode.mSpeed.x > 0.0f)) {
if ((pNode.mPosition.x < pNode.mass && pNode.mSpeed.x < 0.0f) || (pNode.mPosition.x > (midWayX - pNode.mass) && pNode.mSpeed.x > 0.0f)) {
pNode.mSpeed.x = -pNode.mSpeed.x;
int i = 0;
while (pNode.mPosition.x < pNode.mass || (pNode.mPosition.x > (midWayX - pNode.mass) && pNode.mSpeed.x > 0.0f)) {
pNode.mPosition.x += pNode.mSpeed.x * (i++ % 10);
}
} else if (pNode.mPosition.x > (midWayX - pNode.mass) && pNode.mSpeed.x < 0.0f) {
int i = 0;
while (pNode.mPosition.x > (midWayX - pNode.mass) && pNode.mSpeed.x < 0.0f) {
while (pNode.mPosition.x < pNode.mass || pNode.mPosition.x > (midWayX - pNode.mass)) {
pNode.mPosition.x += pNode.mSpeed.x * (i++ % 10);
}
}
}
} else { // Same level as the door
if (pNode.mPosition.x < pNode.mass || pNode.mPosition.x > (code_swarm.width - pNode.mass)) {
if ((pNode.mPosition.x < pNode.mass && pNode.mSpeed.x < 0.0f) || (pNode.mPosition.x > (code_swarm.width - pNode.mass) && pNode.mSpeed.x > 0.0f)) {
pNode.mSpeed.x = -pNode.mSpeed.x;
int i = 0;
while (pNode.mPosition.x < pNode.mass || pNode.mPosition.x > (code_swarm.width - pNode.mass)) {
Expand All @@ -578,23 +575,18 @@ public void onUpdatePerson(code_swarm.PersonNode pNode) {
// | | |

if (rightSide) {
if (pNode.mPosition.x < (midWayX + pNode.mass) || pNode.mPosition.x > (code_swarm.width - pNode.mass)) {
if ((pNode.mPosition.x < (midWayX + pNode.mass) && pNode.mSpeed.x < 0.0f) || (pNode.mPosition.x > (code_swarm.width - pNode.mass) && pNode.mSpeed.x > 0.0f)) {
pNode.mSpeed.x = -pNode.mSpeed.x;
int i = 0;
while (pNode.mPosition.x < (midWayX + pNode.mass) || pNode.mPosition.x > (code_swarm.width - pNode.mass)) {
pNode.mPosition.x += pNode.mSpeed.x * (i++ % 10);
}
}
} else { // left side
if (pNode.mPosition.x < pNode.mass || (pNode.mPosition.x > (midWayX - pNode.mass) && pNode.mSpeed.x > 0.0f)) {
if ((pNode.mPosition.x < pNode.mass && pNode.mSpeed.x < 0.0f) || (pNode.mPosition.x > (midWayX - pNode.mass) && pNode.mSpeed.x > 0.0f)) {
pNode.mSpeed.x = -pNode.mSpeed.x;
int i = 0;
while (pNode.mPosition.x < pNode.mass || (pNode.mPosition.x > (midWayX - pNode.mass) && pNode.mSpeed.x > 0.0f)) {
pNode.mPosition.x += pNode.mSpeed.x * (i++ % 10);
}
} else if (pNode.mPosition.x > (midWayX - pNode.mass) && pNode.mSpeed.x < 0.0f) {
int i = 0;
while (pNode.mPosition.x > (midWayX - pNode.mass) && pNode.mSpeed.x < 0.0f) {
while (pNode.mPosition.x < pNode.mass || pNode.mPosition.x > (midWayX - pNode.mass)) {
pNode.mPosition.x += pNode.mSpeed.x * (i++ % 10);
}
}
Expand All @@ -609,7 +601,7 @@ public void onUpdatePerson(code_swarm.PersonNode pNode) {
//
// _______

if(pNode.mPosition.y < pNode.mass || pNode.mPosition.y > (code_swarm.height - pNode.mass)) {
if ((pNode.mPosition.y < pNode.mass && pNode.mSpeed.y < 0.0f) || ((pNode.mPosition.y > (code_swarm.height - pNode.mass) && pNode.mSpeed.y > 0.0f))) {
pNode.mSpeed.y = -pNode.mSpeed.y;
int i = 0;
while (pNode.mPosition.y < pNode.mass || pNode.mPosition.y > (code_swarm.height - pNode.mass)) {
Expand Down

0 comments on commit fb9feea

Please sign in to comment.