Advertisement

JOGL Enemy Space Ship dont point correctly to the player, HELP!

Started by July 11, 2011 12:54 PM
1 comment, last by cicerohellmann 13 years, 4 months ago
I have a big problem when im trying to point my enemy ships to the player, im braking my head to find an answer and i know its right in front of me but i cant see it!

i F SOME ONE HAVE ANY CLUES OUT THERE ILL BE GLAD TO HEAR AND + i suck in math so this can be the big deal with my problem...

ok

down there is my whole enemy code!

take a look and ask anything you guys want!

the enemy already go to the player location but i did some pog to make it turn to the player...

the enemy, as the player, rotate in 3 axis and this axis are 3x3 vectors

what i need to know exactly is what axis i should set here

MathUtil.rotacionaGLViaVetores(Player.eixoZ, Player.eixoY, Player.eixoX);

what is the math i should do to find the position of the player in the world ( i know how to do that ) and how to calculate the axis for this rate math funcion!


import static org.lwjgl.opengl.GL11.glColor4f;
import static org.lwjgl.opengl.GL11.glPopMatrix;
import static org.lwjgl.opengl.GL11.glPushMatrix;
import static org.lwjgl.opengl.GL11.glRotatef;
import static org.lwjgl.opengl.GL11.glScaled;
import static org.lwjgl.opengl.GL11.glTranslatef;

import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.GLContext;

import matematcbase.MathUtil;
import matematcbase.Matrix4x4;
import matematcbase.Vector3f;
import obj.ObjModel;

