0

I tried to construct a GPolygon (code following) for my paddle instead of a GRect. I found that the detect collision function didn't work for this paddle, but i can't really see why. Perhaps its something about abstract inheritance in the SPL interface that i don't understand. Anyone have any ideas?

 GRect initPaddle(GWindow window)
 {

    GPolygon paddle = newGPolygon();
    setLocation(paddle, (WIDTH/2 - PADDLEW/2), HEIGHT - 40);
    addVertex(paddle, 0, 0);
    addEdge(paddle, -20, -10);
    addVertex(paddle, -20, -10);
    addEdge(paddle, -20, 0);
    addVertex(paddle, -40, -10);
    addEdge(paddle, -20, 10);
    addVertex(paddle, -60, 0);
    addEdge(paddle, 60, 0);
    add(window, paddle);
    setColor(paddle, "RED");
    setFilled(paddle, true);

    return paddle;
 }
2
  • I'm sorry! Looks like I mixed different versions of breakout code. Are you sure your paddle is displaying correctly? (Because I just tried compiling and running your code and your paddle doesn't seem to be displaying correctly).
    – kzidane
    Dec 6 '14 at 17:52
  • Yes i can see and control the paddle which looks like half of a hexagon. But i can't get it to behave like a GRect. Even after i change the initPaddle return type (both in the function definition and the prototype.
    – omcdonnell
    Dec 6 '14 at 17:57
1

After a bit of playing with code, it turns out that the problem is caused by getGObjectAt. It doesn't return a pointer to your paddle even though it's in the specified location. Rather, it returns NULL. Below is a short program that verifies that

#include <stdio.h>
#include <math.h>
#include "gwindow.h"
#include "gobjects.h"

int main(void)
{

    // create the window
    #define WIDTH 400
    #define HEIGHT 600  
    GWindow gw;
    gw = newGWindow(WIDTH, HEIGHT);

    // create the paddle
    GPolygon paddle = newGPolygon();
    setLocation(paddle, (WIDTH/2 - 50/2), HEIGHT - 40);
    addVertex(paddle, 0, 0);
    addEdge(paddle, -20, -10);
    addVertex(paddle, -20, -10);
    addEdge(paddle, -20, 0);
    addVertex(paddle, -40, -10);
    addEdge(paddle, -20, 10);
    addVertex(paddle, -60, 0);
    addEdge(paddle, 60, 0);
    add(gw, paddle);
    setColor(paddle, "RED");
    setFilled(paddle, true);

    // get the coordination of the paddle
    float paddleX = getX(paddle);
    float paddleY = getY(paddle);
    printf("x: %.0f\ny: %.0f\n", paddleY, paddleX);

    // verify the existence of the paddle using getGObjectAt
    if (getGObjectAt(gw, paddleX, paddleY))
        printf("found the paddle\n");
    else
        printf("didn't find the paddle\n");
}

Output:

enter image description here

My suggestion is to write your own version of getGObjectAt that detects collisions of the ball with the paddle using some calculations and maybe use this function as part of detectCollision's definition.

For the sake of simplicity, I'll assume my paddle is a GRect and getGObjectAt doesn't work for GRects, I'd write a function like this to detect collisions of the bottom side of the ball with the top side of my paddle (depending on your approach, you might need your paddle declared as a global variable or to pass an instance of it to the function)

// returns true if bl collides with pdl in gw
bool collidedWithPaddle(GWindow gw, GRect pdl, GOval bl)
{
    // calculate paddle coordinates
    float pdlX = getX(pdl);
    float pdlY = getY(pdl);

    // calculate ball coordinates
    float blX = getX(bl);
    float blY = getY(bl);

    /* check whether the bottom side of the ball collides
     * with the top side of the paddle */
    if (blY + RADIUS >= pdlY)
    {
        // the right side of the paddle
        float pdlR = pdlX + PDL_W;
        // the right side of the ball
        float blR = blX + RADIUS;

        return (blR >= pdlX && blR <= pdlR + RADIUS);
    }

    return false;
}
4
  • Thats certainly not how detectCollision is defined in my breakout.c starter file. The if statement i'm looking at only checks that object != NULL, and that object is assigned with the getObjectAt function.
    – omcdonnell
    Dec 6 '14 at 17:28
  • @omcdonnell I just updated my answer. Hope that helps!
    – kzidane
    Dec 6 '14 at 18:36
  • Thanks, I'll play around with that. I think after the midway point of your sample you started referring to 'pd1Y' as 'bd1y' in error, is that correct?
    – omcdonnell
    Dec 7 '14 at 0:07
  • @omcdonnell yeah sorry for the typos! Edited. Thanks for the heads-up!
    – kzidane
    Dec 7 '14 at 0:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .