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

2014/9/4

Problem with intersecting objects

K_wow K_wow

2014/9/4

#
In my game, I'm trying to get a list of all player objects that intersect a fist object and knock them back. Here's my code:
1
2
3
4
5
6
7
8
9
Object[] players = getIntersectingObjects(Player.class).toArray();
for (int plr = 0; plr < players.length; plr++)
{
    Player p = (Player)players[plr];
    if (p != null && !p.equals(player))
    {
        p.knockback(10, getRotation() + 180);
    }
}
However, intersecting players are not knocked back unless the fists are really close, closer than necessary.
Zamoht Zamoht

2014/9/4

#
getIntersectingObjects checks if the images intersect. You can either draw the fist in a bigger image with a transparent background or you can use getObjectsInRange(int radius, java.lang.Class cls).
K_wow K_wow

2014/9/4

#
The problem is that the images are intersecting but the player is not being knocked back.
Zamoht Zamoht

2014/9/4

#
Are the fists separate actors or a part of the player image?
K_wow K_wow

2014/9/4

#
They are seperate actors. Would you like the source?
Zamoht Zamoht

2014/9/4

#
I guess you are checking if the player is intersecting an other player. Change it so that you use a reference to the fists to make them check wether they intersect with another player or not. If that doesn't work the source would help me a lot to solve the problem.
davmac davmac

2014/9/4

#
Not sure about the problem you're seeing but a few general comments on your code:
1
Object[] players = getIntersectingObjects(Player.class).toArray(); 
Why convert to an array, when you can just use the list directly?
1
if (p != null && !p.equals(player)) 
p will never be null, and you should check reference equality in this case. "equals" is not necessarily well defined. Suggested code:
1
2
3
4
5
6
7
8
for (Object o : getIntersectingObjects(Player.class))
    if (o != player) 
    
        Player p = (Player) o; 
        p.knockback(10, getRotation() + 180); 
    
K_wow K_wow

2014/9/4

#
Zamoht wrote...
I guess you are checking if the player is intersecting an other player. Change it so that you use a reference to the fists to make them check wether they intersect with another player or not. If that doesn't work the source would help me a lot to solve the problem.
The code is in the fist class. Here's the source: https://dl.dropboxusercontent.com/u/62482309/Simple%20Fighter.gfar
davmac wrote...
Not sure about the problem you're seeing but a few general comments on your code:
1
Object[] players = getIntersectingObjects(Player.class).toArray(); 
Why convert to an array, when you can just use the list directly?
1
if (p != null && !p.equals(player)) 
p will never be null, and you should check reference equality in this case. "equals" is not necessarily well defined. Suggested code:
1
2
3
4
5
6
7
8
for (Object o : getIntersectingObjects(Player.class))
    if (o != player) 
    
        Player p = (Player) o; 
        p.knockback(10, getRotation() + 180); 
    
The reason I was converting to an array was that I didn't know how to use a for loop in a list. Thanks for the info! :)
davmac davmac

2014/9/4

#
Given that your code now looks like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
if (punching)
{
    for (Object o : getIntersectingObjects(Player.class)) 
    {   
        if (o != player)   
        {   
            Player p = (Player) o;   
            p.health -= 4;
            p.invincibility = 80;
            p.knockback(7, getRotation() + 180);
        }   
    }  
}
... maybe the problem is really that 'punching' is false? Have you checked this? I.e. what makes you so certain that it is the getIntersectingObjects(...) part that is failing?
danpost danpost

2014/9/4

#
I am not sure that 'getRotation()+180' is what you want to use for the 'knockback' direction. I would think that the direction that the fist is facing would be the direction of the 'knockback'. However, since I do not see the 'knockback' method in this discussion thread, I cannot tell if you reverse the direction in it.
davmac davmac

2014/9/4

#
Ah. Have looked at a bit more. The problem is that you have moved your arm back to its "bodyside" position before you check for the collision. Because you have moved it right in to it's controlliing player's body, it's no longer intersecting with the other player. After doing the collision check, you move the arm to wherever it should be. You should check for collision after the arm has reached the correct position, instead. So:
1
2
3
4
5
if (punching)
{
    // etc
}
move(punchCurrent);
should be:
1
2
3
4
5
move(punchCurrent);
if (punching)
{
    // etc
}
K_wow K_wow

2014/9/4

#
davmac wrote...
Ah. Have looked at a bit more. The problem is that you have moved your arm back to its "bodyside" position before you check for the collision. Because you have moved it right in to it's controlliing player's body, it's no longer intersecting with the other player. After doing the collision check, you move the arm to wherever it should be. You should check for collision after the arm has reached the correct position, instead. So:
1
2
3
4
5
if (punching)
{
    // etc
}
move(punchCurrent);
should be:
1
2
3
4
5
move(punchCurrent);
if (punching)
{
    // etc
}
Thankyou! I fixed up the code and now it works perfectly!
danpost wrote...
I am not sure that 'getRotation()+180' is what you want to use for the 'knockback' direction. I would think that the direction that the fist is facing would be the direction of the 'knockback'. However, since I do not see the 'knockback' method in this discussion thread, I cannot tell if you reverse the direction in it.
No, this is just the way I handle the knockback. the player moves in a negative direction when knocked back.
You need to login to post a reply.