Commit 45401bba authored by Rominho15's avatar Rominho15
Browse files

Version 8.4: Rebond réaliste (selon angle)

parent f304624b
......@@ -3,6 +3,7 @@ package com.example.rleger.cassebrique.Controller;
import android.content.Context;
import android.content.Intent;
import android.graphics.Point;
import android.util.Log;
import android.view.Display;
import android.view.WindowManager;
......@@ -93,10 +94,10 @@ public class GameController {
public void update() {
int cx=gameModel.getBall().getX();
int cy=gameModel.getBall().getY();
int vx=gameModel.getBall().getVx();
int vy=gameModel.getBall().getVy();
float cx=gameModel.getBall().getX();
float cy=gameModel.getBall().getY();
float vx=gameModel.getBall().getVx();
float vy=gameModel.getBall().getVy();
// teste la collision avec les bordures : si c'est le cas on change le signe de la composante vitesse correspondante (rebond sur les bords)
if(cx<=0 || cx>= gameModel.getGround().getWidth()) vx*=-1;
......@@ -112,30 +113,18 @@ public class GameController {
}
// teste la collision entre la batte et la balle (rebond sur la batte)
int col = collides(gameModel.getBall(), gameModel.getBat());
if(col != 0) {
int mod = xCompare(gameModel.getBall(),gameModel.getBat());
if(col == 1) {
vy *= -1;
vx += mod;
if(vx == 0) vx -= mod;
}
else
vx*=-1;
float[] col = collides(gameModel.getBall(), gameModel.getBat(),vx);
if(col != null) {
vx = col[0];
vy = col[1];
}
// Si il existe une Bat2, teste la collision entre la batte et la balle (rebond sur la batte)
if(gameModel.getBat2() != null) {
col = collides(gameModel.getBall(), gameModel.getBat2());
if(col != 0) {
int mod = xCompare(gameModel.getBall(),gameModel.getBat2());
if(col == 1) {
vy *= -1;
vx += mod;
if(vx == 0) vx -= mod;
}
else
vx*=-1;
col = collides(gameModel.getBall(), gameModel.getBat2(),vx);
if(col != null) {
vx = col[0];
vy = -col[1];
}
}
......@@ -146,9 +135,9 @@ public class GameController {
for (int i=0 ; i < brickGroup.getSize() ; i++) {
Brick b = brickGroup.getBrick(i);
col = collides(gameModel.getBall(), b);
if(col != 0) {
if(col == 1)
int colB = collides(gameModel.getBall(), b);
if(colB != 0) {
if(colB == 1)
vy*=-1;
else
vx*=-1;
......@@ -221,17 +210,44 @@ public class GameController {
gameModel.getBat().setX(oldX + addX);
}
public int collides(Ball ba, Bat bt) {
int[] circle = {ba.getX(),ba.getY(),ba.getRayon()};
public float[] collides(Ball ba, Bat bt, float vx) {
int[] circle = {(int)ba.getX(),(int)ba.getY(),ba.getRayon()};
int[] rect = {bt.getX()+bt.getWidth()/2,bt.getY()+bt.getHeight()/2,bt.getWidth(),bt.getHeight()};
return collides(rect,circle);
int col = collides(rect,circle);
float[] vitesse = null;
if(col != 0) {
float[] res = new float[2];
int angle = (180 - (180 * (circle[0] - bt.getX()) / (rect[2])));
Log.d("ok","touche");
Log.d("angle",""+angle);
double rad = (angle * Math.PI / 180);
res[0] = (float)Math.cos(rad);
res[1] = -(float)Math.sin(rad);
vitesse = new float[2];
if(vx >= 0)
vitesse[0] = Math.abs(res[0])+1;
else
vitesse[0] = (-Math.abs(res[0]))-1;
if(res[1] >= 0)
vitesse[1] = res[1]+1;
else
vitesse[1] = res[1]+-1;
}
return vitesse;
}
public int collides(Ball ba, Brick br) {
int res = 0;
if(br != null) {
int[] circle = {ba.getX(),ba.getY(),ba.getRayon()};
int[] circle = {(int)ba.getX(),(int)ba.getY(),ba.getRayon()};
int[] rect = {br.getX()+br.getWidth()/2,br.getY()+br.getHeight()/2,br.getWidth(),br.getHeight()};
res = collides(rect,circle);
......@@ -250,7 +266,7 @@ public class GameController {
if (circleDistance.y > (rect[3]/2 + circle[2])) { return 0; }
if (circleDistance.x <= (rect[2]/2)) { return 1; }
if (circleDistance.y <= (rect[3]/2)) { return 2; }
if (circleDistance.y <= (rect[3]/2)) { return 1; }
int cornerDistance_sq = (circleDistance.x - rect[2])^2 +
(circleDistance.y - rect[3]/2)^2;
......@@ -266,10 +282,10 @@ public class GameController {
public int xCompare(Ball ba, Bat bt) {
int res = 0;
int positionBallByBat = ba.getX() - bt.getX();
int positionBallByBat = (int)ba.getX() - bt.getX();
int gap = bt.getWidth()/3;
int vitesse = ba.getVx();
float vitesse = ba.getVx();
if(positionBallByBat < gap ) {
res = -1;
......
......@@ -5,10 +5,10 @@ package com.example.rleger.cassebrique.Model;
*/
public class Ball {
private int x;
private int y;
private int vx;
private int vy;
private float x;
private float y;
private float vx;
private float vy;
private int rayon = 10;
public Ball(int xi, int yi) {
......@@ -18,35 +18,35 @@ public class Ball {
vy = 2;
}
public int getX() {
public float getX() {
return x;
}
public void setX(int x) {
public void setX(float x) {
this.x = x;
}
public int getY() {
public float getY() {
return y;
}
public void setY(int y) {
public void setY(float y) {
this.y = y;
}
public int getVx() {
public float getVx() {
return vx;
}
public void setVx(int vx) {
public void setVx(float vx) {
this.vx = vx;
}
public int getVy() {
public float getVy() {
return vy;
}
public void setVy(int vy) {
public void setVy(float vy) {
this.vy = vy;
}
......
......@@ -162,8 +162,8 @@ public class GameView extends View {
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
int xBall = gameControlleur.getGameModel().getBall().getX();
int yBall = gameControlleur.getGameModel().getBall().getY();
float xBall = gameControlleur.getGameModel().getBall().getX();
float yBall = gameControlleur.getGameModel().getBall().getY();
int sBall = gameControlleur.getGameModel().getBall().getRayon();
canvas.drawCircle(xBall, yBall, sBall, paintBall);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment