Collision problems. Urgent!

Hi guys posted last time, making a simple pong game as a coursework and cant seem to get the ball to hit of the paddle I will post the code because I really am stumped with it. I have collision with the ball and the walls however want to change the collision on the sides to react off the paddles instead. Urgent help and really would appreciate any help. Thanks guys.


#include "stdafx.h"
#include <iostream>
#include <time.h>
#include <vector>
#include <cmath>
#include "gwin.h"


const int NUMBEROFBALLS = 1;

class Paddle
{
public:
float xPos;
float yPos;
GImage* image;


Paddle(float Xpos, float Ypos, GImage* Image)
{
xPos = Xpos;
yPos = Ypos;
image = Image;

}

void Draw(GWindow* gwin)
{
(*gwin).drawImage((int)xPos, (int)yPos, image);
}

void Limit(GWindow* gwin)
{
if (yPos < 0)
{
yPos = 0;
}

if(yPos > ((*gwin).getHeight() - (*image).getHeight()))
{
yPos = (*gwin).getHeight() - (*image).getHeight();
}
}
};
const float PaddleSpeed = 1.0f;

int main()
{
GWindow Gwin;
//objects info [1];
GImage blueball(GPath("pictures/BLUEBALL.png").transform());
GImage PaddleImg(GPath("pictures/Paddle.png").transform());
std::vector<GSprite *> ball_list;
typedef std::vector<GSprite *>::iterator ball_list_iter;



// 3. Variables for game objects


Paddle player1 = Paddle (0,PaddleImg.getWidth(), &PaddleImg);
Paddle player2 = Paddle(Gwin.getWidth() - PaddleImg.getWidth(), 100, &PaddleImg);


// Create a new sprite for every ball
//for (int i = 0 ; i < NUMBEROFBALLS ; i++)
{
GSprite *ball = new GSprite();
ball_list.push_back(ball);




int x = rand() % 1;

if (!x)

ball->attachImage(&blueball);

//Ball Directions


ball->x = rand() % 50; //+ 250;
ball->y = rand() % 50; //+ 250;

//Ball Speeds

ball->vx = 0;
while (fabs(ball->vx) < 0.3)
ball->vx = ((rand() % 400) - 200.0) / 200.0;

ball->vy = 0;
while (fabs(ball->vy) < 0.3)
ball->vy = ((rand() % 400) - 200.0) / 200.0;


}

int ball_width = blueball.getWidth();
int ball_height = blueball.getHeight();
float paddle_width = PaddleImg.getWidth();
float paddle_height = PaddleImg.getHeight();


// Seed the random-number generator with current time so that
// the numbers will be different every time we run.
srand( (unsigned)time( NULL ) );


bool isRequired = true;
//int x = 90;

// Loop until user presses Esc
while (isRequired)
{
Gwin.clear();

if (Keyboard.isPressed('w'))
{
player1.yPos -= PaddleSpeed;
}

if (Keyboard.isPressed('s'))
{
player1.yPos += PaddleSpeed;
}

player1.Limit(&Gwin);

if(Keyboard.isPressed(GwinKeyNames::CURSOR_UP))
{
player2.yPos -= PaddleSpeed;
}
if (Keyboard.isPressed(GwinKeyNames::CURSOR_DOWN))
{
player2.yPos += PaddleSpeed;
}
player2.Limit(&Gwin);



Gwin.clear();



int z = 0;



for (ball_list_iter i = ball_list.begin() ; i != ball_list.end() ; ++i)
{
(*i)->render();
(*i)->x += (*i)->vx;
(*i)->y += (*i)->vy;


//Check collisions with wall
//if ((((*i)->x + ball_width) > Gwin.getWidth()) || ((*i)->x <= 1))
//(*i)->vx *= -1;
if((((*i)->x + paddle_width) > Gwin.getWidth() - PaddleImg.getWidth())||((*i)->x <+ 1))
(*i)->vx *= -1;



if ((((*i)->y - ball_height)> Gwin.getHeight ()) || ((*i)->y <= 0))
(*i)->vy *= -1;


player1.Draw(&Gwin);
player2.Draw(&Gwin);



++z;
}
Gwin.refresh();

if (Keyboard.isKeyReady())
{// check if they want to exit
if (Keyboard.waitKey() == GwinKeyNames::ESCAPE)
{// let them exit
isRequired = false;
}
}
}
return 0;
}
Fixed. (kind of)

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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#include "stdafx.h"
#include <iostream>
#include <time.h>
#include <vector>
#include <cmath>
#include "gwin.h"
 
 
const int NUMBEROFBALLS = 1;
 
