alien jumping

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

Hi!

You could use forces instead of sine function to make him go up.

Hi,

Unfortunately it isn’t box2d :wink: object…

It doesn’t need to be.
i don’t use box2d myself, what I do for jumping is every time an entity is supposed to jump I set jump force to 2 for example, and
every frame make it lose like .05f or something. The entity will go up until the force gets 0 or < 0. Let’s call it forces.

Ahhh, sorry, misunderstood.

The sin function is nice as it does the jump nice and smooth. My issue is I need to detect when alien has jumped then check for collision on way down, if there is one, stop the jump process. The alien won’t jump up if blocks above.

Pseudo:

switch(state)
{
case Jumping:
Get alien x,y
Check if clear above (x,y)
If so, perform jump
Check if collision on way down (x,y)
If so, end move set new state for alien(IDLE,WONDERING etc)
}

Well, I don’t if I have a better way, but take a look at this:



void test_jump() {

	if(press_space_key) {
		if(!jumping) {
			jumping = true;
			jump_force = 2;
		}
	}
	
	
	if(jumping) {
		
		this.y += jump_force;
		jump_force -= .05;
		
		if(jump_force > 0) {
			check_collisions_up();
		}
		else {
			check_collisions_down();
		}
	}


}


I’ve not tested that function because I always code things differently, but I think it works if you change the values.

Yeah, that looks fine :slight_smile:

Full version, works a treat for making slimes fall down dug out holes ;D


private void jump() {
			
		if(!jump)
		{
			jump = true;
			jumpForce = 2.5f;
		}
		
		if(jump)
		{
			this.y += jumpForce;
			jumpForce -= .05;
			if(jumpForce <0)  // must be coming down
			{
				Vector3 pos = new Vector3(x,y-SPRITEHEIGHT/2,0);				
				jump = this.worldMap.checkCollision(pos);  // collision below
			}
			else // jumping up, could check for collision here
			{
			}
		}
		render(this.innerBatch);	
	}
	

Of course, would need collision checks too.

Nice!
i would like to those slimes soon! :wink: