gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

Beginners > Converting data

#12975 - twillinx - Tue Dec 02, 2003 10:35 pm

Ok, I'm currently doing a little test were I control a sprite with a sinuscurve, the more I press A button the higher the sinus gets. When I release a button the sprite should move along the same tangent (Don't know if thats the english word for it though. :)) I'll try to explain a little better; when I'm not touching either the A nor the B button the sprite should move along in a straight line from where the sinus ends.

But when the value is between 0 and 1 the sprite doesnt move in the Y-axis, how should I fix this? My guess is that it doesnt work all that good when the value is between 2 and 3 ... and so on either. :D

ship.y-=(int)Momentum;


This works great when I use the (int)blahblah-command during the sinuscalculations and with a 2nd degree-calculation but when its a fixed value; like momentum=0.8 then it doesn't work all of a sudden.

I hope somebody gets through all my ranting and can help me with this problem. Thanks.

#12981 - sajiimori - Tue Dec 02, 2003 11:26 pm

Well, you didn't provide any code, so we can only guess what you're actually doing. I suppose Momentum is some sort of floating point value. When you cast a float or double to an int, it just drops the fractional portion, so obviously a value of 0.8 will round down to 0.

Anyway, you shouldn't be using floating point values on GBA because the CPU doesn't support them (so they have to be emulated).

#13003 - twillinx - Wed Dec 03, 2003 10:38 am

I'm just testing it out right now, I'll probably put it in an array when I know exactly how everything will work out and with what values.

I didn't wanna embaress myself with the code but you leave me no choice;

value+=0.15;
konvert=xstart-(sin(1)*value);
sinus=(int)konvert;
ship.y=sinus;

Here I use the (int) and it looks exactly as I want it to.

If I wan't the ship to continue on in a straight line (according to the current value) when releasing the buttons, how will I solve that? The only idea I got right now is another curve, one that never tops, but there should be a simpler and better solution, I hope.

#13005 - yaustar - Wed Dec 03, 2003 12:59 pm

Try using fix point maths instead. It is highly recommended not to use float numbers as it is very slow
_________________
[Blog] [Portfolio]

#13006 - evil saltine - Wed Dec 03, 2003 1:22 pm

Quote:
If I wan't the ship to continue on in a straight line (according to the current value) when releasing the buttons, how will I solve that? The only idea I got right now is another curve, one that never tops, but there should be a simpler and better solution, I hope.


What path is the sprite making on the screen? Is it moving up and down or in a sinusoidal path?

Code:
konvert=xstart-(sin(1)*value);

I don't understand this line, it looks like you're multiplying 'value' by a constant, but if it works it's probably what you wanted.. I dunno.

#13011 - tepples - Wed Dec 03, 2003 2:55 pm

twillinx wrote:
If I wan't the ship to continue on in a straight line (according to the current value) when releasing the buttons, how will I solve that?

First read up on fixed-point arithmetic. Then take the current position, in fixed-point, and subtract the previous position, also in fixed-point. This gives you a velocity in fixed-point; add it every frame to the position of the sprite in question.
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.