Hey there Sign in to join this conversationNew here? Join for free

collision issues c++

Announcements Posted on
TSR Movie Madness in in to the final 16! The group winners battle it out for the overall title! 15-07-2014
    • Thread Starter
    • 0 followers
    Offline

    ReputationRep:
    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
    • 4 followers
    Offline

    ReputationRep:
    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
      }
    }
    • 0 followers
    Offline

    ReputationRep:
    &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)
    • Thread Starter
    • 0 followers
    Offline

    ReputationRep:
    (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
    • Thread Starter
    • 0 followers
    Offline

    ReputationRep:
    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);
    }
    • Thread Starter
    • 0 followers
    Offline

    ReputationRep:
    (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
    • Thread Starter
    • 0 followers
    Offline

    ReputationRep:
    (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?
    • 4 followers
    Offline

    ReputationRep:
    (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;
    • Thread Starter
    • 0 followers
    Offline

    ReputationRep:
    (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
    • 4 followers
    Offline

    ReputationRep:
    (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.
    • 0 followers
    Offline

    ReputationRep:
    (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.
    • Thread Starter
    • 0 followers
    Offline

    ReputationRep:
    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 followers
    Offline

    ReputationRep:
    (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.
    • Thread Starter
    • 0 followers
    Offline

    ReputationRep:
    (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
    • 13 followers
    Offline

    ReputationRep:
    (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.
    • Thread Starter
    • 0 followers
    Offline

    ReputationRep:
    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.
    • Thread Starter
    • 0 followers
    Offline

    ReputationRep:
    I am really struggling to put it to practice could anyone give me a example pls?
    • Thread Starter
    • 0 followers
    Offline

    ReputationRep:
    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.
    • 13 followers
    Offline

    ReputationRep:
    (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.
    • Thread Starter
    • 0 followers
    Offline

    ReputationRep:
    (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.

Reply

Submit reply

Register

Thanks for posting! You just need to create an account in order to submit the post
  1. this can't be left blank
    that username has been taken, please choose another Forgotten your password?

    this is what you'll be called on TSR

  2. this can't be left blank
    this email is already registered. Forgotten your password?

    never shared and never spammed

  3. this can't be left blank

    6 characters or longer with both numbers and letters is safer

  4. this can't be left empty
    your full birthday is required
  1. By joining you agree to our Ts and Cs, privacy policy and site rules

  2. Slide the button to the right to create your account

    Slide to join now Processing…

Updated: April 19, 2012
New on TSR

Your favourite film of the year?

For you personally what has been the best 2014 movie

Article updates
Useful resources
Reputation gems:
You get these gems as you gain rep from other members for making good contributions and giving helpful advice.