Skip to content

Instantly share code, notes, and snippets.

@rngtm
Last active December 12, 2015 07:38
Show Gist options
  • Save rngtm/4737466 to your computer and use it in GitHub Desktop.
Save rngtm/4737466 to your computer and use it in GitHub Desktop.
テスト
/*
* (x1,y1)と(x2,y2)を通る直線に速度(*vx,*vy)で移動する物体がぶつかった時の反射を計算するです。
* 直線に沿って摩擦frictionと反発係数reboundを設定できるです。
*/
void Refrection(double *vx,double *vy,double x1,double y1,double x2,double y2,double friction,double rebound){
double tanx;
double a11,a12,a21,a22;
double bunbo;
double x,y;
if(x1==x2){//鉛直な線分
*vx*=-rebound;
*vy*=friction;
return;
}else if(y1==y2){//水平な線分
*vx*=friction;
*vy*=-rebound;
return;
}
tanx = (y2-y1)/(x2-x1);
bunbo=1+tanx*tanx;
a11 = (friction-rebound*tanx*tanx)/bunbo;
a12 = (friction+rebound)*tanx/(bunbo*2.0);
a21 = (friction+rebound)*tanx/(bunbo*2.0);
a22 = (rebound-friction*tanx*tanx)/bunbo;
x = a11**vx+a12**vy;
y = a21**vx-a22**vy;
*vx=x*rebound;
*vy=y*rebound;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment