Hi,
Tried implementing some code to make one of my aliens jump and when hit something above come straight back down until hit the floor.
private void jump() {
Y = (float) (this.y - Math.sin(angle) * r);
Vector3 pos = new Vector3(x,Y+8,0);
if (jump) {// && !collisionUp) {
if(this.worldMap.checkCollision(pos)) // check above
{
angle -= 0.04f;
this.x += 1;
pos = new Vector3(x,Y-8,0);
if(!this.worldMap.checkCollision(pos)) { // collision below
angle = 0;
}
}
}
}
public boolean checkCollision(Vector3 position) {
Vector3 screenSpace = toScreenSpace(position);
int x = Math.round(screenSpace.x);
int y = Math.round(screenSpace.y);
BlankEntity entity = getBlock(x, y);
if (entity instanceof CaveTopEntity || entity instanceof CaveLeftEntity || entity instanceof CaveRightEntity )
return true;
return entity == null
|| entity instanceof CaveEntity
|| entity instanceof WaterEntity
|| entity instanceof LavaEntity;
}
/*
* getBlock get tile from map
*/
public BlankEntity getBlock(int x, int y) {
if (x <= WorldMap.w && x >= 0 && y <= WorldMap.h && y >= 0)
return worldMap[x][y];
else
return null;
}
this.x and this.y are the position of the alien in world space.
Is there a better way to accomplish what I’m trying to achieve? Think slimes in Terraria.
Thanks