udiudi
Messages postés6Date d'inscriptionvendredi 1 mai 2015StatutMembreDernière intervention 2 juin 2015
-
Modifié par baladur13 le 1/06/2015 à 15:35
bonjour j'ai trouvé ce code sur internet, je pensais le reprendre pour faire le terrain de mon jeux de tanks, puisqu'il génère un terrain aléatoire. Il fonctionne très bien seulement je ne le comprends pas très bien, est ce que quelqu'un d'à l'aise avec les courbes de Bézier et java pourrait me l'expliquer svp? merci beaucoup!!!
package project.java.tankwars;
import java.awt.Point;
public class Terrain {
private Point[] curve;
private Point[] derivative;
private Point[] Pointsdecontrole;
private int counter1, counter2;
public Terrain(){
//tableau qui contient info sur le terrain
curve = new Point[1300];
derivative = new Point[1300];
//the control point that are generated semi-randomly and that define the quadratic Bezier curves (the hills)
Pointsdecontrole = new Point[11];
Pointsdecontrole[0] = new Point(0, 672);
Pointsdecontrole[2] = new Point((int)(550), 672);
Pointsdecontrole[1] = new Point((int)((Pointsdecontrole[2].x-Pointsdecontrole[0].x)/2), (int)(Math.random()*600));
Pointsdecontrole[3] = new Point((int)(440), 672);
Pointsdecontrole[5] = new Point((int)(860), 672);
Pointsdecontrole[4] = new Point((int)(Pointsdecontrole[3].x+(Pointsdecontrole[5].x-Pointsdecontrole[3].x)/2), (int)(Math.random()*600));
Pointsdecontrole[6] = new Point((int)(750), 672);
Pointsdecontrole[8] = new Point(1300, 672);
Pointsdecontrole[7] = new Point((int)(Pointsdecontrole[6].x+(Pointsdecontrole[8].x-Pointsdecontrole[6].x)/2), (int)(Math.random()*600));
//calculating the coordinates of the first curve (hill)
Point[] curve1 = new Point[550];
Point[] derivative1 = new Point[550];
Point p0 = new Point(Pointsdecontrole[0].x, Pointsdecontrole[0].y);
Point p1 = new Point(Pointsdecontrole[1].x, Pointsdecontrole[1].y);
Point p2 = new Point(Pointsdecontrole[2].x, Pointsdecontrole[2].y);
double t = 0.0;
double step = 1.0/550;
for(int i=0; i<550; i++){
derivative1[i] = add(mult(2, mult((1-t), diff(p1, p0))), mult(2, mult(t, diff(p2, p1))));
double theta = Math.asin(derivative1[i].y/Math.sqrt((derivative1[i].x*derivative1[i].x+derivative1[i].y*derivative1[i].y)));
int dx =(int)((theta/Math.abs(theta))*33*Math.cos(Math.PI/2-Math.abs(theta)));
int dy =(int)(33*Math.sin(Math.PI/2-Math.abs(theta)));
int x = (int)(Math.pow((1-t), 2)*p0.x+2*(1-t)*t*p1.x+t*t*p2.x);
int y = (int)(Math.pow((1-t), 2)*p0.y+2*(1-t)*t*p1.y+t*t*p2.y);
curve1[i] = new Point(x+dx, y-dy);
t += step;
}
//calculating the coordinates of the second curve (hill)
Point[] curve2 = new Point[420];
Point[] derivative2 = new Point[420];
p0 = new Point(Pointsdecontrole[3].x, Pointsdecontrole[3].y);
p1 = new Point(Pointsdecontrole[4].x, Pointsdecontrole[4].y);
p2 = new Point(Pointsdecontrole[5].x, Pointsdecontrole[5].y);
t = 0.0;
step = 1.0/420;
for(int i=0; i<420; i++){
derivative2[i] = add(mult(2, mult((1-t), diff(p1, p0))), mult(2, mult(t, diff(p2, p1))));
double theta = Math.asin(derivative2[i].y/Math.sqrt((derivative2[i].x*derivative2[i].x+derivative2[i].y*derivative2[i].y)));
int dx =(int)((theta/Math.abs(theta))*33*Math.cos(Math.PI/2-Math.abs(theta)));
int dy =(int)(33*Math.sin(Math.PI/2-Math.abs(theta)));
int x = (int)(Math.pow((1-t), 2)*p0.x+2*(1-t)*t*p1.x+t*t*p2.x);
int y = (int)(Math.pow((1-t), 2)*p0.y+2*(1-t)*t*p1.y+t*t*p2.y);
curve2[i] = new Point(x+dx, y-dy);
t += step;
}
//calculating the coordinates of the cross point between the first and the second curve
for(int j=0; j<110; j++){
if(curve2[j].y==curve1[j+439].y+1 || curve2[j].y==curve1[j+439].y-1 ||curve2[j].y==curve1[j+439].y+2 || curve2[j].y==curve1[j+439].y-2 || curve2[j].y==curve1[j+439].y){
Pointsdecontrole[9] = new Point(j+437, curve2[j].y);
counter1 = j;
}
}
//calculating the coordinates of the third curve (hill)
Point[] curve3 = new Point[550];
Point[] derivative3 = new Point[550];
p0 = new Point(Pointsdecontrole[6].x, Pointsdecontrole[6].y);
p1 = new Point(Pointsdecontrole[7].x, Pointsdecontrole[7].y);
p2 = new Point(Pointsdecontrole[8].x, Pointsdecontrole[8].y);
t = 0.0;
step = 1.0/550;
for(int i=0; i<550; i++){
derivative3[i] = add(mult(2, mult((1-t), diff(p1, p0))), mult(2, mult(t, diff(p2, p1))));
double theta = Math.asin(derivative3[i].y/Math.sqrt((derivative3[i].x*derivative3[i].x+derivative3[i].y*derivative3[i].y)));
int dx =(int)((theta/Math.abs(theta))*33*Math.cos(Math.PI/2-Math.abs(theta)));
int dy =(int)(33*Math.sin(Math.PI/2-Math.abs(theta)));
int x = (int)(Math.pow((1-t), 2)*p0.x+2*(1-t)*t*p1.x+t*t*p2.x);
int y = (int)(Math.pow((1-t), 2)*p0.y+2*(1-t)*t*p1.y+t*t*p2.y);
curve3[i] = new Point(x+dx, y-dy);
t += step;
}
//calculating the coordinates of the cross point between the second and the third curve
for(int j=0; j<110; j++){
if(curve3[j].y==curve2[j+309].y+1 || curve3[j].y==curve2[j+309].y-1 || curve3[j].y==curve2[j+309].y){
Pointsdecontrole[10] = new Point(j+750, curve3[j].y);
counter2 = j;
}
}
//bringing all the coordinates together in the main array
for(int i=0; i<440+counter1; i++){
curve[i] = new Point(curve1[i].x, curve1[i].y);
derivative[i] = new Point(derivative1[i].x, derivative1[i].y);
}
for(int i=0; i<310-counter1+counter2; i++){
curve[i+440+counter1] = new Point(curve2[i+counter1].x, curve2[i+counter1].y);
derivative[i+440+counter1] = new Point(derivative2[i+counter1].x, derivative2[i+counter1].y);
}
for(int i=0; i<549-counter2; i++){
curve[i+749+counter2] = new Point(curve3[i+counter2].x, curve3[i+counter2].y);
derivative[i+749+counter2] = new Point(derivative3[i+counter2].x, derivative3[i+counter2].y);
}
}
public int getCurveXAt(int i){
return(curve[i].x);
}
public int getCurveYAt(int i){
return(curve[i].y);
}
public int getDerivativeXAt(int i){
return(derivative[i].x);
}
public int getDerivativeYAt(int i){
return(derivative[i].y);
}
public Point[] getControlPoints(){
return Pointsdecontrole;
}
public static Point add(Point a, Point b){
return(new Point(a.x+b.x, a.y+b.y));
}
public static Point mult(double a, Point b){
return(new Point((int)(a*b.x), (int)(a*b.y)));
}
public static Point diff(Point a, Point b){
return(new Point(a.x-b.x, a.y-b.y));
}
}