Jump to content

  • Log In with Google      Sign In   
  • Create Account

#ActualBradDaBug

Posted 23 November 2012 - 01:19 PM

I thought this would be simpler than it has turned out to be. Imagine a string with a heavy weight at one end that's attached to the mouse cursor, and as the player moves the mouse cursor the weight at the end of the string should sway and oscilate around. Creating a simple pendulum is easy, but making the pendulum and the weight move around in response to the movement of the pendulum.... I'm not having any luck. I found this: http://math.stackexc...th-moving-pivot but that diagram doesn't look correct.That angle between A1 and A is not , but rather (right?).

Anyway, here is a snippet of code but it doesn't work. The bob doesn't behave at all like what I want. I call this method each frame, and the coordinates represent the mouse coordinates. I'm not using _bobAngle.Y yet, so ignore the fact that I'm not calculating it. I'm just trying to get the bob to behave correctly in 1 dimension before I work on the second.

[source lang="csharp"]private void update(float elapsedTime, int x, int y){ // figure out the current mouse velocity Vector3 vel = new Vector3(); vel.X = x - _position.X; vel.Y = 0; vel.Z = y - _position.Z; vel *= elapsedTime; // compare the current velocity with the old to calculate acceleration Vector3 acceleration = (vel - _oldVelocity) / elapsedTime; acceleration.Y = -5.0f * elapsedTime; _oldVelocity = vel; float b2 = (float)Math.Atan2(acceleration.X, -acceleration.Y); float a1 = acceleration.Length() * (float)Math.Cos(MathHelper.PiOver2 - Utils.AngleDiff(_bobAngle.X, b2)) * 1.0f; _bobAngularVelocity.X += a1 / _springLength * elapsedTime; // apply damping _bobAngularVelocity -= _bobAngularVelocity * 0.01f * elapsedTime; // apply velocity _bobAngle.X += _bobAngularVelocity.X * elapsedTime; _bobAngle.Y += _bobAngularVelocity.Y * elapsedTime; _bobAngle.X = MathHelper.WrapAngle(_bobAngle.X); _bobAngle.Y = MathHelper.WrapAngle(_bobAngle.Y); _position.X = x; _position.Z = y;}private Vector2 calcBobPosition(){ Vector2 p = new Vector2(); p.X = _position.X + _springLength * (float)Math.Sin(_bobAngle.X); p.Y = _position.Z + _springLength * (float)Math.Sin(_bobAngle.Y); return p;}[/source]

Can anyone tell what I'm doing wrong?

#1BradDaBug

Posted 23 November 2012 - 01:05 PM

I thought this would be simpler than it has turned out to be. Imagine a string with a heavy weight at one end that's attached to the mouse cursor, and as the player moves the mouse cursor the weight at the end of the string should sway and oscilate around. Creating a simple pendulum is easy, but making the pendulum and the weight move around in response to the movement of the pendulum.... I'm not having any luck. I found this: http://math.stackexc...th-moving-pivot but that diagram doesn't look correct.That angle between A1 and A is not a - B, but rather PI/2 - a - B (right?).

Anyway, here is a snippet of code but it doesn't work. The bob doesn't behave at all like what I want. I call this method each frame, and the coordinates represent the mouse coordinates. I'm not using _bobAngle.Y yet, so ignore the fact that I'm not calculating it. I'm just trying to get the bob to behave correctly in 1 dimension before I work on the second.

[source lang="csharp"]private void update(float elapsedTime, int x, int y){ // figure out the current mouse velocity Vector3 vel = new Vector3(); vel.X = x - _position.X; vel.Y = 0; vel.Z = y - _position.Z; vel *= elapsedTime; // compare the current velocity with the old to calculate acceleration Vector3 acceleration = (vel - _oldVelocity) / elapsedTime; acceleration.Y = -5.0f * elapsedTime; _oldVelocity = vel; float b2 = (float)Math.Atan2(acceleration.X, -acceleration.Y); float a1 = acceleration.Length() * (float)Math.Cos(MathHelper.PiOver2 - Utils.AngleDiff(_bobAngle.X, b2)) * 1.0f; _bobAngularVelocity.X += a1 / _springLength * elapsedTime; // apply damping _bobAngularVelocity -= _bobAngularVelocity * 0.01f * elapsedTime; // apply velocity _bobAngle.X += _bobAngularVelocity.X * elapsedTime; _bobAngle.Y += _bobAngularVelocity.Y * elapsedTime; _bobAngle.X = MathHelper.WrapAngle(_bobAngle.X); _bobAngle.Y = MathHelper.WrapAngle(_bobAngle.Y); _position.X = x; _position.Z = y;}private Vector2 calcBobPosition(){ Vector2 p = new Vector2(); p.X = _position.X + _springLength * (float)Math.Sin(_bobAngle.X); p.Y = _position.Z + _springLength * (float)Math.Sin(_bobAngle.Y); return p;}[/source]

Can anyone tell what I'm doing wrong?

PARTNERS