处理——向后翻车跟头

Processing - Backwards car somersault

我必须编写一个程序,显示一辆汽车以正确的弹丸运动在空中飞行时向后翻筋斗,并着陆。

我目前的代码允许我让汽车在空中飞行。我只需要将汽车向后旋转并正确着陆即可。

我目前拥有的代码:

float x,y;
float s = 20;
float theta = 0.64;
float g = 9.8;
float t =0.0;
PImage img;
void setup(){
  size(750,600);
  img = loadImage("myimage.gif");

  noStroke();
  smooth();
}

void draw(){
   fill(0, 22);
   rect(0, 0, width, height);
   t = t+0.01;
   x = s*cos(theta)*t;
   y = (s*t*sin(theta))-(0.5*g*t*t);
   fill(255);
   image(img,x*15,550-y*5);
}

我用于汽车的图片:

使用rotate() to rotate the car around its relative position and translate()将汽车移动到它的位置。

translate(x*15,550-y*5);
rotate(r);
image(img, 0, 0);

旋转角度(r)必须随时间增加(r= f(t)):

float r = alpha * t;

看例子:

float s = 20;
float theta = 0.64;
float g = 9.8;
float t = 0.0;
float alpha = -2.2;
PImage img;

void setup(){
    size(750,600);
    img = loadImage("car.gif");
    noStroke();
    smooth();
}

void draw(){
   float x = s*cos(theta)*t;
   float y = (s*t*sin(theta))-(0.5*g*t*t);
   float r = alpha * t;
   t += 0.01;

   background(0);

   translate(x*15,550-y*5);
   rotate(r);
   image(img, 0, 0);

   if (t > 3.141529) 
       t = 0.0;
}