Click here to Skip to main content
15,445,491 members
Please Sign up or sign in to vote.
2.00/5 (2 votes)
See more:
Hello Codeproject!

I've been trying to create 2D gravity between a few entities, however most of the entities seem to just fly away(always to the right.) Does anyone know what's going on?

C#
VecForce = new Vector2();
Distance = Vector2.Distance(Position, Entity.Position);

xx = Entity.Position.X;
yy = Entity.Position.Y;

Angle = Math.Atan2(yy - Position.Y, xx - Position.X);

Multitude = (Constants.Gravity * (Mass * Entity.Mass) / (Distance * Distance));

VecForce = new Vector2((float)Math.Cos(Angle), (float)Math.Sin(Angle));
VecForce.Normalize();

VelocityForce = Vector2.Add(VelocityForce, VecForce * Multitude) / Mass;


Edit:

Sorry I forgot to include the update and gravity code:

This is how the entity moves:
C#
public void DoMovement(World World)
{
    Position += VelocityForce;
    VelocityForce.X = VelocityForce.Y = 0;
}



This is how the World handles the gravity effect code(The first code)
C#
foreach (Entity Entity in Entities)
{
    foreach (Entity Entity2 in Entities)
    {
        if (Entity2.Equals(Entity)) continue;
        Entity.Attraction(Entity2);
    }
}
Posted
Updated 3-Dec-13 4:50am
v4
Comments
Sergey Alexandrovich Kryukov 3-Dec-13 10:47am     CRLF
The problem seems very simple, based on school Newtonian representation of classical mechanics. But you don't explain your model. What are the forces except gravity? Where Vector2 is defined? If you do something at a variable angle, this is some other force. Multiply Mass by other mass, Entity.Mass — oh! Are you serious? Need a help with simple mechanics? —SA
Deviant Sapphire 3-Dec-13 10:57am    
?
Sergey Alexandrovich Kryukov 3-Dec-13 11:34am     CRLF
If you describe all mechanical model in sufficient detail, we can discuss it and you can get help. So far, there is no a subject for discussion. —SA
Deviant Sapphire 3-Dec-13 11:43am    
I've given you all the code that affects the position, and the problem.
Sergey Alexandrovich Kryukov 3-Dec-13 11:58am     CRLF
I don't need to see you code just yet. How come you don't understand it? But better answer my questions above... —SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900