Cant get object to move at variable speed toward point [C++]

Started by
1 comment, last by ryan20fun 12 years, 5 months ago
Hi All.

im experimenting with gravity wells right now and have hit a snag.

if i want the wells radius to be larger then the wells force, then if then this happens to the object:
withing certain radius it will pull the object( just what i want ).
to fat, and it will push the object( now what i want. )

here is the code for pulling the object

Vector2 GravityWell( Vector2 Well, float radius, float WellStrength, Vector2 ObjectPosition, float DeltaTime )
{
Vector2 Acceleration;

Vector2 B( Well.X, Well.Y );

Vector2 normalized( B - ObjectPosition );
normalized.Normalize();

Vector2 dir( ObjectPosition - B );
dir.Normalize();

float diff_X = B.X - ObjectPosition.X;
float diff_Y = B.Y - ObjectPosition.Y;
float distance = sqrt( diff_X * diff_X + diff_Y * diff_Y );

if( distance < radius )
{
float r = radius;
float s = WellStrength;

float s_s = r / WellStrength;

Acceleration.X -= ( dir.X * ( ( WellStrength * s_s ) - distance ) ) * DeltaTime;
Acceleration.Y -= ( dir.Y * ( ( WellStrength * s_s ) - distance ) ) * DeltaTime;
}

return Acceleration;
}


the WellStrength variable is supposed to be the speed at wich it pulls the object ( the closer the object is to the center of the well the more force the wel exerts on the object )

how would i use the stregth variable so that when the radius is larger, the pull is smaller ?

here is a diagram of whats happening
[attachment=6147:diagram.jpg]

Thanks In Advance.

Never say Never, Because Never comes too soon. - ryan20fun

Disclaimer: Each post of mine is intended as an attempt of helping and/or bringing some meaningfull insight to the topic at hand. Due to my nature, my good intentions will not always be plainly visible. I apologise in advance and assure you I mean no harm and do not intend to insult anyone.

Advertisement

[source]
float s_s = r / WellStrength;

Acceleration.X -= ( dir.X * ( ( WellStrength * s_s ) - distance ) ) * DeltaTime;
Acceleration.Y -= ( dir.Y * ( ( WellStrength * s_s ) - distance ) ) * DeltaTime;
[/source]

WellStrength is never used. s_s = r / WellStrength, then you multiply s_s by WellStrength again. WellStrength * s_s = r / WellStrength * WellStrength = r.

If you want strength to be equal to 0 on the edge of the radius and linearly increase to WellStrength when getting closer to the centre, you can use the following equation: strength = (1.0f - distance / radius) * WellStrength.

Does that answer your question?

[quote name='ryan20fun' timestamp='1321626895' post='4885317']
[source]
float s_s = r / WellStrength;

Acceleration.X -= ( dir.X * ( ( WellStrength * s_s ) - distance ) ) * DeltaTime;
Acceleration.Y -= ( dir.Y * ( ( WellStrength * s_s ) - distance ) ) * DeltaTime;
[/source]

WellStrength is never used. s_s = r / WellStrength, then you multiply s_s by WellStrength again. WellStrength * s_s = r / WellStrength * WellStrength = r.

If you want strength to be equal to 0 on the edge of the radius and linearly increase to WellStrength when getting closer to the centre, you can use the following equation: strength = (1.0f - distance / radius) * WellStrength.

Does that answer your question?
[/quote]

yes that is exactly what im looking for

Never say Never, Because Never comes too soon. - ryan20fun

Disclaimer: Each post of mine is intended as an attempt of helping and/or bringing some meaningfull insight to the topic at hand. Due to my nature, my good intentions will not always be plainly visible. I apologise in advance and assure you I mean no harm and do not intend to insult anyone.

This topic is closed to new replies.

Advertisement