collision issues c++
From C++ to PHP, debugging to webhosting; help and discussion about writing your latest program to running your website. NOT for help when your PC won't work.
| Announcements | Posted on | |
|---|---|---|
| TSR launches Learn Together! - Our new subscription to help improve your learning | 16-05-2013 | |
-
collision issues c++
I am working on a project where a ball is meant to bounce over the screen and bounce off a platform and other objects such as bricks.
However while ive been trying to do the collision am came over a seemly huge problem. When I pull into my function the coordinates of the platform
I get the following error:-
n:\project\brickwars\brickwars\b rickwars.cpp(294) : error C2446: '<=' : no conversion from 'int BrickKiller::* ' to 'int'
1> There is no context in which this conversion is possible
1>n:\project\brickwars\brickwars \brickwars.cpp(294) : error C2297: '<=' : illegal, right operand has type 'int BrickKiller::* '
It is referring to the if statement in the following code fragment
if (xB<=&BrickKiller::x)
{
xdelta=-xdelta;
}
The following code is where it gets the coordinates from regarding the platform
BrickKiller::BrickKiller()//here we load our platform so we can use it.
{
x=230;
y=370;
Platform1.loadImage("Platforms.b mp");
dx=1;
speed = 4;
}
Could anyone give me some input how to fix this issue?
ThanksLast edited by blade.runner; 28-03-2012 at 00:16. -
Re: collision issues c++
Well, you're comparing xB (which I assume is a float) with "&BrickKiller::x" (which I assume is a pointer to the float BrickKiller::x). What if you take out the ampersand?
Also, are you sure you should be trying to access BrickKiller::x? Assuming that's an instance variable, shouldn't it be more like:
Code:void fun(BrickKiller& killer) // games are fun() { if (xBound <= killer.x) // did the BrickKiller move past the bounds { deltaX*=-1.0L; // reverse the direction } } -
Re: collision issues c++If I take out the ampersand the debugger will flag another error and tell me I should have it. Yes i need to access the BrickKiller as that's where the platforms coordinates is located. I have spent hours trying to figure it out but I get no joy(Original post by roblee)
Well, you're comparing xB (which I assume is a float) with "&BrickKiller::x" (which I assume is a pointer to the float BrickKiller::x). What if you take out the ampersand?
Also, are you sure you should be trying to access BrickKiller::x? Assuming that's an instance variable, shouldn't it be more like:
Code:void fun(BrickKiller& killer) // games are fun() { if (xBound <= killer.x) // did the BrickKiller move past the bounds { deltaX*=-1.0L; // reverse the direction } }
-
Re: collision issues c++
This is my function which controls the ball, only the collision am trying to do is a total fail lol.
Code:void MovingBall:: Move( GWindow &Gwin) { Gwin.setPenColour(BLACK); Gwin.circle(xB,yB,rad); // change the fill colour to red Gwin.setPenColour(RED); // update the current position yB=yB+ydelta; xB=xB+xdelta; //draw a red circle at the current position Gwin.circle(xB,yB,rad); if ( (yB<=60) || (yB>=445 ) ) { ydelta=-ydelta; } if ( (xB<=85) || (xB>=450) ) //allow only ball to move inside set amount of the screen. { xdelta=-xdelta;//de-increment balls coorderates to make it move up the screen eg makes it bounce. } if (xB<=&BrickKiller::x) { xdelta=-xdelta; /} if (yB>=500) { Gwin.destroy();//distroy ball if goes past platform. } Gwin.circle(xB,yB,rad); }Last edited by blade.runner; 28-03-2012 at 01:48. -
Re: collision issues c++okay cheers mate good point I'll try that :P(Original post by JGR)
&BrickKiller::x is not what you want.
What you probably want is to use the -> operator on the instantiation of your object.
Unless your BrickKiller::x is static?
BrickKiller obj;
if (xB<=obj->x) -
Re: collision issues c++(Original post by blade.runner)
okay cheers mate good point I'll try that :P
when i try
I get the following error?Code:BrickKiller platform1; if (xB<=platform1->x) { xdelta=-xdelta; }
1>n:\project\brickwars\brickwars \brickwars.cpp(293) : error C2819: type 'BrickKiller' does not have an overloaded member 'operator ->'
1> n:\project\brickwars\brickwars\b rickwars.cpp(66) : see declaration of 'BrickKiller'
1> did you intend to use '.' instead? -
Re: collision issues c++(Original post by blade.runner)
If I take out the ampersand the debugger will flag another error and tell me I should have it. Yes i need to access the BrickKiller as that's where the platforms coordinates is located. I have spent hours trying to figure it out but I get no joy
I can see what your issue is and I am trying to explain it to you. You do not want to write &Class::member.1> did you intend to use '.' instead?
If you do that, it guesses that you're trying to use a deep, dark and murky part of C++ called "pointer to member", which has this syntax:
But you don't want do do that. The class BrickKiller is just that, a class of objects and not an object itself, so you can't access its member variables unless you instantiate it like you would any other variable and access through the "." operator.Code:#include <iostream> struct t{ int a,b,c,d; }; int main(void){ t q={1,2,3,4}; std::cout <<q.*&t::a<<'\n' <<q.*&t::b<<'\n' <<q.*&t::c<<'\n' <<q.*&t::d<<'\n'; }
You don't write:
You write:Code:cout<< int <<endl; cout<< string::size() <<endl; cout<< BrickKiller::x <<endl;
Code:int x=4; string s="hello world"; BrickKiller killer=BrickKiller(); cout<< x <<endl; cout<< s.size() <<endl; cout<< killer.x <<endl;
Last edited by roblee; 28-03-2012 at 02:07. -
Re: collision issues c++I did try to use the . instead but wouldn't work(Original post by roblee)
I can see what your issue is and I am trying to explain it to you. You do not want to write &Class::member.
If you do that, it guesses that you're trying to use a deep, dark and murky part of C++ called "pointer to member", which has this syntax:
But you don't want do do that. The class BrickKiller is just that, a class of objects and not an object itself, so you can't access its member variables unless you instantiate it like you would any other variable and access through the "." operator.Code:#include <iostream> struct t{ int a,b,c,d; }; int main(void){ t q={1,2,3,4}; std::cout <<q.*&t::a<<'\n' <<q.*&t::b<<'\n' <<q.*&t::c<<'\n' <<q.*&t::d<<'\n'; }
You don't write:
You write:Code:cout<< int <<endl; cout<< string::size() <<endl; cout<< BrickKiller::x <<endl;
Code:int x=4; string s="hello world"; BrickKiller killer=BrickKiller(); cout<< x <<endl; cout<< s.size() <<endl; cout<< killer.x <<endl;
-
Re: collision issues c++If BrickKiller is a class, you do not write "BrickKiller.x;" you write "obj.x;" where obj is an instance of BrickKiller.(Original post by blade.runner)
I did try to use the . instead but wouldn't work
If this isn't working for you, post the code where you are declaring BrickKiller. -
Re: collision issues c++Ah yes, it should be . instead of -> for an object.(Original post by blade.runner)
when i try
I get the following error?
1>n:\project\brickwars\brickwars \brickwars.cpp(293) : error C2819: type 'BrickKiller' does not have an overloaded member 'operator ->'
1> n:\project\brickwars\brickwars\b rickwars.cpp(66) : see declaration of 'BrickKiller'
1> did you intend to use '.' instead?
I'm too used to using pointers to objects. -
Re: collision issues c++
I have got it to build however I cannot get the 2 objects to colide anyone could lend me a hand I would be most thankful.
The first code below is where am setting my variables and loading the image
and the 2nd is the function of the bouncing ball. The ball does bounce all over the screen but ignores the platform
Code:BrickKiller::BrickKiller()//here we load our platform so we can use it. { x=230; y=370; h=10;//height l=40;//lengh width = 40; Platform1.loadImage("Platforms.bmp"); dx=1; speed = 4; if ((x<=0) || (x>=400)) { } } void MovingBall:: Move( GWindow &Gwin) { Gwin.setPenColour(BLACK); Gwin.circle(xB,yB,rad); // change the fill colour to red Gwin.setPenColour(RED); // update the current position yB=yB+ydelta; xB=xB+xdelta; //draw a red circle at the current position Gwin.circle(xB,yB,rad); if ( (yB<=60) || (yB>=445 ) ) { ydelta=-ydelta; } if ( (xB<=85) || (xB>=450) ) //allow only ball to move inside set amount of the screen. { xdelta=-xdelta;//de-increment balls coorderates to make it move up the screen eg makes it bounce. } BrickKiller platforms; if (xB<=platforms.width) { xdelta=-xdelta; } if (yB<=platforms.width) { ydelta=-ydelta; } if (yB>=450) { Gwin.destroy();//distroy ball if goes past platform. } Gwin.circle(xB,yB,rad); } -
Re: collision issues c++I don't think your logic for detecting collision with the platform is correct. You're reversing the direction of the ball if the ball's position is less than the platform's width. You need to check if the ball's position is between each end of the platform.(Original post by blade.runner)
I have got it to build however I cannot get the 2 objects to colide anyone could lend me a hand I would be most thankful.
The first code below is where am setting my variables and loading the image
and the 2nd is the function of the bouncing ball. The ball does bounce all over the screen but ignores the platform
Code:BrickKiller::BrickKiller()//here we load our platform so we can use it. { x=230; y=370; h=10;//height l=40;//lengh width = 40; Platform1.loadImage("Platforms.bmp"); dx=1; speed = 4; if ((x<=0) || (x>=400)) { } } void MovingBall:: Move( GWindow &Gwin) { Gwin.setPenColour(BLACK); Gwin.circle(xB,yB,rad); // change the fill colour to red Gwin.setPenColour(RED); // update the current position yB=yB+ydelta; xB=xB+xdelta; //draw a red circle at the current position Gwin.circle(xB,yB,rad); if ( (yB<=60) || (yB>=445 ) ) { ydelta=-ydelta; } if ( (xB<=85) || (xB>=450) ) //allow only ball to move inside set amount of the screen. { xdelta=-xdelta;//de-increment balls coorderates to make it move up the screen eg makes it bounce. } BrickKiller platforms; if (xB<=platforms.width) { xdelta=-xdelta; } if (yB<=platforms.width) { ydelta=-ydelta; } if (yB>=450) { Gwin.destroy();//distroy ball if goes past platform. } Gwin.circle(xB,yB,rad); } -
Re: collision issues c++Thanks for that mate but idk how to do that part, that is where am really stuck(Original post by Psyk)
I don't think your logic for detecting collision with the platform is correct. You're reversing the direction of the ball if the ball's position is less than the platform's width. You need to check if the ball's position is between each end of the platform.
-
Re: collision issues c++Forget that you're programming for a minute. Imagine you draw a rectangle on some graph paper. How would you work out if a point is inside that rectangle?(Original post by blade.runner)
Thanks for that mate but idk how to do that part, that is where am really stuck
The bit you're stuck on now is not actually a programming problem. Once you figure out how to solve it, programming that solution be relatively easy. -
Re: collision issues c++
This is how far ive got
I find the ball does bounce of something but not in the right place eg its not bouncing of the platform but seemly something which don't exists on screen lol.Code://start poss of platform x=230; y=370; //work out dimention of the platform h=(y-10);//height l=(x+40);//lengh leftbound = (x-20); rightbound =(x+20); in my function i have BrickKiller platforms; if ((xB<=platforms.leftbound) && (yB<=platforms.leftbound)) { xdelta=-xdelta; } -
Re: collision issues c++What does the position of the platform mean? Does it mean the centre of the platform, or maybe the top corner? It depends on how you're drawing the platform. I suspect that it's drawing the platform from the top left corner. So try drawing the platform at leftbound instead of x.(Original post by blade.runner)
This is how far ive got
I find the ball does bounce of something but not in the right place eg its not bouncing of the platform but seemly something which don't exists on screen lol.Code://start poss of platform x=230; y=370; //work out dimention of the platform h=(y-10);//height l=(x+40);//lengh leftbound = (x-20); rightbound =(x+20); in my function i have BrickKiller platforms; if ((xB<=platforms.leftbound) && (yB<=platforms.leftbound)) { xdelta=-xdelta; } -
Re: collision issues c++Hey the platform is an image rather than the Gwin rectangle. The problem am having is how to work out what the left bound is to start with mate.(Original post by Psyk)
What does the position of the platform mean? Does it mean the centre of the platform, or maybe the top corner? It depends on how you're drawing the platform. I suspect that it's drawing the platform from the top left corner. So try drawing the platform at leftbound instead of x.