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

2011/11/7

Collision Detection and Boundries

AwesomeNameGuy AwesomeNameGuy

2011/11/7

#
So I was going to attempt a simple side scroller I call megadude, and I was wondering if anyone knew of any good examples where an actor interacts with the world in such a way that there are places they can move, and places they can't (like there are walls and a floors they can't move through for example). I was having trouble with it yesterday, but I kinda got it to work today, but my solution is really convoluted and I was wondering if anyone knew of a good way to do this.
Lildarkone Lildarkone

2011/11/7

#
Actor collided = getOneIntersectingObject(Platform.class); //make a variable(in this case collided) of type actor and make it an intersecting object of the type you are running into
if(collided == null)//if you have not run into it
{
   fall(); //fall, example setLocation(getX(),getY()-5
}
private boolean atTop()
    {
        int blue = getWorld().getColorAt(getX(),getY()).getBlue(); //The amount of blue at the characters x and y
        int green = getWorld().getColorAt(getX(),getY()).getGreen(); //The amount of green at the characters x and y
        if(blue > green) //If there is more blue than green
        {
            return true; //you are at the top
        }
        else
        {
            return false; //otherwise you arent
        }
    }
for objects like platforms and ground, there are 2 ways. One, you can sense the difference in the color of the world's background, and two, you can sense for an object. In theory in a platform game, your character is continually falling, and stops when it jumps or lands on platforms. Color Detection Method works in that you can sense the amount of color in an area in the background. An exampe would be this method, at top, which test the amount of blue to green (sky to ground). The object detection method works so that you get an intersecting object, and then stop moving.
AwesomeNameGuy AwesomeNameGuy

2011/11/8

#
Thanks for the input. I hadn't considered the second solution. I use something like your first solution, except it's kinda modified, for example what if you are falling, and you move up against a wall while you are falling? Then you would stop falling, because a collision is detected, even though there might not be anything beneath you, you would "stick" to the wall. So I am checking every direction, looking at where all the intersecting objects might be, and determining if they really are blocking my movement in that direction or not, and it looks like I am just going to go with it. Ofc it stil needs modification, because if I am moving fast, I can still move some distance into an object before the program stops me, so I have to think of a way around that, because what if I am falling really fast? So I have to still think my way around that part too.
Lildarkone Lildarkone

2011/11/8

#
collided = getOneObjectAtOffset(getX(),getY()+20,Platform.class)//if there is an object offsetting you by 20 pixels below you

if(collided != null)//do the same as before
{
      etc.
}
private static final int MAX_SPEED = 5; //declare a variable called MAX_SPEED, static final referring to as unchangeable and in all of that class, to save memory to any number you want that looks good(in this case 5)

if(speed >= MAX_SPEED)//If speed is greater or equal to MAX_SPEED
{
      speed = MAXSPEED;//set your speed to max
}

Okay, so to stop you moving too fast, just set a max speed that you can fall at, preferably something slow enough that you dont fall into the image. Then it will just keep stopping it from going beyond that point, and you might want to set speed to 0 each time you land on a platform. There is another way to check intersecting objects, and that is objects at offset. This lets you check a point around you, such as getX()+1 would be to the right of you.
kiarocks kiarocks

2011/11/8

#
A good way to get around that is to check for collision then move
AwesomeNameGuy AwesomeNameGuy

2011/11/8

#
For sure, I think that is what I am going to try to do, in one way or another. Well, time to give it a try.
Lildarkone Lildarkone

2011/11/9

#
Tell me how it goes!
AwesomeNameGuy AwesomeNameGuy

2011/11/10

#
I posted an update here if you wanted to check it out. It ended up taking a lot more code than I thought it would, I used getObjectsInRange to check for things I could collide into in the x direction, then I made sure they were really blocking my movement, then I adjusted the amount I could move in that direction, and then I did the same for the y direction, but even still there were cases I could move too far. So I had to do it all again, making sure I am not already inside a block. Anyway, I also added a couple animations, I didn't get around to adding all of them yet, but it's coming along.
Lildarkone Lildarkone

2011/11/10

#
well, overall the game looks awesome! And pretty much what you are doing is detecting below you, and then both sides, and possibly the top. That just means you change where it is getting it at offset. collided = getOneObjectAtOffset(getX(),getY()+NUMBER) is below, etc. And problems with falling through objects is normally caused by going too fast. If your margin, as in the number you adding to getIntersecting, is smaller than your speed, you are not going to detect fast enough, and get stuck.
You need to login to post a reply.