Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Discussion / Gravity and particles

Author
Message
Pixelator
17
Years of Service
User Offline
Joined: 8th Jul 2007
Location: here
Posted: 20th Nov 2008 03:15
Hi, how would i go about having particles in a 2d world gravitate to each other and plot their paths?

i am working on a program that includes the above, but also includes a section that detects when particles are touching and group them together as a larger chunk of matter s that it can assign it more gravitational pull.

I think i can get the particle contact detecting and lumping there of but i am not quite sure how mathematically i would go about the gravity.

Your signature has been erased by a mod
BN2 Productions
21
Years of Service
User Offline
Joined: 22nd Jan 2004
Location:
Posted: 20th Nov 2008 05:00
Gravitational Pull Formula:

F=Gm1m2/(r^2)

In english

Force of gravitational attraction=Gravitational Constant*Mass Object 1*Mass Object 2/the distance between them squared.

Gravitational Constant=6.67300 × 10^-11 m^3 kg^-1 s^-2

Units are: Mass is Kg, and Radius is Meters

Ever notice how in Microsoft word, the word "microsoft" is auto corrected to be "Microsoft" but "macintosh" just gets the dumb red underline?
Pixelator
17
Years of Service
User Offline
Joined: 8th Jul 2007
Location: here
Posted: 21st Nov 2008 01:52
ok, thanks. now , how should i balance between the momentum of an object's movement and the gravitational pull to decide which direction it will go and at what speed?

Your signature has been erased by a mod
BN2 Productions
21
Years of Service
User Offline
Joined: 22nd Jan 2004
Location:
Posted: 21st Nov 2008 08:38
Physics lesson!

F=m*a (Force=mass*acceleration)

Momentum really won't help you too much, it is essentially force*time.

Now, here is how you figure out which way it will go:

First split all forces acting on the object into components:

Fx=F*cos(theta)
Fy=F*sin(theta)

Then you add the directions of the forces to get a net force.

Fnetx=Fx1+Fx2+...
Fnety=Fy1+Fy2+...

You can then calculate the acceleration in each component by:

Fnetx=m*ax (Net Force in the X Direction=mass*acceleration in the x direction)
Fnety=m*ay (Same, but in the y direction)

You can then apply ax and ay to the objects vertical and horizontal velocity (just add them)

You then move your object based off of its horizontal and vertical velocity components.

Let me know if this makes sense or not.

Ever notice how in Microsoft word, the word "microsoft" is auto corrected to be "Microsoft" but "macintosh" just gets the dumb red underline?
Pixelator
17
Years of Service
User Offline
Joined: 8th Jul 2007
Location: here
Posted: 22nd Nov 2008 20:59
um, maybe not?

Your signature has been erased by a mod
BN2 Productions
21
Years of Service
User Offline
Joined: 22nd Jan 2004
Location:
Posted: 22nd Nov 2008 21:42
Ok, I will try again:

Velocity is an objects speed and direction. Also can be thought of as the rate of change of position. (Every iteration, if the velocity is 5, the object will move 5 units in the direction of the velocity).

Acceleration is the rate of change of the objects velocity. So, if acceleration is 5, every iteration of the code will result in the velocity having 5 added to it (which could make it change direction if it has a negative velocity).

So, to review:

In each iteration of the loop:

New Position=old position + velocity
Broken down:
New X=Old x+Velocity X component (will explain components later)
New y=Old y+Velocity Y

New Velocity=Old Velocity + acceleration
Broken down:
New Velocity X=Old Velocity X+ acceleration x component
New Velocity Y=Old Velocity Y+ acceleration y component

The notation from here on out is:
X=X position
Y=Y position

Vx=Velocity X component
Vy=Velocity Y component

ax=Acceleration X Component
ay=Acceleration Y Component

Acceleration and velocity are called Vectors, which means that they have a magnitude and a direction. These two things can be used to break them into components (after this) which are Scalars, that is, they only have a magnitude, such as position.

Component:
Components are how you would deal with directions. Since 5 units at 45 degrees won't increase either direction 5 units, you can't just add 5. To solve the problem a little trig is involved.

Remember:
Sin=Opposite/hypotenuse
Cos=Adjacent/hypotenuse

So, that means that:
Opposite side length=hypotenuse*sin
and
Adjacent side length=hypotenuse*cos

The opposite side length is the y difference (or the rise) of the angle

the adjacent side length is the x difference (or the run) of the angle

The hypotenuse is the value of the unit (so a velocity of 5 is a triangle with hypotenuse 5 units long).

So, this info reveals:

X=R*cos(theta)
Y=R*sin(theta)
where R is the hypotenuse

The components can then be added together to get a net value. So, for forces, the net would be called the net force or Fnet, with components FnetX and FnetY. So, for N (any number) of forces, the equations would be

FnetX=F1x+F2x+...Fnx
FnetY=F1y+F2y+...Fny

Unfortunately, this is all I have time for now, will get back with the physics application of this stuff (essentially this is vector math). Let me know if this makes sense so that I don't lose you further by going into physics (which really is just this stuff only applied to different things).

Ever notice how in Microsoft word, the word "microsoft" is auto corrected to be "Microsoft" but "macintosh" just gets the dumb red underline?
Pixelator
17
Years of Service
User Offline
Joined: 8th Jul 2007
Location: here
Posted: 22nd Nov 2008 22:31
what is the theta?
(i haven't taken trig yet so this is all new to me.)

Your signature has been erased by a mod
BN2 Productions
21
Years of Service
User Offline
Joined: 22nd Jan 2004
Location:
Posted: 23rd Nov 2008 06:11
Theta means the angle, I can't put the greek letter so I just spelled it out. It literally is just a variable that stands for the angle.

Ever notice how in Microsoft word, the word "microsoft" is auto corrected to be "Microsoft" but "macintosh" just gets the dumb red underline?
Pixelator
17
Years of Service
User Offline
Joined: 8th Jul 2007
Location: here
Posted: 23rd Nov 2008 21:14
so the equation is used to break the velocity into its x and y components?

Your signature has been erased by a mod
BN2 Productions
21
Years of Service
User Offline
Joined: 22nd Jan 2004
Location:
Posted: 24th Nov 2008 09:00
Yes. It can be used to break any vector (a line with a direction) into its x and y components, including velocity, acceleration, even displacement (change in position).

Ever notice how in Microsoft word, the word "microsoft" is auto corrected to be "Microsoft" but "macintosh" just gets the dumb red underline?
Pixelator
17
Years of Service
User Offline
Joined: 8th Jul 2007
Location: here
Posted: 30th Nov 2008 21:14
ok, so you take all the forces, break them into x and y components, add them together, use that as the acceleration x and y components, add that to the velocity x and y components, and add that to your old x and y positions to find where your new position will be right?

Your signature has been erased by a mod
TheComet
17
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 1st Dec 2008 11:18
Urm... What?

Just curious, BN2, where did you learn that?

Peachy, and the Chaos of the Gems

BN2 Productions
21
Years of Service
User Offline
Joined: 22nd Jan 2004
Location:
Posted: 1st Dec 2008 19:10 Edited at: 1st Dec 2008 19:13
Quote: "Just curious, BN2, where did you learn that?"


2 years high school physics and 1 quarter of college physics

Quote: "add them together, use that as the acceleration x and y components,"


Don't forget your units. The units of a force are mass*distance/time^2. The units of acceleration are distance/time^2. So what that means is that you will need to divide the resultant forces (the sum of the components) by the mass of the object moving to get its acceleration. So instead its:

add them together, divide by the mass, use that as the acceleration x and y components...

Other than that, yes, exactly.

Ever notice how in Microsoft word, the word "microsoft" is auto corrected to be "Microsoft" but "macintosh" just gets the dumb red underline?
Code eater
17
Years of Service
User Offline
Joined: 17th Feb 2008
Location: Behind You!
Posted: 3rd Dec 2008 19:26
Going back to the gravity I was wondering if i could ask a little bit about that.

Quote: "Force of gravitational attraction=Gravitational Constant*Mass Object 1*Mass Object 2/the distance between them squared.

Gravitational Constant=6.67300 × 10^-11 m^3 kg^-1 s^-2"


I understand the Force of gravitational attraction=Gravitational Constant*Mass Object 1*Mass Object 2/the distance between them squared.
bit but im not so sure about the Gravitational Constant=6.67300 × 10^-11 m^3 kg^-1 s^-2 What do m, kg and s stand for?

thanks,,,

codeeater
BN2 Productions
21
Years of Service
User Offline
Joined: 22nd Jan 2004
Location:
Posted: 3rd Dec 2008 19:40
those are the units, it is meters cubed/(kilogram*second squared) or length^3/mass*time^2

they are just there so that it can work out right. As far as the math goes, you can leave those out and just use the numbers.

Ever notice how in Microsoft word, the word "microsoft" is auto corrected to be "Microsoft" but "macintosh" just gets the dumb red underline?
Latch
18
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 3rd Dec 2008 22:55
Why is m1*m2 and not m1+m2 ? Just messing around trying to come up with a proof but I can't do it!!

Enjoy your day.
BN2 Productions
21
Years of Service
User Offline
Joined: 22nd Jan 2004
Location:
Posted: 3rd Dec 2008 23:23
Other than because its in the formula I am not sure, but here is some reasoning for it:

If there is only 1 object, then there isn't any gravitational force acting on it (since there is nothing to attract it).

So, if it were m1+m2, you would still get a number (though it would be dividing by 0, but lets ignore that). If it were m1*m2 then you would get 0 on top (again, though, that would be 0/0, but just roll with it). Unless of course you consider r as infinity, in which case there is an infinitely small force acting on it in the m1+m2 case and 0 acting on it in the m1*m2 case. This would agree with the observation that there isn't a gravitational pull on the object.

Perhaps? I really just made that up, but the formula IS m1*m2.

Ever notice how in Microsoft word, the word "microsoft" is auto corrected to be "Microsoft" but "macintosh" just gets the dumb red underline?
Latch
18
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 4th Dec 2008 07:32
Quote: "but the formula IS m1*m2."

I don't disagree with the formula, it's just one of things that gnaw at the brain .

Enjoy your day.
Pixelator
17
Years of Service
User Offline
Joined: 8th Jul 2007
Location: here
Posted: 6th Dec 2008 21:06
ok, so now how do i use this all to say, plot an orbit?

Your signature has been erased by a mod
BN2 Productions
21
Years of Service
User Offline
Joined: 22nd Jan 2004
Location:
Posted: 7th Dec 2008 00:51
Well, if you want to cheat, you COULD always just use an ellipse

If you want the math way then there it could be done this way.

The two things to worry about are these:

Velocity of the orbiting particle/planet

Force of gravitation between them (a centripetal force).

The force of gravitation will always be toward the center of the circle.

I am working out some code that will demonstrate how to do it.

What you can do is move the moving one along its path, then calculate the gravity and move back toward the center based off of the gravity.

Working on some demo code, but ran out of time, will try to have it ready by tomorrow.

Ever notice how in Microsoft word, the word "microsoft" is auto corrected to be "Microsoft" but "macintosh" just gets the dumb red underline?
Pillarofire
21
Years of Service
User Offline
Joined: 31st Dec 2003
Location: Good Question, <looks around.>
Posted: 7th Dec 2008 07:12
I seriously apologize if this doesn't work in DarkBASIC Classic, I only have DBPro.

I know it works in DBPro though, and I'm sure with a little work you can adapt the general concept.

Login to post a reply

Server time is: 2025-06-08 00:16:36
Your offset time is: 2025-06-08 00:16:36