Page 1 of 1

Fart-Annoyed: Dowallcollision doesn't work properly

Posted: April 11th, 2017, 10:35 am
by misterchi
Hi,

the Ball doesn't bounce. I think it is because the GetRect function returns bullshit numbers. I added the program.

misterchi

Re: Fart-Annoyed: Dowallcollision doesn't work properly

Posted: April 11th, 2017, 1:59 pm
by albinopapa
Take a look at these constructors, the second and third are incorrect.

Code: Select all

RectF::RectF(float left, float right, float top, float bottom)
	:
	left(left),
	right(right),
	top(top),
	bottom(bottom)
{
}

RectF::RectF(Vec2 & topleft, Vec2 & bottomright) {
	RectF(topleft.x, bottomright.x, topleft.y, bottomright.y);
}

RectF::RectF(Vec2 & topleft, float height, float width) {
	RectF(topleft, topleft + Vec2(width, height));
}

Re: Fart-Annoyed: Dowallcollision doesn't work properly

Posted: April 11th, 2017, 2:13 pm
by albinopapa
Spoiler:

Code: Select all

RectF::RectF( Vec2 & topleft, Vec2 & bottomright ) :
	RectF( topleft.x, bottomright.x, topleft.y, bottomright.y ){
}

RectF::RectF( Vec2 & topleft, float height, float width ) :
	RectF( topleft, topleft + Vec2( width, height ) ){
}
Using your coding style it should look something like this. I point that out simply because I find that the "one true brace" style makes code HARDER to look through. Sometimes it does look nicer, but I almost missed the fact the two constructors were messed up.

I find this to be more easily readable
Spoiler:

Code: Select all

RectF::RectF( Vec2 & topleft, Vec2 & bottomright ) 
	:
	RectF( topleft.x, bottomright.x, topleft.y, bottomright.y )
{
}

RectF::RectF( Vec2 & topleft, float height, float width ) 
	:
	RectF( topleft, topleft + Vec2( width, height ) )
{
}
To each their own though. I'm not suggesting you change your style, I'm merely stating an opinion.

Re: Fart-Annoyed: Dowallcollision doesn't work properly

Posted: April 12th, 2017, 8:43 am
by misterchi
Hi albinopapa,

now I changed the constructors and it works fine. Thanks! But I don't understand why the constructors without ":" don't work. Thanks for your feedback about my codingstyle, too. It helps a lot.

misterchi

Re: Fart-Annoyed: Dowallcollision doesn't work properly

Posted: April 12th, 2017, 2:23 pm
by albinopapa
There are two parts to a constructor, the initializer list and the function body. The initializer list portion is everything between the : and the {} and is responsible for initializing class/struct members. The function body portion is treated like any other function and is used for initializing members that are more complicated to initialize like arrays for instance.

Putting RectF(5, 5, 10, 10) inside the function body would be similar to writing:
int{5};
instead of
int a = int{5}

You are making a temp RectF, but because of scoping rules, it is destroyed at the closing brace '}' because you never assigned it to anything, like

Code: Select all

 *this = RectF(5, 5, 10, 10); 
. Initialization through calling a constructor in the initializer list is built in to the standard, so it's understood that when calling a constructor in the initializer list you are telling the compiler to use a different constructor to initialize this object.

Hopefully, that explanation was helpful.