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
Sign in to Reply
  1. blade.runner's Avatar
    • Junior Member
    • Posts: 30
    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?

    Thanks
    Last edited by blade.runner; 28-03-2012 at 00:16.
  2. roblee's Avatar
    • Exalted and Worshipped Member
    • Posts: 1,061
    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
      }
    }
  3. JGR's Avatar
    • Exalted and Worshipped Member
    • Posts: 1,240
    Re: collision issues c++
    &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)
  4. blade.runner's Avatar
    • Junior Member
    • Posts: 30
    Re: collision issues c++
    (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
      }
    }
    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
  5. blade.runner's Avatar
    • Junior Member
    • Posts: 30
    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.
  6. blade.runner's Avatar
    • Junior Member
    • Posts: 30
    Re: collision issues c++
    (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)
    okay cheers mate good point I'll try that :P
  7. blade.runner's Avatar
    • Junior Member
    • Posts: 30
    Re: collision issues c++
    (Original post by blade.runner)
    okay cheers mate good point I'll try that :P

    when i try
    Code:
    BrickKiller platform1;
    	if (xB<=platform1->x)
    	{
    		xdelta=-xdelta;
    		}
    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?
  8. roblee's Avatar
    • Exalted and Worshipped Member
    • Posts: 1,061
    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
    1> did you intend to use '.' instead?
    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:
    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';
    }
    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.

    You don't write:
    Code:
    cout<< int <<endl;
    cout<< string::size() <<endl;
    cout<< BrickKiller::x <<endl;
    You write:

    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.
  9. blade.runner's Avatar
    • Junior Member
    • Posts: 30
    Re: collision issues c++
    (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:
    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';
    }
    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.

    You don't write:
    Code:
    cout<< int <<endl;
    cout<< string::size() <<endl;
    cout<< BrickKiller::x <<endl;
    You write:

    Code:
    int x=4;
    string s="hello world";
    BrickKiller killer=BrickKiller();
    
    cout<< x <<endl;
    cout<< s.size() <<endl;
    cout<< killer.x <<endl;
    I did try to use the . instead but wouldn't work
  10. roblee's Avatar
    • Exalted and Worshipped Member
    • Posts: 1,061
    Re: collision issues c++
    (Original post by blade.runner)
    I did try to use the . instead but wouldn't work
    If BrickKiller is a class, you do not write "BrickKiller.x;" you write "obj.x;" where obj is an instance of BrickKiller.

    If this isn't working for you, post the code where you are declaring BrickKiller.
  11. JGR's Avatar
    • Exalted and Worshipped Member
    • Posts: 1,240
    Re: collision issues c++
    (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?
    Ah yes, it should be . instead of -> for an object.
    I'm too used to using pointers to objects.
  12. blade.runner's Avatar
    • Junior Member
    • Posts: 30
    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);
    }
  13. Psyk's Avatar
    • TSR Royalty
    • Location: Leamington Spa
    • Posts: 19,040
    Re: collision issues c++
    (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);
    }
    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.
  14. blade.runner's Avatar
    • Junior Member
    • Posts: 30
    Re: collision issues c++
    (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.
    Thanks for that mate but idk how to do that part, that is where am really stuck
  15. Psyk's Avatar
    • TSR Royalty
    • Location: Leamington Spa
    • Posts: 19,040
    Re: collision issues c++
    (Original post by blade.runner)
    Thanks for that mate but idk how to do that part, that is where am really stuck
    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?

    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.
  16. blade.runner's Avatar
    • Junior Member
    • Posts: 30
    Re: collision issues c++
    i would see it over laps?
    If i was working it via maths i would first need to work out the area of the shape and then of the other shape.
  17. blade.runner's Avatar
    • Junior Member
    • Posts: 30
    Re: collision issues c++
    I am really struggling to put it to practice could anyone give me a example pls?
  18. blade.runner's Avatar
    • Junior Member
    • Posts: 30
    Re: collision issues c++
    This is how far ive got

    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;
    	}
    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.
  19. Psyk's Avatar
    • TSR Royalty
    • Location: Leamington Spa
    • Posts: 19,040
    Re: collision issues c++
    (Original post by blade.runner)
    This is how far ive got

    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;
    	}
    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.
    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.
  20. blade.runner's Avatar
    • Junior Member
    • Posts: 30
    Re: collision issues c++
    (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.
    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.
Sign in to Reply
Share this discussion:  
Useful resources
Article updates
Moderators

We have a brilliant team of more than 60 volunteers looking after discussions on The Student Room, helping to make it a fun, safe and useful place to hang out.

Reputation gems:
The Reputation gems seen here indicate how well reputed the user is, red gem indicate negative reputation and green indicates a good rep.
Post rating score:
These scores show if a post has been positively or negatively rated by our members.