[SOLVED] UVector2 and divisions :/

If you found a bug in our library or on our website, please report it in this section. In this forum you can also make concrete suggestions or feature requests.

Moderators: CEGUI MVP, CEGUI Team

BigG
Not too shy to talk
Not too shy to talk
Posts: 22
Joined: Tue Oct 13, 2009 14:25

[SOLVED] UVector2 and divisions :/

Postby BigG » Tue Oct 13, 2009 14:40

Hey folks,
first of all I'd like to say that I'm really impressed by how powerful and handy this engine is :)

Writing some starts for an item/inventory system, I encountered a possible flaw (idk what's your opinion on this one) when centering a window inside another one using the following code:

Code: Select all

m_IconDragContainer->setPosition((CDragDropIconContainer::Size - CDragDropIcon::Size) / CEGUI::UVector2(CEGUI::UDim(0,2),CEGUI::UDim(0,2)));

m_IconDragContainer is a CEGUI::Window* and CDragDropIconContainer::Size and CDragDropIcon::Size are CEGUI::UVector2 that only have fixed dimensions (as I want items to be 32x32 pixel always).
What I was trying to do is dividing the total border area size by two to get the offset to center the item icon.

Looking at

Code: Select all

UVector2 operator/(const UVector2& other) const     { return UVector2(d_x / other.d_x, d_y / other.d_y); }

and

Code: Select all

UDim operator/(const UDim& other) const     { return UDim(d_scale / other.d_scale, d_offset / other.d_offset); }

in CEGUIUDim.h, one can easily see what is going to happen at this part:

Code: Select all

d_scale / other.d_scale
- as other.d_scale is zero we get a division by zero exception :(

of course there are several workarounds to this like just setting other.d_scale to something else as this->d_scale is zero anyway:

Code: Select all

m_IconDragContainer->setPosition((CDragDropIconContainer::Size - CDragDropIcon::Size) / CEGUI::UVector2(CEGUI::UDim(1,2),CEGUI::UDim(1,2)));

or doing the division manually

Code: Select all

CEGUI::UVector2 diff = CDragDropIconContainer::Size - CDragDropIcon::Size;
m_IconDragContainer->setPosition(CEGUI::UVector2(cegui_absdim(diff.d_x.d_offset / 2),cegui_absdim(diff.d_y.d_offset /2)));

but all of them are quite nasty :D

I'd suppose either checking for zero in the division routines or adding custom division functions like CEGUI::UVector2::divAbs and CEGUI::UVector2::divRel or something similar :)

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

another feature I'd like to request in this context is overloaded operators of UVector2 and UDim like Ogre3D has it for their Vector classes:

Code: Select all

UVector2 operator+(const UDim& other) const;
UVector2 operator-(const UDim& other) const;
UVector2 operator/(const UDim& other) const;
UVector2 operator*(const UDim& other) const;

and so on, which divide both, x and y, by other. Would come in quite handy to shorten my initial code to

Code: Select all

m_IconDragContainer->setPosition((CDragDropIconContainer::Size - CDragDropIcon::Size) / cegui_absdim(2));

or something similar :)

Thanks for your time and again thanks for that great engine :)
-BigG
Last edited by BigG on Fri Oct 16, 2009 00:53, edited 1 time in total.

User avatar
CrazyEddie
CEGUI Project Lead
Posts: 6760
Joined: Wed Jan 12, 2005 12:06
Location: England
Contact:

Re: UVector2 and divisions :/

Postby CrazyEddie » Tue Oct 13, 2009 18:14

Hi,

Yeah, I agree that the division by zero is a bug. I also think the other suggestion, with regards to the overloads taking a UDim, are fair points also. Thanks :)

CE.

BigG
Not too shy to talk
Not too shy to talk
Posts: 22
Joined: Tue Oct 13, 2009 14:25

Re: UVector2 and divisions :/

Postby BigG » Tue Oct 13, 2009 19:53

Hey CE,
thanks for clearing things up :) I'm glad to hear it's going to be fixed, until then I'll just use that nasty temporary fix :o

-BigG

//Edit:
It's in the latest branch now, set to solved.


Return to “Bug Reports, Suggestions, Feature Requests”

Who is online

Users browsing this forum: No registered users and 10 guests