This site requires JavaScript, please enable it in your browser!
Greenfoot back
grizzly1
grizzly1 wrote ...

2016/6/12

need help with my collision

grizzly1 grizzly1

2016/6/12

#
Hey guys, iam programming a jump n run in a "minecraft world", everything in my world is a 16x16px block and i already generated them at the right spot in my world. The problem is that I dont know how to write the collision because in the forum there is nothing that could help me. Maybe you guys could give me some tips or a link to a tutorial. Thanks in advance grizzly
Super_Hippo Super_Hippo

2016/6/12

#
A common way to do it is this: -save current position -move -move back to the saved position if a collision was detected
danpost danpost

2016/6/12

#
Just to clarify, I added one step below:
-save current position -move -check for collision -move back to the saved position if a collision was detected
grizzly1 grizzly1

2016/6/12

#
well that makes sense but other than checking for collision I dont know, how to code either saving the current postion nor jumping back to it. Please help me with that, or show we I can find that out.
danpost danpost

2016/6/12

#
grizzly1 wrote...
well that makes sense but other than checking for collision I dont know, how to code either saving the current postion nor jumping back to it. Please help me with that, or show we I can find that out.
You should probably show your current/attempted movement/collision code so we have something to work with.
grizzly1 grizzly1

2016/6/12

#
it isn't much because I dont really know where to start.
    public void checkCollision()
    {
        Actor Block = getOneIntersectingObject(Block.class); 
        if(Block != null)
        {
            act();
        }
        else
        {
            World RainersWelt = getWorld();
            RainersWelt.removeObject(Block);
        }
    }   
Super_Hippo Super_Hippo

2016/6/12

#
From where do you call the checkCollision-method? The else-block is executed when 'Block' is null, so if there is no intersecting block. You are trying to remove the not-existing block from the world. What are you trying to do?
grizzly1 grizzly1

2016/6/12

#
iam not quite sure how this works, thats why iam asking...:S
grizzly1 grizzly1

2016/6/15

#
that is what i got so far, the problem is that the if I jump to a block from underneath it the charakter gets stuck in the block does anyone why that happens?
public class Rainer extends Objects
{
    private int speed = 7;    
    private int vSpeed = 0;
    private int acceleration = 1;
    private int jumpPower = 10; //Variable negativ*
    
    public void act() 
    {
        move(4);
        checkFall();
    }
    
    public void move(int moveRange)
    {
        int dx = 0, dy = 0;
        if (Greenfoot.isKeyDown("right")) 
        {
            dx++;
        }
        
        if (Greenfoot.isKeyDown("left"))
        {
            dx--;
        }
        
        if (Greenfoot.isKeyDown("space"))
        {
            dy = dy - 10;
            
            
            
        }
        
        for (int i = 0; i < moveRange; i++)
        {
            setLocation(getX() + dx, getY());
            if (getOneIntersectingObject(Block.class) != null) setLocation(getX() - dx, getY());
            setLocation(getX(), getY() + dy);
            if (getOneIntersectingObject(Block.class) != null) setLocation(getX(), getY() + dy); 
            
        }
    }
    
    public void fall()
    {
        setLocation (getX(), getY() + vSpeed);
        vSpeed = vSpeed + acceleration;
    }
    
    public boolean onGround()
    {
        Actor under = getOneIntersectingObject(Block.class);
        return under != null;   
    }
       
    
    public void checkFall()
    {
        if(onGround()) 
        {
            vSpeed = 0;
        }
        else
        {
            fall();
        }
    }
    
    public void jump()
    {
        vSpeed = -jumpPower;
        fall();
    }
    


    
}
You need to login to post a reply.