We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
Page Index Toggle Pages: 1
gravity (Read 755 times)
gravity
Oct 4th, 2005, 9:13pm
 
Anyone have a nice example of simple gravity with or without friction?
Thanks

Re: gravity
Reply #1 - Oct 5th, 2005, 4:27am
 
This is probably not the BEST example.. but it might give you a good idea.

http://users.design.ucla.edu/~mflux/interactive/ballbounce/index.html

Hmm.. the source is ...

http://classes.design.ucla.edu/Fall03/157A/cursos/00/index_visor.php?id=8&ejercicio_id=5&persona_id=20

It's overly complicated. I'll write a simplified version some time for people who need to grasp this.
Re: gravity
Reply #2 - Oct 5th, 2005, 9:56am
 
I've done a couple too:

http://arkitus.com/?id=5
http://arkitus.com/?id=16
Re: gravity
Reply #3 - Oct 5th, 2005, 12:16pm
 
Code:

particle [] p = new particle [10];
void setup(){
for(int i = 0; i < p.length; i++){
p[i] = new particle(random(400),random(400),random(2,12));
}
size (400,400);
ellipseMode(CENTER);
smooth();
}
void draw(){
background (255);
for(int i = 0; i<p.length; i++){
p[i].gmove();
p[i].draw();
}
line(mouseX,0,mouseX,height);
}
class particle{
float x,y,thisMass,xVelocity;
particle (float x, float y, float thisMass){
this.x = x;
this.y = y;
this.thisMass = thisMass;
xVelocity = 0;
}
void gmove(){
float mouseMass = 5;
float distX = mouseX - x;
//introduce the y and use pythagoras for the distance with two axises
/*
float distY = mouseY - y;
float dist = sqrt(sq(distX) + sq(distY));
*/
float dist = abs(distX); //remove this line for two axises
//kill acceleration to create an orbit if too near mouse
if (dist < 15){
dist = 15;
}
//gravity equations - x axis only so you can pick it apart
float grav = (thisMass * mouseMass) / sq(dist) * 5;
float xGrav = grav * (distX/dist);
float xAccel = xGrav / thisMass;
xVelocity += xAccel;
x += xVelocity;
/*
float yGrav = grav * (distY/dist);
float yAccel = yGrav / thisMass;
yVelocity += yAccel;
y += yVelocity;
*/
}
void draw(){
ellipse(x,y,thisMass,thisMass);
}
}
Re: gravity
Reply #4 - Oct 6th, 2005, 7:54am
 
All very nice fellas.
I hope to code as well some day Tongue

Thanks
Page Index Toggle Pages: 1