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

2011/12/31

Collision

DMCGames DMCGames

2011/12/31

#
I am having trouble finding out how to make it so that the actor can't go through the ground. I would like the ground to be like a solid block so nothing can pass through. Please and Thank you.
danpost danpost

2011/12/31

#
Please show what code you have, so answers can be more accurate. Whatever code is relavent. Give pertinent details. Thanks in advance.
DMCGames DMCGames

2011/12/31

#
Umm I really have no relevant code, I just came notice that the game I'm making needs this.
danpost danpost

2011/12/31

#
Throw something together, and see what you come up with. If, at that point, you are still having difficulties, then discuss. (you will probably need Actor method(s) relating to getting objects (intersecting, in range, or what not); view the Greenfoot API to determine which one(s) would suit your needs.
thetibster thetibster

2011/12/31

#
I would do that using the getOneIntersectingObject() method. I use it whenever i need something to bounce off another object or obstacle. But I'd need to know some more information about what you're trying to program and what the actor is supposed to be doing and what the obstacles are...etc.
mjrb4 mjrb4

2011/12/31

#
As above, getOneIntersectingObject(Ground.class) (or whatever "Ground" is called) is your friend here - you can check for that and react accordingly. You may find that you sink into the ground a bit with that simple method if you're moving more than one pixel at a time, but there are ways round it (such as using the getObjectsInRange() method to "look ahead" and react accordingly, see http://www.greenfoot.org/scenarios/215 for an example.) Just try something simple first though then you can work on making it better!
AwesomeNameGuy AwesomeNameGuy

2012/1/1

#
Well it depends on how the ground works. In my sidescroller, I have everything made up of little blocks and in that case, I found that the method to use is getOneObjectAtOffset (or however it's spelled, check the documentation). Basically I have a boolean variable for each direction. Before I do any moving, I check each direction using getOneObjectAtOffset and if I detect something blocking me, the cooresponding boolean gets set to false, else, it gets set to true. Then I have all the movement code inside of an if statement, and it will only execute if the cooresponding boolean is true. That's the basic set up anyway, I tweaked it a little bit in my program so that if something is moving really fast it will check for objects in a greater range so it doesn't sink into an object before it detects a collision etc. Took a bit of debugging to get it to work, but that's the basic idea I used.
DMCGames DMCGames

2012/1/1

#
Well my game is made of many blocks..... can you give me an example of how I can do this?
AwesomeNameGuy AwesomeNameGuy

2012/1/1

#
Well, you know.. something like this
class CantGoThroughBlocks() {
      boolean blockedOnRight;
      public void act() {
              checkForCollisions();
              if (blockedOnRight) {
                    //do nothing 
              } else {
                     moveRight();
              }
      }

     public void checkForCollisions() {
               Block b = getOneObjectAtOffset(this.getWidth()/2+1,0,Block.class);
              if (b != null) {
                     blockedOnRight = true;
               } else {
                     blockedOnRight = false;
               }
     }

     public void moveRight() { 
              // insert incredibley creative and 
             // brilliant code here
       }
}
This probably has errors in it so don't copy and paste, and it only checks in one direction, you probably want to look in every direction, and it's obviously incomplete, but that's the basic idea I was suggesting. I do something very similar in my program I'm working on and it seems to work good.
DMCGames DMCGames

2012/1/5

#
By using your method I successfully blocked the right and left. Also, I am not sure how to make it so that it won't fall through the ground.
DonaldDuck DonaldDuck

2012/1/5

#
Copy AwesomeNameGuys checkForCollisions method but modify it to look below the object rather than beside it. Change the blockedOnRight to an onGround boolean. Now, wherever you increase the gravity (y speed), say if(!onGround) before. In the future, this may be an easier way to deal with this.
public boolean hit(int xoffset, int yoffset) //called by typing if(hit(int, int)) { stop moving in current direction }
{
    Ground g = (Ground) getOneObjectAtOffset(xoffset, yoffset, Ground.class); //check for ground at the given offsets
    if(g != null) { return true; } //if ground is found, return true
    return false; //otherwise return false
}
You need to login to post a reply.