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

2013/3/31

defining the sides of an object

welleid welleid

2013/3/31

#
So actually I want to know if there's any kind of function that could do it. I have a bloc, a square. A ball hits it. I want to know if there is any way to know which side of the bloc it hit. Cause i have some problems for the rotation of the ball. If the ball hits an horizontal side, it's not the same as a vertical side. I thought about some getX() and getY() but i'm not sure it can work as I expect. thanks a lot!
danpost danpost

2013/3/31

#
How accurately you want to detect the sides could drastically change the way you code it. Something real crude would be to determine if ball.getX() was between bloc.getX()-bloc.getImage().getWidth()/2 and bloc.getX()+bloc.getImage().getWidth()/2. If so, then the ball is bouncing on a horizontal surface. If not, then the ball is bouncing on a vertical surface. Of course, the bouncing on the corners will make the ball bounce as if it was a vertical surface, and will not really look right. Checking on the vertical side (instead of the horizontal side) will probably give a slightly better effect (it depends on the scenario itself). But, as I said, it was a very basic way. For a very 'complete' way of coding a ball bounce on a block, check out the Ball class code of my Pong Pinball scenario.
welleid welleid

2013/3/31

#
How would you write for example your "bloc.getX() ? Knowing that I have a Bloc.class and I'm writing the code in my Ball.class. Thanks a lot ! P.S I have another question as the newbie that I am, a completely different question. I have some cool missiles that I want to explode the blocs. Thus I wrote this method in the Missile.class :
1
2
3
4
5
6
7
8
9
public void missileDestroyBloc()
{
    Bloc bloc = (Bloc)getOneIntersectingObject(Bloc.class);
     
    if(bloc != null)
    {
        getWorld().removeObject(bloc);
    }
}
But I want the missile to be removed when it destroys the bloc. How can I write it in this function ? (The missile is not declared in the World.class, but in another actor class). Thanks a lot mate !
danpost danpost

2013/3/31

#
As far as your other question, if you wrote the method in the Missile.class, then:
1
2
3
4
5
6
7
8
9
public void missileDestroyBloc()
{
    Bloc bloc = (Bloc)getOneIntersectingObject(Block.class);
    if (bloc != null)
    {
        getWorld().removeObject(bloc);
        getWorld().removeObject(this); // this line remove the missile
    }
}
For your first question, after line 4 (within the 'if' block) in the above code, you can use 'bloc.getX()' to get the x location of the bloc object.
welleid welleid

2013/4/1

#
Thanks for the answer ! but I still have problems with my ball.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int isBonus = 0;
Bloc bloc = (Bloc)getOneIntersectingObject(Bloc.class); 
 
if(bloc != null)
{
    Greenfoot.playSound("plop.wav");
     
    if(getX() > bloc.getX() || getX() < bloc.getX() ) //balle tape côté
    {
        setRotation(180 - getRotation());
         
        getWorld().removeObject(bloc); 
    }
     
    else if(getY() > bloc.getY() || getY() < bloc.getY() )
    {
        setRotation(360 - getRotation());
         
        getWorld().removeObject(bloc); 
    }
This is my function of removing blocs. The first part works weel, if the ball hits the left or right side of the bloc, everything is perfect. But if it comes to the top or bottom side, it goes in a wrong way. In fact, I tried to put the second part in commentary and it did the same, as if it wasnt even read. The dimensions of my images are : 99 x 50 for the bloc, (width x height) and 30 x 30 for the ball. I think the error may come from the first part of the function, where it only comes to know if the ball hits a bloc. I use the getOneIntersectingObject. Thanks a lot !
danpost danpost

2013/4/1

#
You left the condition open in line 8. Your condition (as it is right now) states that if the bloc is anywhere to the left of the ball or if the bloc is anywhere to the right of the ball then such-n-such. The only possibility that will allow execution to proceed to the 'else' part of the expression is if the ball and the block are exactly above or below one another.
1
2
3
4
5
// change line 8 to
if (getX() >= bloc.getX()-bloc.getImage().getWidth()/2 && getX() <= bloc.getX()+bloc.getImage().getWidth()/2)
// and change line 15 to
else if (getY() >= bloc.getY()-bloc.getImage().getHeight()/2 && getY() <= bloc.getY()+bloc.getImage().getHeight()/2)
// you will need another else for corner hits
You need to login to post a reply.