zoukankan      html  css  js  c++  java
  • processing ball gravity simulation

    模拟方案1:

    void setup() {
      size(500, 500);
      smooth();
    }
     
    float yPos=25;
    float g=.15;
    float acceleration=0;
     
    void draw() {
      background(0);
      makeball();
      yPos += acceleration;
      //ball drop or bounce back
      acceleration += g;
      //accelerate speed add g
      //when bounce back, -acceleration+g = slower speed
      if (yPos>height-25) {
        acceleration=-acceleration/1.15;
        //when hit bottom, bounce back, but drop down at lower height
        //Thanks to Mauricio
      }
    }
     
    void makeball() {
      ellipse(width/2, yPos, 50, 50);
      fill(255);
    }

    方案2:

    // Learning Processing
    // Daniel Shiffman
    // http://www.learningprocessing.com
    
    // Example 5-9: Simple Gravity
    
    float x = 100;   // x location of square
    float y = 0;     // y location of square
    
    float speed = 0;   // speed of square
    
    // A new variable, for gravity (i.e. acceleration).   
    // We use a relatively small number (0.1) because this accelerations accumulates over time, increasing the speed.   
    // Try changing this number to 2.0 and see what happens.
    float gravity = 0.1;  
    
    void setup() {
      size(200,200);
    
    }
    
    void draw() {
      background(255);
    
      // Display the square
      fill(175);
      stroke(0);
      rectMode(CENTER);
      rect(x,y,10,10);
      
      // Add speed to location.
      y = y + speed;
      
      // Add gravity to speed.
      speed = speed + gravity;
      
      // If square reaches the bottom
      // Reverse speed
      if (y > height) {
        // Multiplying by -0.95 instead of -1 slows the square down each time it bounces (by decreasing speed).   让speed乘以-0.95《1,使高度小于最开始的高度。
        // This is known as a "dampening" effect and is a more realistic simulation of the real world (without it, a ball would bounce forever).
        speed = speed * -0.95;  
      }
    }

    2种方法都差不多,只不过参数不同而已。

  • 相关阅读:
    攻防世界 xff_referer
    攻防世界 simple——js
    攻防世界 你知道什么是cookie吗?
    攻防世界 你知道php备份文件吗?
    攻防世界 robots题
    bugku 管理员系统
    bugku 速度要快
    2016北京集训测试赛(七)Problem A: 自动机
    BZOJ 4543 2016北京集训测试赛(二)Problem B: thr
    BZOJ 4631 踩气球
  • 原文地址:https://www.cnblogs.com/youxin/p/2996416.html
Copyright © 2011-2022 走看看