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

2011/11/27

Can't get collision detection to work!!

keiko1dog keiko1dog

2011/11/27

#
I need to have collision detection in the paddle class as well as in the orb class. I can't get the paddles to detect the orbs and reverse their direction. This has to be done in the paddle class not in the orb class. In my paddle class I have: public void handlePaddleCollision() { Actor orb = getOneIntersectingObject(Orb.class) if (orb != null) { reverseY } } I don't think the reverseY is right, but I don't know what to put to make the orb reverse its direction. Right now it just passes through the paddles. Any help??
kiarocks kiarocks

2011/11/27

#
what is the rest of the code?
Morran Morran

2011/11/27

#
Try:
public void handlePaddleCollision() { 
   Orb orb = getOneIntersectingObject(Orb.class);
   if(orb != null) { 
       reverseY();
       orb.reverseY();
   }
}
And then fill out Orb's reverseY() method with whatever it needs to be. (Maybe it would look like this:
public void reverseY()
{
    xSpeed = -xSpeed;
    ySpeed = -ySpeed;
}
davmac davmac

2011/11/27

#
That won't compile. Try this:
public void handlePaddleCollision() { 
   Orb orb = (Orb) getOneIntersectingObject(Orb.class); // needs the cast!
   if(orb != null) { 
       // reverseY(); - probably not needed
       orb.reverseY();
   }
}
The idea is that you call the "reverseY()" method on the Orb, which tells the Orb to change its direction. The code by Morran assumes that the Orb tracks its direction via two variables, "xSpeed" and "ySpeed", but if that's not the case you will need to alter the method appropriately.
keiko1dog keiko1dog

2011/11/28

#
Hey davmac and Morran! Thanks for the input! I tried both ways (and a few others.) Now I get- cannot find symbol-method reverseY(); Here's the whole class. The paddles have to have the reverse direction in this class not the orb class(which is where I put it and got better results) nevertheless, the reverse direction has to come from the paddle class. public class Paddle { int xSpeed; int ySpeed; } public void act() { checkKeyPress(); handlePaddleCollision(); } public void checkKeyPress() { if (Greenfoot.isKeyDown("a")) { move(-20); } if (Greenfoot.isKeyDown("s")) { move(20); } } public void reverseY() { xSpeed; ySpeed; } public void handlePaddleCillision() { Orb orb = (Orb)getOneIntersectingObject(Orb.class); if (orb !null) { reverseY(); orb.reverseY(); } } not quite sure what to do now. Any thoughts?
davmac davmac

2011/11/28

#
Do you have a reverseY() method in your Orb class? (also, when you get a compiler error, please tell us on which line of your code the problem is! Thanks). Also, your code seems to have been copied wrongly. You would get plenty of other errors first if you tried to compile the exact code you posted! You have a couple of an extra '}' symbol. Make sure you copy and paste the exact code!
You need to login to post a reply.