Hi folks

So I'm developing a small game for a homework question. I have 3 classes. Apart from the main class, my drawing class draws on screen the objects for my game, like so..

public void bat(Graphics g)
    {
        //draw the bat
        g.setColor(Color.ORANGE);
        g.fillRect(batX, batY, breadth, batLength);
        g.getClipBounds();
    }
    public void ball(Graphics g)
    {
         g.setColor(Color.yellow);
         g.fillOval(ballX, ballY ,50,50);
         g.getClipBounds();
    }

and in the class that does the actions I want to tell the objects bat and ball, if you collied I send the ball off in a different direction. I know this has to do with the intersection of the objects but not sure how to do this. Anyone any tips?

Thanks in advance.

Recommended Answers

All 2 Replies

for your ball, write a getCenter() method, which returns the exact location of the center of the ball.
for your bat, write a method that takes the result of that getCenter() method as parameter, and that returns true if that center + ( or - ) 50 would come into collision with your bat. for each move your bat or your ball makes, run this check.

for your ball, write a getCenter() method, which returns the exact location of the center of the ball.
for your bat, write a method that takes the result of that getCenter() method as parameter, and that returns true if that center + ( or - ) 50 would come into collision with your bat. for each move your bat or your ball makes, run this check.

Thanks syultuske, will give that a try and let you know how that works out. Appreciate the response.

Cheers

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.