|
Sin And Cos Help |
dakatt
Member #10,695
February 2009
|
Can someone with a bit more knowledge tell me where I'm going wrong. I've tried different combinations of fixed and floats but the results seem weird. The rotation is off after a full rotation. <code ="1"> void Update(); void Draw(); CShip* my_ship; CKeyboard* keyboard; al_fixed xpos,ypos,xvel,yvel; CPlayer::CPlayer(CKeyboard* k,CMouse* m){ my_ship = new CShip(); angle_rad = al_ftofix(0.0); speed_acc = al_ftofix(0.5); xpos = al_itofix(0); xvel = al_itofix(0); void CPlayer::Update(){ void CPlayer::Move(){ if(keyboard->key_flag[KEY_NAME_UP]){speed_current =1;} if(angle_deg < 0) angle_rad = (angle_deg * PI) / 180; xvel = al_fixmul(speed_current, al_fixcos(angle_rad)); xpos+=xvel; void CPlayer::Draw(){ al_draw_textf(myfont,al_map_rgb(255,255,255),10,188,NULL,"SPEED : %f",al_fixtof(speed_current)); Thanks |
Edgar Reynaldo
Major Reynaldo
May 2007
|
There is no reason to use the 'fixed' class anymore. Just use regular floats or doubles, don't mess around with converting back and forth between ftofix. As a rule, you can always regenerate your speed and heading from your rotation value. You can use transforms or just regular sin and cos and rotation matrices. Generally I keep a speed (magnitude of velocity) and angle in radians, since that's what the C library works with. Here's a treat from old days about working with angles : And here's sin and cos, a programmer's pal, except I think they give you bad advice about using fixed numbers. My Website! | EAGLE GUI Library Demos | My Deviant Art Gallery | Spiraloid Preview | A4 FontMaker | Skyline! (Missile Defense) Eagle and Allegro 5 binaries | Older Allegro 4 and 5 binaries | Allegro 5 compile guide |
Neil Roy
Member #2,229
April 2002
|
I highly recommend reading the following series of blog posts which cover things like rotations and the like using vectors which is the best way to represent locations and to do rotations etc. This is probably one of the best posts on the subject I have seen. http://blog.wolfire.com/2009/07/linear-algebra-for-game-developers-part-1/ --- |
dakatt
Member #10,695
February 2009
|
I'll check that blog out later today Neil, it looks interesting and I don't know much about vectors but it looks user friendly. You were right Edgar, I replaced all the fixeds and it works as it should. Also the code I posted wasn't right since I was playing around with some variables and pasted it after. Here's the working code for anyone interested <code ="1"> class CPlayer{ void Update(); void Draw(); CShip* my_ship; CKeyboard* keyboard; float angle_deg,angle_rad; CPlayer::CPlayer(CKeyboard* k,CMouse* m){ my_ship = new CShip(); xpos = 0; angle_deg = 0.0; } void CPlayer::Update(){ void CPlayer::Move(){ if(angle_deg >= 360)angle_deg-=360; angle_rad = (angle_deg * PI) / 180; if(keyboard->key_flag[KEY_NAME_UP]){speed = 1;} xvel = speed * cos(angle_rad); xpos += xvel; </code> Thanks guys |
Edgar Reynaldo
Major Reynaldo
May 2007
|
<code>code goes here...</code> tags are helpful It was probably because there are only 256 allegro degrees in a circle, so you have to convert as well. My Website! | EAGLE GUI Library Demos | My Deviant Art Gallery | Spiraloid Preview | A4 FontMaker | Skyline! (Missile Defense) Eagle and Allegro 5 binaries | Older Allegro 4 and 5 binaries | Allegro 5 compile guide |
LennyLen
Member #5,313
December 2004
|
Edgar Reynaldo said: code goes here... tags are helpful He used code tags, but he has '="1"' instead of 'start="1"'.
|
Edgar Reynaldo
Major Reynaldo
May 2007
|
Sorry, I'm half blind sometimes... I saw that and my brain acknowledged it, but still posted it anyway. My Website! | EAGLE GUI Library Demos | My Deviant Art Gallery | Spiraloid Preview | A4 FontMaker | Skyline! (Missile Defense) Eagle and Allegro 5 binaries | Older Allegro 4 and 5 binaries | Allegro 5 compile guide |
dakatt
Member #10,695
February 2009
|
Damn it, I tried using the formatting help. Hopefully this is better Also you can't edit a post? |
LennyLen
Member #5,313
December 2004
|
|