blog posts

databases (1) django (1) drupal (2) java (4) processing (7) wamp (1) yahoo pipes (1)

Sunday, 13 February 2011

Gravity tutorial

In this tutorial I explain how gravity can work

first you need to declare a variable called
x and y these are the x and y position

eg
float x = 100;
float y = 0;
float y2 = 600;
float y3 = 600;
// initial speed is set to zero
float speed = 0;
float speed2 = 0;
float speed3 = 0;
float gravity = 0.1;
float gravity2 = 0.02;
float gravity3 = 0.04;




void setup()
{

void setup()
{
// width and height of the sketch
size(400,800);
// the speed of the fram rate. 
frameRate(9.8);


 
 
}

// this code can be improved by using loops but i am showing basic principles

void draw()
{
background(255);
// Display the square
fill(0);
noStroke();
rectMode(CENTER);
// the x coordinate is static and has a global float set to 100
// the y coordinate starts at zero and is effected by the y varabile as well as the speed and gravity variable
rect(x,y,10,10);
// what this line of code does is takes the current y position and then adds the speed onto this position

y = y + speed;
// this will increment the gravity by 0.1 each time the draw method is run.

speed = speed + gravity ;
/*
rect(x,y2,10,10);

y2 = y2 + speed;
speed = speed + gravity2 ;
*/
fill(255,0,0);
ellipse(x,y2,10,10);
y2 = y2 + speed2;

speed2 = speed2 + gravity2;

fill(0,255,0);
ellipse(x,y3,10,10);

y3 = y3 + speed3;

speed3 = speed3 + gravity3;

 
}

No comments:

Post a Comment

Popular Posts