# 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.

## 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]

##### Share on other sites

[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.

##### 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.

[/quote]

yes that is exactly what im looking for

1. 1
2. 2
3. 3
Rutin
15
4. 4
khawk
14
5. 5
frob
12

• 9
• 11
• 11
• 23
• 12
• ### Forum Statistics

• Total Topics
633660
• Total Posts
3013221
×