class Paddle {
  public:
    float xPos;
    float yPos;
    GImage *image;
 
 
     Paddle(float Xpos, float Ypos, GImage * Image) {
	xPos = Xpos;
	yPos = Ypos;
	image = Image;
 
    } void Draw(GWindow * gwin) {
	(*gwin).drawImage((int) xPos, (int) yPos, image);
    }
 
    void Limit(GWindow * gwin) {
	if (yPos < 0) {
	    yPos = 0;
	}
 
	if (yPos > ((*gwin).getHeight() - (*image).getHeight())) {
	    yPos = (*gwin).getHeight() - (*image).getHeight();
	}
    }
};
 
const float PaddleSpeed = 1.0f;
 
int main()
{
    GWindow Gwin;
//objects info [1];
    GImage blueball(GPath("pictures/BLUEBALL.png").transform());
    GImage PaddleImg(GPath("pictures/Paddle.png").transform());
    std::vector < GSprite * >ball_list;
    typedef std::vector < GSprite * >::iterator ball_list_iter;
 
 
 
// 3. Variables for game objects
 
 
    Paddle player1 = Paddle(0, PaddleImg.getWidth(), &PaddleImg);
    Paddle player2 = Paddle(Gwin.getWidth() - PaddleImg.getWidth(), 100, &PaddleImg);
 
 
// Create a new sprite for every ball
//for (int i = 0 ; i < NUMBEROFBALLS ; i++)
    {
	GSprite *ball = new GSprite();
	ball_list.push_back(ball);
 
 
 
 
	int x = rand() % 1;
 
	if (!x)
 
	    ball->attachImage(&blueball);
 
//Ball Directions
 
 
	ball->x = rand() % 50;	//+ 250;
	ball->y = rand() % 50;	//+ 250;
 
//Ball Speeds
 
	ball->vx = 0;
	while (fabs(ball->vx) < 0.3)
	    ball->vx = ((rand() % 400) - 200.0) / 200.0;
 
	ball->vy = 0;
	while (fabs(ball->vy) < 0.3)
	    ball->vy = ((rand() % 400) - 200.0) / 200.0;
 
 
    }
 
    int ball_width = blueball.getWidth();
    int ball_height = blueball.getHeight();
    float paddle_width = PaddleImg.getWidth();
    float paddle_height = PaddleImg.getHeight();
 
 
// Seed the random-number generator with current time so that
// the numbers will be different every time we run.
    srand((unsigned) time(NULL));
 
 
    bool isRequired = true;
//int x = 90;
 
// Loop until user presses Esc
    while (isRequired) {
	Gwin.clear();
 
	if (Keyboard.isPressed('w')) {
	    player1.yPos -= PaddleSpeed;
	}
 
	if (Keyboard.isPressed('s')) {
	    player1.yPos += PaddleSpeed;
	}
 
	player1.Limit(&Gwin);
 
	if (Keyboard.isPressed(GwinKeyNames::CURSOR_UP)) {
	    player2.yPos -= PaddleSpeed;
	}
	if (Keyboard.isPressed(GwinKeyNames::CURSOR_DOWN)) {
	    player2.yPos += PaddleSpeed;
	}
	player2.Limit(&Gwin);
 
 
 
	Gwin.clear();
 
 
 
	int z = 0;
 
 
 
	for (ball_list_iter i = ball_list.begin(); i != ball_list.end(); ++i) {
	    (*i)->render();
	    (*i)->x += (*i)->vx;
	    (*i)->y += (*i)->vy;
 
 
//Check collisions with wall
//if ((((*i)->x + ball_width) > Gwin.getWidth()) || ((*i)->x <= 1))
//(*i)->vx *= -1;
	    if ((((*i)->x + paddle_width) > Gwin.getWidth() - PaddleImg.getWidth()) || ((*i)->x < +1))
		(*i)->vx *= -1;
 
 
 
	    if ((((*i)->y - ball_height) > Gwin.getHeight()) || ((*i)->y <= 0))
		(*i)->vy *= -1;
 
 
	    player1.Draw(&Gwin);
	    player2.Draw(&Gwin);
 
 
 
	    ++z;
	}
	Gwin.refresh();
 
	if (Keyboard.isKeyReady()) {	// check if they want to exit
	    if (Keyboard.waitKey() == GwinKeyNames::ESCAPE) {	// let them exit
		isRequired = false;
	    }
	}
    }
    return 0;
}
 


(Thanks to indentcode.net)


Now my suggestion to fix collisions:

You're overcomplicating things. Make a class Rectangle which has an x and y coordinate (probably of the upper-left corner) and a width and height. Assign a rectangle to the ball and both paddles. You can easily check if two of these rectangles are overlapping; I'll let you figure that out. If the two rectangles touch, you need to move the ball back along its path to the front edge of the paddle and modify its velocity somehow.
Last edited on
Thanks for your help will give it a go now.
I have created the class for rectangle, and that all seems to be fine, I keep getting errors when trying to assign the rectangle to the paddle and ball... any more advice? would be really be appreciated.

