Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7961641
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T05:06:05+00:00 2026-06-04T05:06:05+00:00

I have been working on a (kind of) gravity simulation using C# and Winforms,

  • 0

I have been working on a (kind of) gravity simulation using C# and Winforms, and I am getting some very weird behavior. It pretty much makes an object when you click, and is attracted to the other objects. The problem is that, unless they are within some weird distances, they will not be attracted in the positive directions (right, down.), but they are attracted upwards and to the left.

This is the update code:

    public Vector GetGravEffect(GravObject other)
    {
        if ((Math.Abs(X - other.X) <= Mass * Form.DrawScale + other.Mass * Form.DrawScale) && 
            (Math.Abs(Y - other.Y) <= Mass * Form.DrawScale + other.Mass * Form.DrawScale))
        {
            return new Vector(0, 0);
        }

        double tAngle = Math.Atan2((other.Y - Y), (other.X - X));

        double tMagnitude = (GravModifier * Mass * other.Mass / ((Math.Pow((other.X - X), 2)) + (Math.Pow((other.Y - Y), 2))) * 1000);

        Complex c = Complex.FromPolarCoordinates(tMagnitude, tAngle);

        Vector r = new Vector(c.Real, c.Imaginary);

        return r;
    }

Full code is here: https://docs.google.com/open?id=0B79vmyWxBr-kTnUtQURPUlVidzQ

Thanks for any help!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T05:06:07+00:00Added an answer on June 4, 2026 at 5:06 am

    Problem is not in GetGravEffect method (it gives correct results), but in Update method. It completely ignores physical laws. You cannot sum values returned by GetGravEffect and consider it to be velocity. GetGravEffect returns force that one object attracts the other. You must sum these forces and then do additional computations that includes inertia, acceleration and time to calculate resulting velocity. Also casting X and Y to int is not good idea, because you looses much of accuracy, especially for slow velocities. With following corrected method it works great:

        public void Update() {
            Vector Force = new Vector(0, 0);
            foreach (GravObject g in Form.Objects) {
                if (this != g)
                    Force += GetGravEffect(g);
            }
    
            double TimeElapsedSinceLastUpdate = Form.Timer.Interval * 0.001;
            Vector acceleration = Force / Mass;
            Velocity += acceleration*TimeElapsedSinceLastUpdate;
    
            X = (X + Velocity.X * Form.VelocityScale);
            Y = (Y + Velocity.Y * Form.VelocityScale);
    
            if (X + Mass * Form.DrawScale >= Form.Panels.Panel2.Width || X - Mass * Form.DrawScale <= 0)
                Velocity.X *= -1;
            if (Y + Mass * Form.DrawScale >= Form.Panels.Panel2.Height || Y - Mass * Form.DrawScale <= 0)
                Velocity.Y *= -1;
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So, I have been working on adding BitmapImages to a ListBox for pretty much
I have been working with android for a little while now and feel pretty
I have been working on an Android app using Phonegap and now would like
I have been working with Visual Studio (WinForm and ASP.NET applications using mostly C#)
Hi, i have been having some problems using JSON data in flash builder lately
I have been working with the commercial version of Qt on Windows for some
I have been working on some of the projects of my own and dont
I have been working on this, very, small photo cms lately. And now i
I've been working with core data (successfully ;) , following some tutorials and kind
I have been working for two years in software industry. Some things that have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.