Advertisement
Guest User

Processing 2D physics demo

a guest
Mar 15th, 2014
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.81 KB | None | 0 0
  1. // model
  2. float ball_x = 0, ball_y = 0; // [m]
  3. float ball_vx = 0, ball_vy = 0; // [m/s]
  4.  
  5. float FRAMERATE = 50;
  6. float dt = 1/FRAMERATE;
  7.  
  8. void setup() {
  9.   size(600,400);
  10.   background(200,230,255);
  11. }
  12.  
  13. void draw() {
  14.   update(dt);
  15.   // background(200,230,255);
  16.   fill(255,150,0);
  17.   ellipse(ball_x*50,height - ball_y*50, 20, 20);
  18. }
  19.  
  20. void mouseClicked () {
  21.   ball_x = 0;
  22.   ball_y = 0;
  23.   ball_vx = 2*mouseX/50;
  24.   ball_vy = 2*(height - mouseY)/50;
  25. }
  26.  
  27. void update(float time) {
  28.   // gravity
  29.   ball_vy = ball_vy - 9.8*time;
  30.  
  31.   // air resistance
  32.   float speed = sqrt(ball_vx*ball_vx + ball_vy*ball_vy);
  33.   float k = 0.1;
  34.   ball_vx = ball_vx - k*speed*ball_vx*time;
  35.   ball_vy = ball_vy - k*speed*ball_vy*time;
  36.  
  37.   // update position
  38.   ball_x = ball_x + ball_vx*time;
  39.   ball_y = ball_y + ball_vy*time;
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement