Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Dark GDK / rate my work

Author
Message
Gatorules
15
Years of Service
User Offline
Joined: 17th Dec 2008
Location:
Posted: 22nd Jan 2009 05:44
I am kinda new to c++ and dark basic but i tried to make this function and as far as i know it works. It is supposed to move a sprite at a set speed in a certain direction


void movesprite(int id, int direction, int speed, int image)
{
int obx=dbSpriteX(id);
int oby=dbSpriteY(id);

//sin(direction)*speed=x
float x=dbSin(direction)*speed;
//squareroot((speed*speed)-(x*x))=y
float y=dbSQRT( (speed*speed)-(x*x) );
if (direction>270 && direction<=360 || direction>=0 && direction<90)
{
dbSprite(id, obx+x, oby-y, image);
}
else
{
dbSprite(id, obx+x, oby+y, image);
}
}
BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 22nd Jan 2009 09:54
I've moved this to the GDK board

veltro
User Banned
Posted: 22nd Jan 2009 12:32 Edited at: 22nd Jan 2009 12:33
If you use dbCos(direction) * speed to get the y offset you do not need to test the direction

SunDawg
19
Years of Service
User Offline
Joined: 21st Dec 2004
Location: Massachusetts
Posted: 22nd Jan 2009 21:27
I see you were trying to use the sin() function from the math header for C. The angle for that is supposed to be specified in radians (0 to 2*pi rather than 0 to 360), which would be why you were getting values you weren't expecting.


My site, for various stuff that I make.
Gatorules
15
Years of Service
User Offline
Joined: 17th Dec 2008
Location:
Posted: 22nd Jan 2009 23:52
Veltro, thank you for your comment. It seems to work, although i am not quite sure why, so i have changed my code to use it.

void movesprite(int id, int direction, int speed, int image)
{
int obx=dbSpriteX(id);
int oby=dbSpriteY(id);
float x=dbSin(direction)*speed;
float y=dbCos(direction)*speed;
dbSprite(id, obx+x, oby-y, image);
}

Login to post a reply

Server time is: 2024-09-30 15:15:28
Your offset time is: 2024-09-30 15:15:28