9 Replies Latest reply: Jul 5, 2012 4:12 PM by morgalr

# Why is the head jumping around the body?

I wrote the following code for Head which should follow the body like a spring. I added friction so the head will stop bouncing sometime but no success. The head keeps jumping near the beggining of the spring (which is the beggining of the body) and not stopping. What should I do to make the head stop at some time?
``````  final static int MASS = 10;
final static int FRICTION = 100;
private final double hardness = 3;

public void followBody(Body body) {
double[] coards = getCoardinatesBy(getRadius(), body.getXCoardStart(), body.getYCoardStart(), 180+body.getAngel()); // get the coordinates of the beginning of the spring (where should the head be in the end
coards[0] -= getRadius();
coards[1] -= getRadius();
if (getYSpeed() < 0) {
this.setYAcceleration((getYForce(coards[1]) + FRICTION)/MASS);
} else if (getYSpeed() > 0) {
this.setYAcceleration((getYForce(coards[1]) - FRICTION)/MASS);
} else if (Math.abs(getYForce(coards[1])) < FRICTION ) { //static friction, equal to the power or less
this.setYAcceleration(0);
} else {
this.setYAcceleration(getYForce(coards[1]) / MASS); // speed is zero. new the head is far away from the spring
}
if (getXSpeed() < 0) {
this.setXAcceleration((getXForce(coards[0]) + FRICTION)/MASS);
} else if (getXSpeed() > 0) {
this.setXAcceleration((getXForce(coards[0]) - FRICTION)/MASS);
} else if (Math.abs(getXForce(coards[0])) < FRICTION) { // static friction, equal to the power or less
this.setXAcceleration(0);
} else {
this.setXAcceleration(getXForce(coards[0]) / MASS); // speed is zero. new the head is far away from the spring
}
}
double getYForce(double y){
return hardness*(y-yCoardStart);
}

double getXForce(double x){
return hardness*(x-xCoardStart);
}

static double[] getCoardinatesBy(double height, double startX, double startY, double angel) {
double[] coardinates = new double[2];
coardinates[1] = startY - (height * Math.sin((Math.toRadians(angel))));
coardinates[0] = startX + (height * Math.cos(Math.toRadians(angel)));
return coardinates;
}

public void update() {
xSpeed += xAcc;
ySpeed += yAcc;
xCoardStart += xSpeed;
xCoardEnd += xSpeed;
yCoardEnd += ySpeed;
yCoardStart += ySpeed;
angularVelocity+=angularAcc;
this.angel += angularVelocity;
autoSetEndXY();
}

private void autoSetEndXY() {
double[] coards = getCoardinatesBy(height, xCoardStart, yCoardStart, angel);
yCoardEnd = coards[1];
xCoardEnd = coards[0];
}``````
Edited by: Vitali.pom on May 20, 2012 6:01 AM

Edited by: Vitali.pom on May 29, 2012 12:19 AM

Edited by: Vitali.pom on Jun 26, 2012 12:05 PM
• ###### 1. Re: Why is the head jumping around the body?
You do not show enough of your calculations, nor definitions of your variables to make an intelligent answer other than your speed never damps out.

Acceleration = Force/Mass and Speed = Aacceleration*Time and Speed Change = Speed Start + Acceleration*Time, supposing that you know that, and that Friction is a damping Force, then you are either not ever getting to 0 due to some mangnitude calculaton or your over all calculation if just messed up. Without seeing more, we cannot really say what it is though.
• ###### 2. Re: Why is the head jumping around the body?
I added some code, hope it helps.
• ###### 3. Re: Why is the head jumping around the body?
Actually, no it does not. You're going to have to post a short working example illustrating your problem and using the same formulas and approach you are using in your current program.

