giving a function 2 types as parameters gave me wrong numbers

Previous topic - Next topic

nabz32

Hello,
I tired to catch a bug in my collision routine, where the player was moved by way to large numbers, when collision sets in.

I shortened the code as much as I could, but just couldn´t find an error leading to this.
I debugged the code and noticed, that in collision case, sometimes the coordinates of movement where way to large ( ca. 7000 units instead of the 2.5 units I tested the collision with ).
I compared the coordinates from the point they where before they where given to the function and inside the function itself and noticed, that the coordinates given where allways what I assumed, but the coordinates after where sometimes screwed up.

The type I used to pass the coordinates to the function was rather simple:

Code (glbasic) Select
type coord
x# as float
y# as float
z# as float
endtype


As I didn´t know where the distorted coordinates came from,
I changed the functions parameters from 2 coord objects to 6 normal floats instead.
( I just copied i.e. startX for start.x in the code)
After this I never experienced a distortion of those coordinates again.
( Whats really strange is that the function later calls another function and passes it 4 3d coordinate sets of the same type, the numbers passed to this function where not corrupted [ only if they where passed falsely to my collision function allready )

I never experienced this in my sample code though ( had the exact same collision function ) , maybe its caused by having to much types defined.
The other difference to my example code, the example used only 2D coordinates, whereas the real function used 3D coordinates.





Hemlos

That snippet you show here in your message, is that the exact code, using 'x# as float' ?
Is it for inline?
Did you code 'float' TYPE? eg. TYPE float:; ENDTYPE

i dont think you need to write it that way in regular GLBasic code.

What makes a float, a float:
First you designated it to be a float using #
Second, it will remain a float, as long as you do all your math using a coefficient or constant at the end of the calculation as a float style numeral.

That said, x# should be good enough.

Code (glbasic) Select

GLOBAL Vector AS coord
TYPE coord:
X#
Y#
Z#
ENDTYPE

Vector.X = 0.345656



AS will designate a name AS a type, in GLBasic.
Bing ChatGpt is pretty smart :O

nabz32


MrTAToad

Another cause could be using integers instead of floats, invalid conversions to strings and so on.

kanonet

Actually Hemlos is not 100% correct:
a% = a AS DGNat = GLBasic integer = int in c++
b# = b = b AS DGInt = GLBasic float = double in c++ on most platfroms! Only on arm platforms (like android and iphone) it uses float.
Generally its strongly recommended to use GLBasic data types, not c++ ones, unless you really need to do it for some reason and know what you are doing. And % and # is just faster. ;)
Lenovo Thinkpad T430u: Intel i5-3317U, 8GB DDR3, NVidia GeForce 620M, Micron RealSSD C400 @Win7 x64

Hemlos

Not sure what you mean, i never implied it should be done in c++. :S
Bing ChatGpt is pretty smart :O

kanonet

I was talking about this:
Quote from: Hemlos on 2014-Sep-07First you designated it to be a float using #
Its not completely correct, since # will make it a double on most platforms, only on mobiles it will be single precision float. Generally I was more trying to give him some background informations and was just using your post as a cheap excuse to post an answer.  8)
Lenovo Thinkpad T430u: Intel i5-3317U, 8GB DDR3, NVidia GeForce 620M, Micron RealSSD C400 @Win7 x64