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

2024/12/13

moving entities spinning and getting stuck at world edge or obstacles

soap soap

2024/12/13

#
in my game i have some randomly moving entities, where they bounce off the world edge and obstacles. but sometimes, not all the time, they get stuck inside the obstacles or at the edge and spin in place before getting unstuck and moving normally again. sometimes they dont get unstuck and just spin there. how do i stop this? id appreciate an answer asap. this a project i need to get done in a few hours
public class Human extends BaseClass
{
    private int x;
    private int randomWalk = 5;
    private int speed = 2;
    
    public void act()
    {
        turnAtEdge();
    }
    
    public void turnAtEdge()
    {
        int min=0;
        int max=getRotation();
        int x = getX();
        int y = getY();

        if(atWorldEdge())
        {
            if (getX()>570)
            {
                setRotation(180-getRotation() + getRandomNumber(150,210));
                setRotation(360-getRotation() + getRandomNumber(150, 210));
                
            }
            else if (getY() > 370) 
            {
                setRotation(180-getRotation() + getRandomNumber(240,300));
                setRotation(360-getRotation() + getRandomNumber(240, 300));
                
                
            }
            else if (getX() < 30) 
            {
                setRotation(180-getRotation() + getRandomNumber(0,60) - 30);
                setRotation(360-getRotation() + getRandomNumber(0, 60) - 30);
                
            }
            else if (getY() < 30)
            {
                setRotation(180-getRotation() + getRandomNumber(60,120));
                setRotation(360-getRotation() + getRandomNumber(60, 120));
                
            } 

        } else if (isObstacleAt(x, y)) 
        {
            setRotation(180-getRotation() + getRandomNumber(240, 300));
            setRotation(360-getRotation() + getRandomNumber(240, 300));
        }
        move(4);
        
    }
    
    public void walkRandom()
    {
        int randomNumber = getRandomNumber (-randomWalk, randomWalk +1);
        setRotation(getRotation()+randomNumber);
    }
    
    public int getRandomNumber(int min, int max)
    {
     Random random = new Random();
     int n = random.nextInt(max-min) + min;
     return n; 
    }
    
    private boolean isObstacleAt(int x, int y) 
    {
       Actor obstacle = getOneObjectAtOffset(x - getX(), y - getY(), Actor.class);
       if (obstacle instanceof Barrier || obstacle instanceof HumanBarrier) 
       {
           return true;
       }
       return false;
    }

}
danpost danpost

2024/12/13

#
soap wrote...
in my game i have some randomly moving entities, where they bounce off the world edge and obstacles. but sometimes, not all the time, they get stuck inside the obstacles or at the edge and spin in place before getting unstuck and moving normally again. sometimes they dont get unstuck and just spin there. how do i stop this? << Code Omitted >>
The movement code is not in this class. Need to see BaseClass codes.
You need to login to post a reply.