I don't see any miminum checking on the velocity--so if you pass 0 and go negative that may be what's happening.
• ###### 4. Re: Why is the head jumping around the body?
Of course what you described here can happen... Just like in (real) physics. Now I really have no idea what else can I copy to here, my code consists from hundreds lines of code and a short example can be only a main which will show the usages of those examples, but I'm in big doubt it will help or worth the work. Could you please be more specific like in your last question on what exactly is unclear? It is much easier for me to answer than code in addition to what I coded so far.
• ###### 5. Re: Why is the head jumping around the body?
It's very simple... your damping effect is not tending to 0, watch the values in your debugger and it will show you what is wrong, since you cannot post a short example to illustrate your problem. Once you get to a "close enough" to be considered completely damped, then you need to change your state, so you do not have an increment, velocity, or dampening vector to change your directions.
• ###### 6. Re: Why is the head jumping around the body?
Thanks for the try. That's what I tried:
``````public void followBodyPart(BodyPart body) {
double[] coards = getCoardinatesBy(getRadius(), body.getXCoardStart(), body.getYCoardStart(), 180+body.getAngel());
coards[0] -= getRadius();
coards[1] -= getRadius();
if (getYSpeed() < 0) {
this.setYAcceleration((getYForce(coards[1]) + FRICTION)/MASS);
} else if (getYSpeed() > 0) {
this.setYAcceleration((getYForce(coards[1]) - FRICTION)/MASS);
System.out.println("Player moved.");
} else if (Math.abs(getYForce(coards[1])) < FRICTION) {
this.setYAcceleration(0);
System.out.println("y force is 0.");
} else {
this.setYAcceleration((getYForce(coards[1]))/ MASS);
}
if (getXSpeed() < 0) {

this.setXAcceleration((getXForce(coards[0]) + FRICTION)/MASS);
} else if (getXSpeed() > 0) {
this.setXAcceleration((getXForce(coards[0]) - FRICTION)/MASS);
System.out.println("Player moved.");
} else if (Math.abs(getXForce(coards[0])) < FRICTION) {
this.setXAcceleration(0);
System.out.println("x force is 0.");
} else {
this.setXAcceleration((getXForce(coards[0]))/ MASS);
}

}``````
And that's what I got (repeating in the end):
Player moved.
y force is 0.
y force is 0.
Player moved.
y force is 0.
y force is 0.
Player moved.
y force is 0.
Player moved.
Player moved.
Player moved.
Player moved.
Player moved.
...

Edited by: Vitali.pom on Jun 16, 2012 3:54 AM
• ###### 7. Re: Why is the head jumping around the body?
You have 2 places where it says "Player moved" both of which show positive speed components--so what does "Player moved" mean when yiou don't know what triggers that output? You're speed in the Y direction to show complete damping, has to be 0--it is not from what you are showing me. What may be better indicator is to print out all of your variables for your velocity vectors and position each time you enter the method so you can track what is happening.
• ###### 8. Re: Why is the head jumping around the body?
Thanks, now I have two problems. I tried to set the friction to 0 and did what you said, that what I have (the head now moves a bit from the very begging):
Velocity: (0.0,0.0)Force: (0.0,-8.526512829121202E-14)
Velocity: (0.0,-8.526512829121202E-15)Force: (0.0,-8.526512829121202E-14)
Velocity: (0.0,-1.7053025658242404E-14)Force: (0.0,0.0)
Velocity: (0.0,-1.7053025658242404E-14)Force: (0.0,8.526512829121202E-14)
Velocity: (0.0,-8.526512829121202E-15)Force: (0.0,8.526512829121202E-14)
Velocity: (0.0,0.0)Force: (0.0,8.526512829121202E-14)
...
Now you can believe me that the spring actually behaves as a spring.

Friction is back and after we move the body, we get:

Velocity: (-7.841111727850599,4.689589551932404)Force: (20.58212928052734,-28.61018677672928)
Velocity: (4.217101200202135,-8.171429125740524)Force: (7.930825679920929,-4.095899399507687)
Velocity: (-4.989816231805772,1.418980934308708)Force: (22.900274375338256,-8.352842202433806)
Velocity: (7.300211205728054,-9.416303285934672)Force: (0.9996407581540723,19.896067655370132)
Velocity: (-2.5998247184565386,2.573303479602341)Force: (8.79911491352371,12.176157216563183)
Velocity: (8.280086772895832,-6.20908079874134)Force: (-16.041145405163775,30.803399612787246)
Velocity: (-3.324027767620546,6.871259162537385)Force: (-6.0690621023021265,10.189622125175049)
Velocity: (6.069066022149242,-2.109778624945111)Force: (-24.276260168749815,16.518958000010457)
Velocity: (-6.35855999472574,9.542117175055935)Force: (-5.200580184572601,-12.107393525157306)

Discrete time intervals, is that the problem?

P.S I changed the + to - and vice versa in the first message since my X was Y and vice versa. I also fixed this in my code.
• ###### 9. Re: Why is the head jumping around the body?
Just looking at your numbers without friction, it doesn't give expected results--imperically speaking: 0 velocity vector and you apply a force and your velocity is changed apply the same force and your velocity is changed in an opposite direction. Then no force is supplied and your velocity is changed.

If the force is the force of the spring, I would expect it to be zero to start and increase while the velocity starts at max and decreases, or if the force starts at max and decreases to 0 which should show the velocity vector from 0 to max. I'm not seeing that. In any case the force should always push the velocity in the same direction or be acting as a dampener if the force is spring force. It seems something is messed up there.

Aside from that you need to look at the granularity of your movement calculations--what is the accuracy of your pixel calcualtions.

Here is a link to a good discussion on Motion of a Mass on a Spring: http://www.physicsclassroom.com/class/waves/u10l0d.cfm

I think you'll find something in your calculations, that you are not showing, is messed up.