#include "stdafx.h"
#include <iostream>
#include <time.h>
#include <vector>
#include <cmath>
#include "gwin.h"


const int NUMBEROFBALLS = 1;

class Paddle
{
public:
float xPos;
float yPos;
GImage* image;


Paddle(float Xpos, float Ypos, GImage* Image)
{
xPos = Xpos;
yPos = Ypos;
image = Image;

}

void Draw(GWindow* gwin)
{
(*gwin).drawImage((int)xPos, (int)yPos, image);
}

void Limit(GWindow* gwin)
{
if (yPos < 0)
{
yPos = 0;
}

if(yPos > ((*gwin).getHeight() - (*image).getHeight()))
{
yPos = (*gwin).getHeight() - (*image).getHeight();
}
}
};
class Box
{
public:
float x1;
float x2;
float x3;
float x4;

Box(float X1, float X2, float X3, float X4)
{
x1 = X1;
x2 = X2;
x3 = X3;
x4 = X4;
}
void DrawRectangle(GWindow* gwin)
{
(*gwin).rectangle((int)x1,(int)x2, (int)x3, (int)x4);
}
};
const float PaddleSpeed = 1.0f;

int main()
{
GWindow Gwin;
//objects info [1];
GImage blueball(GPath("pictures/BLUEBALL.png").transform());
GImage PaddleImg(GPath("pictures/Paddle.png").transform());
std::vector<GSprite *> ball_list;
typedef std::vector<GSprite *>::iterator ball_list_iter;



// 3. Variables for game objects

Box rectangle = Box (100,100,200,200);
Paddle player1 = Paddle (0,PaddleImg.getWidth(), &PaddleImg);
Paddle player2 = Paddle(Gwin.getWidth() - PaddleImg.getWidth(), 100, &PaddleImg);




// Create a new sprite for every ball
//for (int i = 0 ; i < NUMBEROFBALLS ; i++)
{
GSprite *ball = new GSprite();
ball_list.push_back(ball);




int x = rand() % 1;

if (!x)

ball->attachImage(&blueball);

//Ball Directions


ball->x = rand() % 50; //+ 250;
ball->y = rand() % 50; //+ 250;

//Ball Speeds

ball->vx = 0;
while (fabs(ball->vx) < 0.3)
ball->vx = ((rand() % 400) - 200.0) / 200.0;

ball->vy = 0;
while (fabs(ball->vy) < 0.3)
ball->vy = ((rand() % 400) - 200.0) / 200.0;


}

int ball_width = blueball.getWidth();
int ball_height = blueball.getHeight();
float paddle_width = PaddleImg.getWidth();
float paddle_height = PaddleImg.getHeight();


// Seed the random-number generator with current time so that
// the numbers will be different every time we run.
srand( (unsigned)time( NULL ) );


bool isRequired = true;
//int x = 90;

// Loop until user presses Esc
while (isRequired)
{
Gwin.clear();

if (Keyboard.isPressed('w'))
{
player1.yPos -= PaddleSpeed;
}

if (Keyboard.isPressed('s'))
{
player1.yPos += PaddleSpeed;
}

player1.Limit(&Gwin);

if(Keyboard.isPressed(GwinKeyNames::CURSOR_UP))
{
player2.yPos -= PaddleSpeed;
}
if (Keyboard.isPressed(GwinKeyNames::CURSOR_DOWN))
{
player2.yPos += PaddleSpeed;
}
player2.Limit(&Gwin);



Gwin.clear();



int z = 0;



for (ball_list_iter i = ball_list.begin() ; i != ball_list.end() ; ++i)
{
(*i)->render();
(*i)->x += (*i)->vx;
(*i)->y += (*i)->vy;


//Check collisions with wall
//if ((((*i)->x + ball_width) > Gwin.getWidth()) || ((*i)->x <= 1))
//(*i)->vx *= -1;
if((((*i)->x + ball_width) > Gwin.getWidth() - PaddleImg.getWidth())||((*i)->x <+ 1))
(*i)->vx *= -1;



if ((((*i)->y - ball_height)> Gwin.getHeight ()) || ((*i)->y <= 0))
(*i)->vy *= -1;


player1.Draw(&Gwin);
player2.Draw(&Gwin);



++z;
}
Gwin.refresh();

if (Keyboard.isKeyReady())
{// check if they want to exit
if (Keyboard.waitKey() == GwinKeyNames::ESCAPE)
{// let them exit
isRequired = false;
}
}
}
return 0;
}
Topic archived. No new replies allowed.