Problems with number sense -_-

This is a discussion on Problems with number sense -_- within the C++ Programming forums, part of the General Programming Boards category; I didn't spot this bug until I started messing around with the paddles and moving them and such. Code: #include ...

  1. #1
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964

    Problems with number sense -_-

    I didn't spot this bug until I started messing around with the paddles and moving them and such.

    Code:
    #include "vector2.h"
    class Paddle
    {
    public:
    	vector2 location;
    	vector2 d_min;
    	vector2 d_max;
    	
    	Paddle()
    	{
    		d_min.x = -1;
    		d_min.y = -5;
    		d_max.x = 1;
    		d_max.y = 5;
    	}
    
    	const vector2 & get_position() const { return location; }
    
    	void draw(vector2 & vec)
    	{
    		glPushMatrix();
    		glTranslatef(vec.x,vec.y, -100.0f);
    		glBegin(GL_QUADS);						// Start Drawing Quads
    			glVertex3f( d_min.x, d_min.y,  1.0f);	// Bottom Left Of The Texture and Quad
    			glVertex3f( d_max.x, d_min.y,  1.0f);	// Bottom Right Of The Texture and Quad
    			glVertex3f( d_max.x, d_max.y,  1.0f);	// Top Right Of The Texture and Quad
    			glVertex3f(	d_min.x, d_max.y,  1.0f);	// Top Left Of The Texture and Quad
    		glEnd();
    		glPopMatrix();
    	}
    };
    Currently I get the true 2 dimensional location of one of these by doing this:

    Code:
    	void check_ball_collision()
    	{
    		if(ball.velocity == moveRight){
    			a_min = ball.location += ball.d_min;
    			a_max = ball.location += ball.d_max;
    			b_min = paddle1.location += paddle1.d_min;
    			b_max = paddle1.location += paddle1.d_max ;
    			if (collision2d(a_min, a_max, b_min, b_max))
    				{reverse_velocity(ball);}}		
    		else if(ball.velocity == moveLeft){
    			a_min = ball.location += ball.d_min;
    			a_max = ball.location += ball.d_max;
    			b_min = paddle2.location += paddle2.d_min;
    			b_max = paddle2.location += paddle2.d_max;
    			if (collision2d(a_min, a_max, b_min, b_max))
    				{reverse_velocity(ball);}}
    	}
    Unfortunately this simply will not do, after a quick debug step through I realized that.... Well these run time environment variables aught to shed some light:

    Code:
    		vector2::operator+= returned	{x=70.000000 y=0.00000000 }	vector2 &
    +		a_max	{x=0.00000000 y=0.00000000 }	vector2
    +		a_min	{x=-1.0000000 y=-1.0000000 }	vector2
    +		b_max	{x=70.000000 y=0.00000000 }	vector2
    +		b_min	{x=69.000000 y=-5.0000000 }	vector2
    +		paddle1	{location={...} d_min={...} d_max={...} }	Paddle
    +		paddle1.d_max	{x=1.0000000 y=5.0000000 }	vector2
    +		paddle1.location	{x=70.000000 y=0.00000000 }	vector2
    +		this	0x004bde64 class Mechanics game {a_min={...} a_max={...} b_min={...} ...}	Mechanics * const
    As you can see a_max is only reaching to the middle of the paddle, this simply won't do! This is the state of things right before we perform our collision test! The top half of the paddle is a GHOST! Oh for goodness sake, the ball too!, both of the paddles do this!

    A little more source code to shed some light on the issue.
    Code:
    #include "vector2.h"
    class Paddle
    {
    public:
    	vector2 location;
    	vector2 d_min;
    	vector2 d_max;
    	
    	Paddle()
    	{
    		d_min.x = -1;
    		d_min.y = -5;
    		d_max.x = 1;
    		d_max.y = 5;
    	}
    
    	const vector2 & get_position() const { return location; }
    
    	void draw(vector2 & vec)
    	{
    		glPushMatrix();
    		glTranslatef(vec.x,vec.y, -100.0f);
    		glBegin(GL_QUADS);						// Start Drawing Quads
    			glVertex3f( d_min.x, d_min.y,  1.0f);	// Bottom Left Of The Texture and Quad
    			glVertex3f( d_max.x, d_min.y,  1.0f);	// Bottom Right Of The Texture and Quad
    			glVertex3f( d_max.x, d_max.y,  1.0f);	// Top Right Of The Texture and Quad
    			glVertex3f(	d_min.x, d_max.y,  1.0f);	// Top Left Of The Texture and Quad
    		glEnd();
    		glPopMatrix();
    	}
    };
    As you can see the paddle is CLEARLY drawn out to be 2x10 in dimensions, but my collision code does not agree with this, OFF WITH THEIR HEADS! >:
    I don't think it would be wise to change anything in the actual drawing code accept maybe decoupling my dimension vectors from the actual glVertex3f calls. Maybe I need a function that will calculate the true size of the rectangle relative to it's position to give to the collision function.

    Any thoughts?

    Side note: I posted here because I didn't feel like this was really too specific to opengl or a graphics language or anything like that, it's in there but is well commented.
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Does this vector2 have a default constructor that sets x and y to known values? Otherwise paddle location might be uninitialized.

    The bolded logic looks a bit strange too.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    Let me give you the whole class, should help with you understanding my methods, not that they are very good or anything like that, but here it is:

    Code:
    #include "Paddle.h"
    #include "Ball.h"
    
    #include "vector2.h"
    
    Paddle paddle1;
    Paddle paddle2;
    Ball ball;
    
    class Mechanics
    {
    public:
    
    
    
    	Mechanics(){
    		paddle1.location = Player_1_Start;
    		paddle2.location = Player_2_Start;
    		resetAABB();}
    
    	void move_paddle(vector2 direction, Paddle & gamePaddle){gamePaddle.location += direction;}
    
    	void check_ball_collision()
    	{
    		if(ball.velocity == moveRight){
    			a_min = ball.location += ball.d_min;
    			a_max = ball.location += ball.d_max;
    			b_min = paddle1.location += paddle1.d_min;
    			b_max = paddle1.location += paddle1.d_max ;
    			if (collision2d(a_min, a_max, b_min, b_max))
    				{reverse_velocity(ball);}}		
    		else if(ball.velocity == moveLeft){
    			a_min = ball.location += ball.d_min;
    			a_max = ball.location += ball.d_max;
    			b_min = paddle2.location += paddle2.d_min;
    			b_max = paddle2.location += paddle2.d_max;
    			if (collision2d(a_min, a_max, b_min, b_max))
    				{reverse_velocity(ball);}}
    	}
    
    
    private:	
    	bool collision2d(vector2 & a_min, vector2 & a_max, vector2 & b_min, vector2 & b_max) {
    	return (a_min.x <= b_max.x) && (a_min.y <= b_max.y) && (a_max.x >= b_min.x) && (a_max.y >= b_min.y);}
    
    	void reverse_velocity(Ball & ball)
    	{
    		ball.velocity *= -1;
    	}
    	void resetAABB(){a_min *= 0; a_max *= 0; b_min *= 0; b_max *= 0;}
    	vector2 a_min, a_max, b_min, b_max;
    
    };
    
    Mechanics game;
    I have local vector2's for object a's minimum and maximum dimensions as well as b's. I do this because I don't want to change the value of ball.d_min or ball.d_max, as these vector's are being used to actually draw the rectangles. at the end of that block of code b_max should be b_max(70,5)

    I guess because I'm simply adding vector's together while the vector is already at -5, so it only increases by 5, the specified d_max
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 02-19-2009, 06:19 PM
  2. xor linked list
    By adramalech in forum C Programming
    Replies: 23
    Last Post: 10-14-2008, 10:13 AM
  3. Random number + guessing game trouble
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-08-2007, 03:33 AM
  4. GPA Program Problems
    By Clint in forum C Programming
    Replies: 3
    Last Post: 04-28-2005, 10:45 AM
  5. Replies: 4
    Last Post: 03-09-2002, 12:22 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21