Fart-Annoyed: Dowallcollision doesn't work properly

The Partridge Family were neither partridges nor a family. Discuss.
Post Reply
misterchi
Posts: 20
Joined: November 12th, 2016, 3:15 pm

Fart-Annoyed: Dowallcollision doesn't work properly

Post by misterchi » April 11th, 2017, 10:35 am

Hi,

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

misterchi
Attachments
FartAnnoyed.zip
(756.3 KiB) Downloaded 154 times

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

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

Post by albinopapa » April 11th, 2017, 1:59 pm

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));
}
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

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

Post by albinopapa » April 11th, 2017, 2:13 pm

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.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

misterchi
Posts: 20
Joined: November 12th, 2016, 3:15 pm

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

Post by misterchi » April 12th, 2017, 8:43 am

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

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

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

Post by albinopapa » April 12th, 2017, 2:23 pm

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.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

Post Reply