【プログラムの説明】
このプログラムはProcessing上で、
BallとPlayerが衝突した時に「collision!!」という文字列を表示するプログラムです。
【問題】
しかし、実際に動かしてみると、衝突していないのに文字列が表示されてしまいます。
原因がわかりません。
Java
1 2/* クラスの定義 -----> */ 3class Player{ 4 private int x, y; 5 private int r; 6 7 public Player(int r){ 8 this.r = r; 9 } 10 11 public void update(){ 12 x = mouseX; 13 y = mouseY; 14 } 15 16 public void render(){ 17 fill(#FF0000); 18 ellipse(x,y,r,r); 19 } 20 21 public int getX(){ return x; } 22 public int getY(){ return y; } 23 public int getR(){ return r; } 24 25} 26 27 28class Ball{ 29 private int x, y; 30 private int r; 31 32 public Ball(int x, int y, int r){ 33 this.x = x; 34 this.y = y; 35 this.r = r; 36 } 37 38 public void update(){ 39 40 } 41 42 public void render(){ 43 fill(#FF00FF); 44 ellipse(x,y,r,r); 45 } 46 47 public int getX(){ return x; } 48 public int getY(){ return y; } 49 public int getR(){ return r; } 50 51} 52 53 54class Collision{ 55 56 public void collideBall(Player player, Ball ball){ 57 int sx = player.getX() - ball.getX(); 58 int sy = player.getY() - ball.getY(); 59 int sr = player.getR() + ball.getR(); 60 61 if( (sx*sx)+(sy*sy) <= (sr*sr) ) 62 text("collision!!", width-50, 10); 63 } 64 65} 66 67Ball ball = new Ball(100,100,50); 68Player player = new Player(50); 69Collision collision = new Collision(); 70 71void setup(){ 72 size(500,500); 73} 74 75 76void draw(){ 77 background(#00FF00); 78 79 ball.update(); 80 player.update(); 81 ball.render(); 82 player.render(); 83 84 collision.collideBall(player, ball); 85}
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/06/24 10:56