Click here to Skip to main content
15,400,447 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
   
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
   
?
   
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.
   
I don't need to see you code just yet. How come you don't understand it? But better answer my questions above...
—SA
Deviant Sapphire 3-Dec-13 11:43am
   
And with that there is nothing else I could add on here. :c
   
Good bye then. :-)
—SA
Ron Beyer 3-Dec-13 10:47am
   
What is your programming environment? Unity? Would help if you gave more information about what framework you are working with other than just C#
Deviant Sapphire 3-Dec-13 10:57am
   
I'm using XNA 4.0. :)
   
Does not matter much. So far, it looks like a gibberish at the level of school-level mechanics. Please see my comments above.
—SA
_Maxxx_ 3-Dec-13 22:05pm
   
Don't be so rude and obnoxious.
BillW33 3-Dec-13 12:58pm
   
You should include your code for your Entity class, especially Entity.Attraction. We need more info to figure out what is going on.
Deviant Sapphire 3-Dec-13 13:57pm
   
The code in the beginning is attraction.

public void Attraction(Entity Entity)
{
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;
}

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