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

2017/2/25

isTouching detecting Transparency problem

Brandawg Brandawg

2017/2/25

#
So I am making a 2D fighter game and I want to have my players run down and up hills falls into holes etc. I only have one world right now but there is a big hole in the center where you drop. The problem is my gravity uses if Touching and if it is touching then it falls if it doesn't then it stays up. But the problem is it detects transparency. So it is just floating in mid air. I have found Pixel Pefectiong but I don't know how to include that in my code. Here is my code as of now:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class Shooter here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Fighter extends Actor
{
    /**
     * Act - do whatever the Shooter wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public int damage;
    public int level;
    private boolean facingLeft;
    private int waitCycle;
    private boolean gravityOn = true;
    private GreenfootImage image1 = new GreenfootImage("step1Right.png");
    private GreenfootImage image2 = new GreenfootImage("step2Right.png");
    private GreenfootImage image3 = new GreenfootImage("step1Left.png");
    private GreenfootImage image4 = new GreenfootImage("step2Left.png");
    public void act()
    {
        checkKeys();
        gravity();
    }
    public void checkKeys()
    {
        if(Greenfoot.isKeyDown("d") && !Greenfoot.isKeyDown("a")){
            facingLeft = false;
            moveRight();
        }
        if(Greenfoot.isKeyDown("a") && !Greenfoot.isKeyDown("d")){
            facingLeft = true;
            moveLeft();
        }     
        if(Greenfoot.isKeyDown("space")){
        }          
    }
    public void moveRight()
    {
        waitCycle++;
        if (waitCycle==30) waitCycle=0;       
        move(5);
        if (waitCycle==0)    setImage(image1);
        if (waitCycle==15)    setImage(image2);
     
    }
    public void moveLeft()
    {
        waitCycle++;
        if (waitCycle==30) waitCycle=0;       
        move(-5);
        if (waitCycle==0)    setImage(image3);
        if (waitCycle==15)    setImage(image4);
     
    }   
    public void animateRight()
    {
        if(getImage().equals(image1)){
           setImage(image2);
        }
        if(getImage().equals(image2)){
           setImage(image1);
        }       
    
    public void gravity()
    {
        if(gravityOn = true){
        if (getIntersectingObjects(Tunnel.class).isEmpty())
        this.setLocation(getX(), getY() + 1);
    }
    }
    public void attack()
    {
       if(level == 0){
           damage = 5;          
        }
       if(level == 1){
           damage = 6;
        }      
       if(level == 2){
           damage = 7;
        }       
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class Tunnel here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Tunnel extends Actor
{
    /**
     * Act - do whatever the Tunnel wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        // Add your action code here.
    }   
}
Super_Hippo Super_Hippo

2017/2/26

#
Pixel perfect collision detecting can cause a lot of lag. If possible, try to use actors with smaller images. For example check the intersecting with the "hole" rather than with the rest.
Nosson1459 Nosson1459

2017/2/26

#
There is a scenario here about pixel perfect collision detection which probably checks the pixels instead of the image and there isn't really any lag (depending on what you call "lag").
danpost danpost

2017/2/26

#
If the transparent parts are complete rows or columns of transparent pixels around the outside edge of the image, they can be removed with my Image Transparency Adder/Trimmer scenario. Download, open in greenoot and run; load and save (that is it) the image file to remove excess transparencies. You can even "share" it to make an application and use it outside of the greenfoot IDE.
Nosson1459 Nosson1459

2017/2/26

#
Brandawg wrote...
...I want to have my players run down and up hills falls into holes etc. ... there is a big hole in the center where you drop. The problem is my gravity uses if Touching and if it is touching then it falls if it doesn't then it stays up. But the problem is it detects transparency. So it is just floating in mid air. I have found Pixel Pefectiong but I don't know how to include that in my code. Here is my code as of now:
To me it sounds like there is a transparent part in the middle of the image so when you run on top you're floating over this hole. ___ ____ ||||||| |||||||||
danpost danpost

2017/2/26

#
Nosson1459 wrote...
To me it sounds like there is a transparent part in the middle of the image so when you run on top you're floating over this hole. ___ ____ ||||||| |||||||||
Could be; but what was stated is totally opposite of what was coded and there were really no specifics on the image(s) used. That is why I started with 'If'.
Nosson1459 Nosson1459

2017/2/26

#
I know. That's why I'm clarifying (I didn't try to decipher the question from the code).
Brandawg Brandawg

2017/2/26

#
Nosson1459 wrote...
Brandawg wrote...
...I want to have my players run down and up hills falls into holes etc. ... there is a big hole in the center where you drop. The problem is my gravity uses if Touching and if it is touching then it falls if it doesn't then it stays up. But the problem is it detects transparency. So it is just floating in mid air. I have found Pixel Pefectiong but I don't know how to include that in my code. Here is my code as of now:
To me it sounds like there is a transparent part in the middle of the image so when you run on top you're floating over this hole. ___ ____ ||||||| |||||||||
Yeah he is floating but I don't know how to fix it without dividing the images which would be a hassle for making more maps
danpost danpost

2017/2/26

#
Brandawg wrote...
Yeah he is floating but I don't know how to fix it without dividing the images which would be a hassle for making more maps
How about just adding a transparent actor at the "hole" and if is touching it, fall. It should probably be a bit smaller than the hole itself to make allowance for the size of the actor falling through it.
Nosson1459 Nosson1459

2017/2/27

#
danpost wrote...
How about just adding a transparent actor at the "hole" and if is touching it, fall. It should probably be a bit smaller than the hole itself to make allowance for the size of the actor falling through it.
That's an idea, or you can create your own isTouching method ( @author (Busch2207 (Moritz L.)) ) which looks for pixels (line 49 makes sure NOT transparent) instead of the whole image:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/** This method is a pixel perfect collision detection.
 * @param clss the class to see if touching
 * @return if it intersects an actor of the given class */
public boolean isTouching(Class clss)
{
    List<Actor> list =
        getWorld().getObjects(clss),
    list2 = new ArrayList();
    for(Actor A : list)
        if(intersects(A)&&touch(A))
            return true;
    return false;
}
 
/** This method is a pixel perfect collision detection.
 * @param a_big the Actor to see if touching
 * @return true, if the object touches the given Actor */
public boolean touch(Actor a_big)
{
    Actor a_small;
    if(getImage().getWidth()*getImage().getHeight()>a_big.getImage().getHeight()*a_big.getImage().getWidth())
    {
        a_small=a_big;
        a_big=this;
    }
    else
        a_small=this;
 
    int i_hypot=(int)Math.hypot(a_small.getImage().getWidth(),a_small.getImage().getHeight());
 
    GreenfootImage i=new GreenfootImage(i_hypot,i_hypot);
    i.drawImage(a_small.getImage(),i_hypot/2-a_small.getImage().getWidth()/2,i_hypot/2-a_small.getImage().getHeight()/2);
    i.rotate(a_small.getRotation());
    int w=i_hypot;
 
    GreenfootImage Ai = a_big.getImage(),
    i2=new GreenfootImage(i_hypot=(int)Math.hypot(Ai.getWidth(),Ai.getHeight()),i_hypot);
    i2.drawImage(Ai,i2.getWidth()/2-Ai.getWidth()/2,i2.getHeight()/2-Ai.getHeight()/2);
    i2.rotate(a_big.getRotation());
    Ai=i2;
 
    int
    x_Offset=a_big.getX()-a_small.getX()-(Ai.getWidth()/2-w/2),
    y_Offset=a_big.getY()-a_small.getY()-(Ai.getHeight()/2-w/2);
 
    boolean b = true;
    for(int yi =Math.max(0,y_Offset); yi<w && yi<i_hypot+y_Offset && b; yi++)
        for(int xi =Math.max(0,x_Offset); xi<w && xi<i_hypot+x_Offset && b; xi++)
            if(Ai.getColorAt(xi-x_Offset,yi-y_Offset).getAlpha()>0 && i.getColorAt(xi,yi).getAlpha()>0)
                b=false;
    return !b;
}
You need to login to post a reply.