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

2014/12/9

Collision Problems

Space0fAids Space0fAids

2014/12/9

#
Hey, I'm trying to make collision detection on my platformer game. I've added the following, and it works, except for whenever I land on the ground after a fall I will fall anywhere from 0-20ish pixels deep into the platform I am landing on. Does anyone see an issue with this code? GroundOfBackVariety is my class for my platform.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.List;

/**
 * Write a description of class GroundBack here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Player extends Mover
{
    private int vSpeed = 0;
    private int jumpSpeed = -30;
    private int gravity = 1;
    private int animationTimer = 0;
    private boolean onGround;
    public Player()
    {
        this.velocity = 4;
    }
    /**
     * Act - do whatever the GroundBack wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.ii
     */
    public void act() 
    {
        checkKeys();
        animation();
        checkFalling();
        groundCollision();
    }
    private void animation()
    {
        animationTimer++;
        if(animationTimer == 10)
        {
            setImage("French Guy Walking1.png");
        }
        if(animationTimer == 20)
        {
            setImage("French Guy Walking2.png");
            animationTimer = 0;
        }
        if(getOneObjectAtOffset(0, getImage().getHeight()+10, GroundOfBackVariety.class) == null)
        {
            setImage("French Guy Falling.png");
            animationTimer = 0;
        }
    }
    private void checkKeys()
    {
        if(Greenfoot.isKeyDown("d"))
        {           
                moveRight();               
        }
        if(Greenfoot.isKeyDown("a"))
        {            
                moveLeft();   
        }
        if(Greenfoot.isKeyDown("w"))
        {            
            setLocation(getX(), getY() + jumpSpeed - gravity);   
        }
    }
    public void groundCollision()
    {
        if(getOneObjectAtOffset( 0, getImage().getWidth()/2 + 5, GroundOfBackVariety.class) != null)
        {
            onGround = true;
        }
        if(getOneObjectAtOffset( 0, getImage().getWidth()/2 + 5, GroundOfBackVariety.class) == null)
        {
            onGround = false;
        }
        if(onGround == false)
        {
            fall();
        }
        if(onGround == true)
        {
            setLocation(getX(), getY());
            vSpeed = 0;
        }
    }
    public void fall()
    {
        setLocation(getX(),getY() + vSpeed);
        vSpeed = vSpeed + gravity;
    }
    private void checkFalling()
    {
        if (onGround == false)
        {
            fall();
        }
    }
}
danpost danpost

2014/12/9

#
I presume line 81 is supposed to set the actor above or 'on' the ground. As is, line 81 says to place the actor at its current location which does not in any way adjust its location.
Space0fAids Space0fAids

2014/12/9

#
danpost wrote...
I presume line 81 is supposed to set the actor above or 'on' the ground. As is, line 81 says to place the actor at its current location which does not in any way adjust its location.
That is supposed to stop it from going down anymore. If it can detect that there is ground below it within a certain distance, stop making down anymore. But, looking at it, ti does seem a bit.. Finicky. I think I will try to make it retrieve the image of the platform, get the width height of it, halve that, and set that as the Y. Thanks a lot for the help, cheers!
danpost danpost

2014/12/9

#
It will take the Y of the platform minus half the height of its image AND half the height of the player image.
You need to login to post a reply.