Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // model
- float ball_x = 0, ball_y = 0; // [m]
- float ball_vx = 0, ball_vy = 0; // [m/s]
- float FRAMERATE = 50;
- float dt = 1/FRAMERATE;
- void setup() {
- size(600,400);
- background(200,230,255);
- }
- void draw() {
- update(dt);
- // background(200,230,255);
- fill(255,150,0);
- ellipse(ball_x*50,height - ball_y*50, 20, 20);
- }
- void mouseClicked () {
- ball_x = 0;
- ball_y = 0;
- ball_vx = 2*mouseX/50;
- ball_vy = 2*(height - mouseY)/50;
- }
- void update(float time) {
- // gravity
- ball_vy = ball_vy - 9.8*time;
- // air resistance
- float speed = sqrt(ball_vx*ball_vx + ball_vy*ball_vy);
- float k = 0.1;
- ball_vx = ball_vx - k*speed*ball_vx*time;
- ball_vy = ball_vy - k*speed*ball_vy*time;
- // update position
- ball_x = ball_x + ball_vx*time;
- ball_y = ball_y + ball_vy*time;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement