|
Multiplication, integer problems...matrix multiplication Displaying 1-13 of 13 total.
1
Omni
|
Heya. I'm working on some ideas for a 3D engine, and I made a function that rotates Matrixes. The actual Matrixes are structs which are passed to the functions via an array reference.
Well, one of the matrixes is a translation matrix, which moves points in 3D space. 3D Units in my engine are represented in what I assume is 10x3 fixed point. IE, 1000 = 1.000, 500 = 0.500, etc.
To do this, each matrix is multiplied, then all members are divided by 1000. When I express a translation by 2000 3D units, it is actually translating the matrix by 2,000,000, which comes out to 2000 after the division by 1000.
Anyway. After translating by 2,150 units, the 3D model begins to decrement negatively. I was wondering what was causing this, so I created a "more-difficult-than-it-needs-to-be" logging function that will quickly log up to 12 megs of matrix multiplications in seconds.
I have a picture of this log and the offending line when the translation multiplication returns a negative number.
What causes this? Is it because the Verge3 integers wrap to negative when they increase too high?
[click for image]
How does 1000 * 2150000 = -2144967296?
(replaced image with link. -Grue)
Posted on 2004-07-24 00:19:45 (last edited on 2004-07-24 04:59:58)
|
Zip
|
Yes.
The biggest positive number you can store is 2^31 - 1 = 2147483647
You have there 1000 * 2150000 = 2150000000
2147483647
2150000000
See the problem? You must be able to handle this kind of bounds error in the engine.
Also, I would STRONGLY recommend using << >> 10 (or, better, 8) rather than * / 1000 - in a 3D engine it will make noticable speed differences.
Zip
[Edit: < again. And do something about that silly wide image]
Posted on 2004-07-24 00:28:07 (last edited on 2004-07-24 00:30:44)
|
mcgrue
|
I'm somewhat surprised about the number of times this question comes up. I thought the wrapping behavior of signed integers was well known amoung programmer circles.
Posted on 2004-07-24 00:41:24
|
Zip
|
...maybe because it's not mentioned in the docs anywhere....
I quickly added something about it in the variables section - by the way, aen's fixed point might want its own section somewhere obvious (rather than just being on to of the maths functions) for all those people who go what? no floats?
Zip
Posted on 2004-07-24 01:13:50
|
mcgrue
|
I am open to suggestion about where best to put it. Appendice? New section somewhere else?
Posted on 2004-07-24 01:43:56
|
vecna
|
yeah idunno. thats like. how ints work in EVERY PROGRAMMING LANGUAGE.
I guess it should still be documented, but, there's a lot of things that are implicit and not documented. This seemed like one of things there where 'if you know enough to run into this problem, you probably ought to know how ints work' sort of thing.
Posted on 2004-07-24 03:33:10
|
Omni
|
Sorry, I knew about how the variables wrapped, but I had no concept of at what value they wrapped.
I understand wrapping and signed integers, I just don't know their max values. Short, long, int...search me.
I figured it was wrapping when I posted it, but I wanted to be sure. All that x1000 multiplying will be taken out...but I kinda liked it.
EDIT: and thanks Zip for that bitshifting tip. I forgot all about that, and I may look into it. I'm not sure if I'll use any more mathematical magnification of data. If that's what you call it.
Sorry for using everyone's time.
Posted on 2004-07-24 05:57:59 (last edited on 2004-07-24 06:01:52)
|
Zip
|
Quote:Originally posted by Omni
Sorry for using everyone's time.
Don't be silly, Omni - the help board is FOR helping people. Also, you highlighted that a good portion of the verge community DOEN'T have a formal programming backround, so we need to remember to make certain things explicit in the docs that might otherwise be taken for granted.
Bitshifting is good - you won't get rounding errors, it's faster, and it's a better way of using the available space. If you've not already, aen's fixed-point numbers introduction is a good from-scratch intro, and there are more advanced tutorials to be found floating around on the interweb too.
Zip
Posted on 2004-07-24 14:34:16
|
Omni
|
Hey, hey, let me defend my integrity a bit here. I'm not stupid, I'm just not professional.
When you say "good portion of the Verge community DOESN'T have a formal programming background"...I'm going to say, "No, really?"
A lot us of get into this stuff early...I think, and I've been interested in Verge since high school. Riding on nothing but some Atari Basic 800XL experience (thank you, grandfather), which was then revamped when I figured out what the heck a "function" was. I plan on raising my kids the same way. (Here's a Nintendo, Game Boy, Atari 800XL, SNES/Genesis, Crappy Pentium 90mhz, your first RPG maker, Intro to Verge, a Real Computer to aid you in all things technology-related, and from there you're on your own, son. Don't let me down.)
Professional? Heck no. I improvised, and I'm proud :) Which, is probably going to be the rule, because I don't think I'm going to pursue computing in college. I think.
And that is why internet tutorials will always be my friend. That ought to be a section of my site...or, this site. Just a whole LOAD of tutorials. For everything. In fact, can we submit links? I'd check right now, but as I post this my wireless is down because my family's on the phone (yes...when my family picks up the cordless phone, the laptop's wireless connection dies. I find it has something to do with the vector collision between the unit vector of the phone to its receiver and the computer to the wireless hub. Mainly because she's on the other side of the house, and yet despite being practically ten yards from the wireless hub (I've even failed to get a connection at 10 inches with phone on), I'm....well. I'm rambling a lot, aren't I?
Posted on 2004-07-24 17:12:54
|
mcgrue
|
I'm quite impressed that a pure hobbyist could be coding a 3d engine. I meant no offense.
Posted on 2004-07-24 18:04:52
|
Omni
|
Well, don't be too impressed. Right now I'm trying to implement bitshifting and I'm screwing everything up :)
Posted on 2004-07-24 18:16:06
|
Zip
|
If it's confusing you, just do *256 and /256 then do a find/replace later with >>8 and <<8 later on. And well done for correcting my spelling when quoting me. One day I'll learn. :D
Zip
Posted on 2004-07-24 18:26:47
|
Omni
|
Er, that's not the problem. I'm just screwing up all the calculations in general now, and I'll probably have to end up re-implementing all the matrix stuff. It isn't worth the time trying to fix it because I've got no clue what I'm doing wrong.
EDIT: Nevermind. Turns out I was fixing the bitshifting when I removed a crucial cos() function. Cos(z_angle)>>8 suddenly became (z_angle)>>8 and stuff started dying.
Posted on 2004-07-24 18:41:59 (last edited on 2004-07-24 18:48:58)
|
Displaying 1-13 of 13 total.
1
|
|