Click here to Skip to main content
15,911,485 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Hi,
I am currently writing a library in c# known as "AnotherPhys" (https://github.com/bentekken/AnotherPhys[^])


edit: Firstly, I need a way of solving collisions (main issue) in an efficient manner. Secondly, I want to be able to keep it fairly simple but effective.

This project is for personal interest but also for me to broaden my understanding of c#. Once I have successfully written this library, I'm going to try and write in c++.
this is the code so far:
public class PhysicsObject
    {
        //Default Constructor
        public PhysicsObject() { }
        public PhysicsObject(double mass, double acceleration)
        {
            this.Mass = mass;
            this.Acceleration = acceleration;
        }


        int x; int y;
        double StartTime; double Time; double ChangeTime;
        double StartSpeed; double Speed; double ChangeSpeed;
        double Mass { get; set; }
        double Acceleration { get; set; }
        public double GetAccelSync()
        {
            ChangeSpeed = (Speed - StartSpeed);
            ChangeTime = (Time - StartTime);
            Acceleration = (Speed - StartSpeed) / (Time - StartTime);
            return (Speed - StartSpeed) / (Time - StartTime);
        }
        public double GetSpeedSync()
        {
            return Acceleration * ChangeTime;
        }
        public void MoveUpdate()
        {
            GetAccelSync();
        }
        /// <summary>
        /// Returns a timespan based on time change. The unit is pixels per second (p/s).
        /// Returning a TimeSpan provides more flexibility.
        /// </summary>
        /// <returns>TimeSpan</returns>
        TimeSpan GetDuration()
        {
            return TimeSpan.FromSeconds(Time - StartTime);
        }
    }
    public class PhysicsWorld
    {
        Rectangle WorldBounds;
        PhysicsObject[] PhysObjArray { get; set; }
        Timer WorldTime;
        public PhysicsWorld() 
        {
            WorldTime.Tick += new EventHandler(WorldTime_Tick);
        }


        void WorldTime_Tick(object sender, EventArgs e)
        {
            Collisions();
            
        }
        public void Collisions()
        {
            foreach (PhysicsObject obj in PhysObjArray)
            { 
                
            }
        }
        public bool Freeze()
        {
            WorldTime.Stop();
            return true;
        }
        public bool UnFreeze()
        {
            WorldTime.Start();
            return true;
        }
        
        public void AddPhysObj(PhysicsObject PhysObj)
        {
            PhysObjArray[PhysObjArray.Length] = PhysObj;
        }


        double GravityValue { get; set; }
        public void SetGravity(double gravity)
        {
            this.GravityValue = gravity;
        }
    }
Posted
Updated 16-May-13 23:38pm
v2
Comments
Kschuler 16-May-13 9:12am    
Generally people do not like to click on links to view projects. There is no way for us to know if it is safe. It's best to post your code here in a code block. Also, what are you asking for? Could you please elaborate or provide an example because it's not clear where you are having trouble.
bentekken 17-May-13 5:28am    
Im fairly new to codeplex so I apologise for my shoddy question. I'll post a code block ASAP...
Thanks for pointing that out ;)

Your PhysicsObject's looks dimensionless points (it is a quite arguable assumption) hence a collision happens if their {x,y} coordinates are exactly the same. You might check that this way:
for (i=0; n<physobjarray.length-1; i++)   {
  for (j=i+1; n<physobjarray.length; j++)    {
    if (  PhysObjArray[i].x == PhysObjArray[j].x && PhysObjArray[i].y == PhysObjArray[j].y)
    {
      // collision detected here
    }
  }
}


By the way your Collisions methjod should either handle itself the collions (that is their consequences) or return an array containing colliding objects.
 
Share this answer
 
v2
Comments
bentekken 18-May-13 4:26am    
What do you mean by using n<>?
CPallini 18-May-13 15:51pm    
Sorry that was bad-formatted. Should be fixed now.
First I'd make the default constructor private. You probably don't want to create objects with all 0 attributes, at least not by accident.
Your object constructor only has mass and acceleraton. Instead I'd create objects at least with mass and location (x,y) and possibly some measure of size (e.g., diameter/radius) for collision detection.
The speed (velocity) and acceleration need to be settable, or must be part of the constructor (I'd prefer the constructor).
Location needs to be a readable property.
Velocity and acceleration are both vector quantities (x', y' and x'', y'')
The Mass probably should not be settable after the constructor (depending on what you're modelling).
If you want to accurately model objects changing position and velocity over time, for example due to gravitational interaction, then the updating needs to be done in two phases.
1. for each object,
  a. calculate the vector sum of forces acting on it
    - e.g., for gravity, magnetic or electrostatic interaction, you'll need all current locations
  b. calculate the next acceleration vector
  c. calculate the next velocity vector based on the mean of the current and next acceleration vectors
  d. calculate the next location based on the mean of the current and next velocity vectors
2. for each object
  - update the current acceleration, velocity and location vectors with the corresponding next vectors
 
Share this answer
 
Comments
bentekken 19-May-13 5:06am    
Thanks for the answer, I guess I am trying to emulate some real physical properties, but I am not a top student in science ;)

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