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

2013/7/3

Solid Objects

Kartoffelbrot Kartoffelbrot

2013/7/3

#
How do you make objects solid, so that you can't move into/onto them. I made it in Tiny Tank like this:
public boolean objectInFront(Class clss)
    {
        Actor a = getOneIntersectingObject(clss);
        if(a != null)
        {
            if((getX()<a.getX()) && (getY() < a.getY()))
            {
                if((getRotation()>=0 && getRotation()<=180)||getRotation()>=270)
                    return true;
            }
            if((getX()>a.getX()) && (getY() < a.getY()))
            {
                if((getRotation()>=0 && getRotation()<=270))
                    return true;
            }
            if((getX()<=a.getX()) && (getY() >= a.getY()))
            {
                if((getRotation()<=90 && getRotation()>=0)||(getRotation()>=180))
                    return true;
            }
            if((getX()>a.getX()) && (getY() > a.getY()))
            {
                if(getRotation()>=90)
                    return true;
            }

            if((getX()<a.getX()) && (getY() == a.getY()))
                if(((getRotation()>=0)&&getRotation()<=90) || (getRotation()>=270))
                    return true;
            if((getX()>a.getX()) && (getY() == a.getY()))
                if(getRotation()>=90 && getRotation() <= 270)
                    return true;
            if((getX()==a.getX()) && (getY() < a.getY()))
                if(getRotation()>=0 && getRotation()<=180)
                    return true;
            if((getX()==a.getX()) && (getY() > a.getY()))
                if(getRotation()>=180 && getRotation() <= 359)
                    return true;
        }
        return false;
    }
It works, but only objects in front. My method to check wether there is an object backwards is the same, only the < are > at the checking for rotation. And all who played Tiny Tank already might have recognized, that sometimes the enemies (and the player, too!) can drive through walls backwards! I have no more idea how I can check on those collisions. And for my new game I need a method that works to all sides of an objects. But how? Any ideas or help?
davmac davmac

2013/7/3

#
A couple of thoughts: 1 - you use getOneIntersectingObject(...), so you only will see one intersecting object even if there is more than one. You might need to use getIntersectingObjects(...) and process each object that it returns. 2 - your method of checking whether an object is "in front" seems more complicated than it needs to be. You could just calculate the angle to the object, subtract your current rotation, and then check whether the result is in a certain range (for instance).
danpost danpost

2013/7/3

#
To avoid driving through walls or passing through objects, just do the following for the movement in the act or a method it calls: (in pseudo-code)
// determine values for xSpeed and ySpeed (if needed)
// move the actor
setLocation(getX()+xSpeed, getY()+ySpeed);
// if intersection occurs, move the actor back
if (!getOneIntersectingObject(null).isEmpty())
{
    setLocation(getX()-xSpeed, getY()-ySpeed);
}
You could use a 'while' instead of an 'if' and move back in fractional amounts of xSpeed and ySpeed (if your actor is moving in large distances per act and you want to be consistent in where the actor stops when intersection occurs or if you care to be more accurate as to where the actor stops).
davmac davmac

2013/7/3

#
danpost, good suggestion, but I think: if (!getOneIntersectingObject(null).isEmpty()) should be: if (getOneIntersectingObject(null) != null)
Kartoffelbrot Kartoffelbrot

2013/7/3

#
Ok thanks! I'll try that.
Kartoffelbrot Kartoffelbrot

2013/7/3

#
Now my method looks like this:
    public Actor solidObjectInFront()
    {
        List<Car>cars = getIntersectingObjects(Car.class);
        for(Car a : cars){
            double deg = degreesAway(a.getX(), a.getY());
            if( -30<deg && deg<30 )
                return a;
        }
        return null;
    }
I had written the degreesAway method before, so I was able to use it now. It looks like this:
    public double degreesAway(int x, int y)
    {
        help a = new help();
        addObject(a,getX(),getY());
        a.turnTowards(x,y);
        int r = a.getRotation();
        removeObject(a);
        int dr = r-getRotation();
        if(dr<-180)
            dr=dr+360;
        if(dr>180)
            dr=dr-360;
        return dr;
    }
Thank you for your help.
davmac davmac

2013/7/3

#
You could also write the degreesAway(...) method as follows:
    public double degreesAway(int x, int y)  
    {  
        int r = (int) Math.atan2(y - getY(), x - getX());
        int dr = r-getRotation();  
        if(dr<-180)  
            dr=dr+360;  
        if(dr>180)  
            dr=dr-360;  
        return dr;  
    }
Kartoffelbrot Kartoffelbrot

2013/7/3

#
Ok, thanks.
You need to login to post a reply.