Collision Java

Résolu/Fermé
matt2421 Messages postés 16 Date d'inscription jeudi 19 mai 2016 Statut Membre Dernière intervention 28 novembre 2016 - Modifié par KX le 13/07/2016 à 00:42
 SpeedZealot - 12 juil. 2016 à 14:49
salut. voici mes classes "bubble" et "Fish" que j'ai cree.
j'aimerai que losqu'un poisson rencontre une bulle, que ce derniere disparaisse. comment dois je l'implementer?

Classe Fish

package Model;

import java.util.Random;

/**


* 

* Ozeanobjekt Fisch

* 

* @author Nitharshan
 *


*/
public class Fish extends OceanObject {

 private static final long serialVersionUID = 1L;
 private Direction direction = Direction.Left;

 /** Direction shows whether the fish looks to the left or right.*/
 public enum Direction
 {
  Right,Left
 }
 
 /**The constructor of Fish, he takes care of the ocean in which the object is,


* the size of the objects and the position. */
 public Fish(Ocean ocean, int sizeWidth, int sizeHeight, int x, int y, String name) 
 {
  super(ocean, sizeWidth, sizeHeight, x, y, name);
 }
 
 /** The Move function of fish calculates a random new location, 


* which is then checked whether it is still in the ocean

*  and whether on the position itself another object. If so, 

*  then the fish is not moving and changes its direction.*/
 @Override
 public void move(int width, int depth)
 {
  int posX, posY ; // Die Position mit einer x-koordinate und einer y-koordinate werden initialisiert.
  Random rn = new Random(); // es wird eine beliebige zahl generiert.
  if(rn.nextInt(500) == 0) // Aendert die richtung mit einer wahrscheinlichkeit von 1 zu 500.
  {
   changeDirection();
  }
  
  if(direction == Direction.Left) 
  {
   posX = this.getX() - (rn.nextInt(4) + 2); // Der Fisch schwimmt nach links
  }
  else
  {
   posX = this.getX() + rn.nextInt(4) + 1; // Der Fisch schwimmt nach rechts.
  }
      posY = this.getY() + rn.nextInt(3) - 1; // und rauf oder runder.
  
  if(ocean.isPositionFree(this, posX, posY)) // prueft ob position frei ist.
  {
   this.setX(posX); // positionen werden bestaetigt.
   this.setY(posY);// positionen werden bestaetigt.
   
   if(posX < sizeWidth/2) // wird der linke rand ueberschritten?
   {
    changeDirection(); // Dann wird die richtung geaendert.
    this.setX(sizeWidth/2); // und die position auf den rand gelegt.
   }
   if(posX > width - sizeWidth/2) // wird der rechte rand ueberschritten?
   {
    changeDirection(); 
    this.setX(width - sizeWidth/2);
   }
   if(posY > depth - sizeHeight/2) // wird der untere rand ueberschritten?
   {
    this.setY(depth - sizeHeight/2);
   }
   if(posY < sizeHeight/2) // oder der obere?
   {
    this.setY(sizeHeight/2);
   }
  }
  else
  {
   changeDirection(); // ist die position nicht frei wird die richtung geaendert.
  }
 }
 
 /** Query the direction */
 public Direction getDirection()
 {
  return direction; // gibt die richtung zurueck.
 }
 
 /** Changes direction.


* Æ’ndert die Richtung des Fishes ;)*/
 public void changeDirection() 
 {
  if(direction == Direction.Left) // ist die richtung links
  {
   direction = Direction.Right;// wird sie zu rechts 
  }
  else
  {
   direction = Direction.Left; // und umgekert.
  }
 }

}



classe Bubble
package Model;

/**


* 

* Ozeanobjekt Blase

* 

* @author Nitharshan
 *


*/
public class Bubble extends OceanObject {

 /**


* 

*/
 private static final long serialVersionUID = 1L;
 private boolean change = false;

 public Bubble(Ocean ocean, int sizeWidth, int sizeHeight, int x, int y, String name) {
  super(ocean, sizeWidth, sizeHeight, x, y, name);
 }

 /*


* 

* Bewegung einer Blase

* 

*/
 public void move(int width, int depth) {
  if(!getAway()){
  int newYPos = getY() + 3;
  int newXPos = getX();
  if (change) {
   newXPos += (int) (Math.random() * 8 + 1);
   change = false;
  } else {
   newXPos -= (int) (Math.random() * 8 + 1);
   change = true;
  }

  if (newYPos > depth) {
   setAway(true);
  } else {
   setY(newYPos);
   setX(newXPos);
  }
  }
 }
}
A voir également:

1 réponse

Bonjour

J'immagine, que pour faire se déplacer tes poissons, tu utilise une liste ou un tableau, donc pour faire disparaître le poisson tu peux simplement le retirer du tableau ou de la liste.

après pour ce qui est de la collision, je te propose de changer ta méthode move pour qu'elle prenne en paramètres la liste de toutes les bulles, et que ta méthode renvoi un booléen (true si collision avec une bulle)

Dans ta méthode move, tu n'a plus qu'a regarder si ton poisson est en collision avec une bulle après le changement de ses coordonnées.

C'est une solution rapide et facile, mais pas forcément la plus optimisée

P.S : pour les morceaux de code, tu à la balise <code java>
0
matt2421 Messages postés 16 Date d'inscription jeudi 19 mai 2016 Statut Membre Dernière intervention 28 novembre 2016
12 juil. 2016 à 14:17
Je ne suis pas un As de java, si tu pouvais me montrer directement via mon code, ce serait cool!
0
SpeedZealot > matt2421 Messages postés 16 Date d'inscription jeudi 19 mai 2016 Statut Membre Dernière intervention 28 novembre 2016
Modifié par SpeedZealot le 12/07/2016 à 14:50
Donc grossièrement, ca va donner ca :

public class Fish extends OceanObject {

//tes autres méthodes

@Override 
public boolean move(int width, int depth, List<OceanObject> oobject) {
   //ton ancien code
   return checkCollision(bulles);
}

public boolean checkCollision(List<OceanObject> oobject) {
   for (OceanObject oo : oobject) {
      if (oo instanceof Bulle) { //si c'est une bulle (ca peut etre un poisson aussi mais ta collision ne compte que si c'est une bulle)
          Bubble b = (Bubble) oo;
          if () {// ici faire des maths (j'ai la flemme..) pour voire si ton poisson touche la bulle avec les coordonnées et le rayon de la bulle
             return true; //on renvoi le fait qu'il y a bien collision
          } 
      }
   }
   return false; //Aucune collision trouvée
}
}

public class Ocean {

public List<OceanObject> oceanObjects;

//tes autres méthodes

public void faireBougerLesPoissonsEtLesBulles() {
  Iterator<OceanObject> iter = oceanObject.iterator();
  while (iter.hasNext()) { //on parcours toute la liste de tes OceanObject
    OceanObject oo = iter.next();
    if (oo.move(width, depth, oceanObject)) { //si la méthode move renvoi une collision
        iter.remove(); //on supprime le poisson ou la bulle
    }
  }
}


Je t'invite tout de même à faire quelques essais pour trouver une solution plus optimisée, la c'est vraiment barbare comme solution ^^
0