Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am trying to create a platform game and doing wrong collision detection with the blocks.

Here's my code

// Variables
GTimer jump = new GTimer(1000);
boolean onground = true;

// The update method
public void update(long elapsedTime){
    // Add the gravity
    if (!onground && !{
    // Jumping
    if (isPressed(VK_SPACE) && onground){
        onground = false;
    if (jump.action(elapsedTime)){
        // jump expired
    // Horizontal movement
    if (isPressed(VK_LEFT)){
    if (isPressed(VK_RIGHT)){

// The collision method
public void collision(GObject other){
    if (other instanceof Block){
        // Determine the horizontal distance between centers
        float h_dist = Math.abs((other.getX() + other.getWidth()/2) - (getX() + getWidth()/2));
        // Now the vertical distance
        float v_dist = Math.abs((other.getY() + other.getHeight()/2) - (getY() + getHeight()/2));
        // If h_dist > v_dist horizontal collision else vertical collision
        if (h_dist > v_dist){
            // Are we moving right?
            if (getX()<other.getX()){
            // Are we moving left?
            else if (getX()>other.getX()){
        } else {
            // Are we moving up?
            if ({
            // We are moving down
            else {
                onground = true;

The problem is that the object jumps well but does not fall when moved out of platform. Here's an image describing the problem.


I know I'm not checking underneath the object but I don't know how. The map is a list of objects and should I have to iterate over all the objects???


share|improve this question
Possible duplicate of… – Markus von Broady Oct 27 '12 at 12:42
I'm not using tilemaps. – Sri Harsha Chilakapati Oct 27 '12 at 12:52
The engine uses QuadTrees – Sri Harsha Chilakapati Oct 27 '12 at 13:08
The QuadTree implementation is private and the collision method is called automatically. The only thing which is accessible from the map is the list of objects – Sri Harsha Chilakapati Oct 27 '12 at 13:17
What's wrong with arthur's answer from your question, it actually answers this question too, which makes this a duplicate. Take the time to implement arthur's answer and ask new questions when you are stuck with it. Repeating the same question over and over will hardly bring you anywhere. voting to close – Maik Semder Oct 27 '12 at 13:41

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.