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

2019/4/24

Having better collision detection

Notted Notted

2019/4/24

#
The primary mode of hit detection for my Space Invaders Remake is the isTouching() method built into Greenfoot. This is adequate for the basic aliens for now; their sprites point downward. The problem is everything else. isTouching() does not care whether or not what it touches is an active pixel (a pixel with color instead of transparency). What ends up happing is bad collision detection. We need to find some way to get those active pixels to able to get hit. How are we going to do that?
danpost danpost

2019/4/24

#
What exactly are you dealing with -- what actor has transparency and what kind of image does it have? (describe its shape)
Notted Notted

2019/4/24

#
We will take the wall as an example. Called spaceWall in code, this is a rectangle at its most basic. It has a rectangle and two smaller rectangles at its ends. This is its image: All images have transparency in my game.
danpost danpost

2019/4/24

#
Notted wrote...
We will take the wall as an example. Called spaceWall in code, this is a rectangle at its most basic. It has a rectangle and two smaller rectangles at its ends. << Image Link Omitted >> All images have transparency in my game.
It would be pretty easy to just use 3 actors, instead of 1, for the wall. You could make a basic wall class that could create walls of any given (rectangular) size:
import greenfoot.*;

public class Wall extends Actor
{
    public Wall() { this(80, 20); } // major rectangle size (adjust as needed)
    
    public Wall(int w, int h)
    {
        GreenfootImage img = new GreenfootImage(w, h);
        img.setColor(Color.GREEN.brighter());
        img.fill();
        setImage(img);
    }
}
Notted Notted

2019/4/25

#
Generally, I want to keep the art style for my game in order to make it stand out. While this is functional, we may want to look for some way to work with the original art design that I made.
Notted Notted

2019/4/25

#
Code for detection:
private void wallTakesAHit()
    {
        MyWorld spaceInvWorld = (MyWorld) getWorld();
        boolean pbHitWall = isTouching(worldBullet.class);
        boolean abHitWall = isTouching(xenoBullet.class);
        
        if (this != null && pbHitWall)
        {
            wallsHitPoints = wallsHitPoints - 1;  
            removeTouching(worldBullet.class);
        }
        if (this != null && abHitWall)
        {
            wallsHitPoints = wallsHitPoints - 1; 
            removeTouching(xenoBullet.class);
        }
    }
You need to login to post a reply.