Hallo,
ich hätte da ein "kleines" Problem. Dieses besteht darin, dass ich momentan ein 2D Game programmiere, jedoch funktioniert die Kollisionsabfrage nicht so wie ich es will, bzw. garnicht.
Also hier ist der src:
Und hier der Code der Player-Klasse:
So, nun besteht das Problem darin, dass der Spieler einfach durch andere Objekte hindurch fallen kann, anstatt bei der Kollsision mit anderen Objekten zu stoppen. Hätte da vielleicht irgendjemand eine Idee?
Die x,y, velX und velY Variablen sind übrigens alles doubles.
Ich hoffe mal, das mir hier geholfen werden kann.
JavaGamer
ich hätte da ein "kleines" Problem. Dieses besteht darin, dass ich momentan ein 2D Game programmiere, jedoch funktioniert die Kollisionsabfrage nicht so wie ich es will, bzw. garnicht.
Also hier ist der src:
Java:
/**
* Controls the physics for the object. If it's true you will get physics, if false, no physics are enalbed, but
* the collision is always active!
*/
protected boolean physics = true;
/**
* The gravity
*/
public float gravity = 1.0F;
/** The maximum of speed */
public float speed = 159.0F / 60;
public Rectangle rect;
/**
* Override this method to load stuff for the game object.
*
* Don't forget to call this method in the init-method of your main class.
*/
public abstract void init();
/**
* Override this method to update the game object.
* Don't forget to call this method in the update-method of your main class.
*/
public abstract void update();
/**
* Override this method to render the game object.
* Don't forget to call this method in the render-method of your main class.
*
* @param graphics The java.awt.Graphics class. This is a must, that you can render stuff in this method.
*/
public abstract void render(Graphics g);
public boolean collide(GameObject tempObject)
{
int bx = (int) (x + velX);
int by = (int) (y + velY);
rect.setLocation((int)bx, (int)by);
if(!this.physics)
return false;
if(rect.intersects(tempObject.rect))
return true;
return false;
}
public void fall(GameObject tempObject)
{
if(!this.physics)
return;
if(!collide(tempObject))
{
//System.out.println("?");
this.velY += this.gravity;
if(this.velY > this.speed)
this.velY = this.speed;
}
else if(collide(tempObject))
{
//System.out.println("?---------");
this.velY = 0;
}
}
Und hier der Code der Player-Klasse:
Java:
@Override
public void update()
{
this.x += this.velX;
this.y += this.velY;
for(int i = 0; i < this.handler.objects.size(); i++)
{
GameObject tempObject = this.handler.objects.get(i);
this.fall(tempObject);
}
}
So, nun besteht das Problem darin, dass der Spieler einfach durch andere Objekte hindurch fallen kann, anstatt bei der Kollsision mit anderen Objekten zu stoppen. Hätte da vielleicht irgendjemand eine Idee?
Die x,y, velX und velY Variablen sind übrigens alles doubles.
Ich hoffe mal, das mir hier geholfen werden kann.
JavaGamer