public class Inimigo extends Entidade {

public final static short MOVENDOSE_ALEATORIAMENTE = 0;
public final static short AGRUPANDO = 1;
public final static short MOVENDO_E_ATIRANDO = 2;
public final static short FUGINDO = 3;

public short estado = 0;

ObjModel meuModelo;

Vector3f vetorDeAproximacao;

boolean tomoChumbo = false;

// Tiro tiro;
float mx, my;
boolean freeMotion = true;
public static float playerAlpha = 1.0f;
int loadTiro = 0;
public static float velRotation = 190.0f;

Player playerQueDevoMatar;

boolean teste = true;
int tempo = 0;


public Inimigo(float X, float Y, float Z, float raio, float velX,
float velY, float velZ, String modeloDaNaveDoInimigo, Player player) {
super(X, Y, Z, raio, velX, velY, velZ);
// TODO Auto-generated constructor stub

meuModelo = new ObjModel();
meuModelo.loadObj(modeloDaNaveDoInimigo);
playerQueDevoMatar = player;
vetorDeAproximacao = new Vector3f();
vivo = true;
vida = 100;
estado = MOVENDO_E_ATIRANDO;
}

void rotacionaEixos() {

}

public void movimentacao(double diftime) {

if(Player.X>this.X+0.5){

rotAngleY = (float) -1;
Matrix4x4 k = new Matrix4x4();
k.setRotate(rotAngleY, eixoY.x, eixoY.y, eixoY.z);
k.transform(eixoZ);
k.transform(eixoY);
k.transform(eixoX);
eixoZ.Normalize();
eixoY.Normalize();
eixoX.Normalize();
}
if(Player.X<this.X-0.5){

rotAngleY = (float) 1;
Matrix4x4 k = new Matrix4x4();
k.setRotate(rotAngleY, eixoY.x, eixoY.y, eixoY.z);
k.transform(eixoZ);
k.transform(eixoY);
k.transform(eixoX);
eixoZ.Normalize();
eixoY.Normalize();
eixoX.Normalize();
}
if(Player.Y>this.Y+0.5){

rotAngleX = (float) -1;
Matrix4x4 k = new Matrix4x4();
k.setRotate(rotAngleX, eixoY.x, eixoY.y, eixoY.z);
k.transform(eixoZ);
k.transform(eixoY);
k.transform(eixoX);
eixoZ.Normalize();
eixoY.Normalize();
eixoX.Normalize();
}
if(Player.Y<this.Y-0.5){

rotAngleX = (float) 1;
Matrix4x4 k = new Matrix4x4();
k.setRotate(rotAngleX, eixoY.x, eixoY.y, eixoY.z);
k.transform(eixoZ);
k.transform(eixoY);
k.transform(eixoX);
eixoZ.Normalize();
eixoY.Normalize();
eixoX.Normalize();
}

// rotAngleX = (float) 1;
// Matrix4x4 k = new Matrix4x4();
// k.setRotate(rotAngleX, eixoX.x, eixoX.y, eixoX.z);
// k.transform(eixoZ);
// k.transform(eixoY);
// k.transform(eixoX);
// eixoZ.Normalize();
// eixoY.Normalize();
// eixoX.Normalize();
//
// movimenta para a esquerda e direita

// rotAngleY = (float) 1;
// Matrix4x4 m = new Matrix4x4();
// m.setRotate(rotAngleY, eixoY.x, eixoY.y, eixoY.z);
// m.transform(eixoZ);
// m.transform(eixoY);
// m.transform(eixoX);
// eixoZ.Normalize();
// eixoY.Normalize();
// eixoX.Normalize();
}

public void IA(double Difftime) {

switch (estado) {

case MOVENDOSE_ALEATORIAMENTE:

break;

case AGRUPANDO:

break;

case MOVENDO_E_ATIRANDO:
//


// X += vetorDeAproximacao.x * velX * Difftime / 1000.0f;
// Y += vetorDeAproximacao.y * velY * Difftime / 1000.0f;
// Z += vetorDeAproximacao.z * velZ * Difftime / 1000.0f;
//

// X += vetorDeAproximacao.x *eixoZ.x * velX * Difftime / 1000.0f;
// Y += vetorDeAproximacao.y *eixoZ.y * velY * Difftime / 1000.0f;
// Z += vetorDeAproximacao.z * eixoZ.z * velZ * Difftime / 1000.0f;

// if(vetorDeAproximacao.x > eixoX.x){
// rotAngleZ = -(float) (90 * Difftime / 1000.0f);
// Matrix4x4 m = new Matrix4x4();
// m.setRotate(rotAngleZ, eixoY.x, eixoY.y, eixoY.z);
// m.transform(eixoZ);
// m.transform(eixoY);
// m.transform(eixoX);
// eixoZ.Normalize();
// eixoY.Normalize();
// eixoX.Normalize();
// }

if (colideCicrular(vetorDeAproximacao.x, vetorDeAproximacao.y,
vetorDeAproximacao.z, 1.0f)) {
encontraVetorDeAproximacao();

}

break;

case FUGINDO:

break;
}

}

@Override
void simulaSe(double Diftime) {
// TODO Auto-generated method stub
// super.simulaSe(Diftime);

tempo++;
movimentacao(Diftime);
if (playerQueDevoMatar.listaDeTiros != null) {
for (int i = 0; i < playerQueDevoMatar.listaDeTiros.size(); i++) {
Tiro tiro = (Tiro) playerQueDevoMatar.listaDeTiros.get(i) ;

if(colideCicrular(tiro.X, tiro.Y, tiro.Z, tiro.raio))
{
tomoChumbo = true;
vida -= 50;
playerQueDevoMatar.listaDeTiros.remove(i);

}else{
tomoChumbo = false;
}

}

}

if (playerQueDevoMatar.listaDeMisseis != null) {
for (int i = 0; i < playerQueDevoMatar.listaDeMisseis.size(); i++) {
Missil missil = (Missil) playerQueDevoMatar.listaDeMisseis.get(i) ;

if(colideCicrular(missil.X, missil.Y, missil.Z, missil.raio))
{
tomoChumbo = true;
vida -= 100;
missil.timer=missil.tempoDaExplosão+1;


}else{
tomoChumbo = false;
}

}

}

if(vida <= 0)
{
X = 10 - (float) (Math.random()*20);
Y = 10 - (float) (Math.random()*20);
Z = 10 - (float) (Math.random()*20);
vida = 100;
}

switch (estado) {

case MOVENDOSE_ALEATORIAMENTE:

break;

case AGRUPANDO:

break;

case MOVENDO_E_ATIRANDO:

// if (teste) {

// só pra procurar na primeira iteração da classe.
encontraVetorDeAproximacao();

teste = false;
//
break;

case FUGINDO:

break;
}

IA(Diftime);
}

@Override
void desenhaSe() {
// TODO Auto-generated method stub
glPushMatrix();
{


glTranslatef(X, Y, Z);


glScaled(0.005f, 0.005f, 0.005f);




glRotatef(180, 0, 0, 0);

MathUtil.rotacionaGLViaVetores(Player.eixoZ, Player.eixoY, Player.eixoX);






meuModelo.desenhase();

}
glPopMatrix();

}

void encontraVetorDeAproximacao() {

vetorDeAproximacao.x = Player.X - this.X;
vetorDeAproximacao.y = Player.Y - this.Y;
vetorDeAproximacao.z = Player.Z - this.Z;

vetorDeAproximacao.Normalize();

}

public boolean colideCicrular(float x, float y, float z, float Raio) {

float difx = x - this.X;
float dify = y - this.Y;
float difz = z - this.Z;

float dist = difx * difx + dify * dify + difz * difz;

if (((Raio + this.raio) * (Raio + this.raio)) > dist) {
return true;
}

return false;
}

public void trataMorte(Player player)
{

}

}
Here's a good place to start.

http://en.wikipedia.org/wiki/Euclidean_vector

Dave Mark - President and Lead Designer of Intrinsic Algorithm LLC
Professional consultant on game AI, mathematical modeling, simulation modeling
Co-founder and 10 year advisor of the GDC AI Summit
Author of the book, Behavioral Mathematics for Game AI
Blogs I write:
IA News - What's happening at IA | IA on AI - AI news and notes | Post-Play'em - Observations on AI of games I play

"Reducing the world to mathematical equations!"

Advertisement

Here's a good place to start.

http://en.wikipedia....uclidean_vector



Fking thank you
: )

im doing a game development course but they didnt gave me a good mathematic base... and im getting raped, im good at the game craft art im just not good with math..since this 2 are very close to each other i may be not the best as i thought


This topic is closed to new replies.

Advertisement