Jump to content
  • Advertisement
Sign in to follow this  
ryan20fun

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

This topic is 2579 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites

[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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!