| bullet.collisionRect.X = (int)bullet.position.X; bullet.collisionRect.Y = (int)bullet.position.Y; |
Is there any way round the unboxing/cast? The collision rects are XNA Rectangles, position X, Y are floats (stored in a Vector2).
| bullet.collisionRect.X = (int)bullet.position.X; bullet.collisionRect.Y = (int)bullet.position.Y; |
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
SenthilkumarPosted Apr 9, 2012, 11:26 PM
Here what is the problem in type casting;?
Here you need the conversion. Float and int are the value types, but you want to assign the value as expected.
But when you do the type conversion, there is a chance of data lost.
Assume that, you are trying to convert 4 byte data into 2 byte, definitely there will be some short of data lost.
VulpesPosted Apr 9, 2012, 3:23 PM
I don't know whether XNA is built on similar lines but, if it isn't, it's no big deal - you're only talking about value type conversions for which performance implications should be minimal.
time and spacePosted Apr 9, 2012, 3:10 PM
VulpesPosted Apr 9, 2012, 2:33 PM
There's no question of boxing here as both int and float are value types. However, the former is a 4 byte integer and the latter is a 4 byte floating point value and, since assigning the latter to the former may not work at runtime (and will probably lose info), a cast is needed.