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.

AppGameKit Classic Chat / Moving sprite according to rotation

Author
Message
x3meblue
15
Years of Service
User Offline
Joined: 11th Dec 2008
Location: Nuremberg, Germany
Posted: 17th May 2015 08:30
Hello!

I make a tank game and want the tank to move according to its current rotation. I got it working with this code:



The question is, why i need to subtract 90 from the rotation?

Normally the formula is:
x += speed * Cos(angle)
y += speed * Sin(angle)

Is there a bug in AGKs Sin and/or Cos functions?

AGK2 Tier 1 / GameGuru / DB PRO / ProMotion 6.5
baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 17th May 2015 11:48
At a guess because of the angle of your image. Rotate the image so the tank points right maybe?

Using AppGameKit V2 Tier 1
MikeHart
AGK Bronze Backer
21
Years of Service
User Offline
Joined: 9th Jun 2003
Location:
Posted: 17th May 2015 13:11
It is no bug, its just a normal behaviour. You will find that feature in a lot of development tools.

-----------------------------------------------------------
Using AGK2 Tier 1
Maindric
15
Years of Service
User Offline
Joined: 22nd Jul 2009
Location:
Posted: 17th May 2015 15:16
This is because a circle starts at the far right (0 degrees or 0 * pi), and from the sound of it, your tank sprite is normally facing up (90 degrees or .5 * pi). Subtracting 90 from the existing sprite sets it to the correct 0 degrees.

http://upload.wikimedia.org/wikipedia/commons/3/3b/Circle_cos_sin.gif

This link kind of shows what I am talking about.
x3meblue
15
Years of Service
User Offline
Joined: 11th Dec 2008
Location: Nuremberg, Germany
Posted: 17th May 2015 16:35
Thanks for the answers. Yes, my tank is facing up and it's initial rotation is set to 0°. Ok, then i was thinking wrong, because i thought the 0 degrees are pointing up and not to the right. Thanks again.

AGK2 Tier 1 / GameGuru / DB PRO / ProMotion 6.5
Coilwinder
11
Years of Service
User Offline
Joined: 28th Feb 2013
Location: Norway
Posted: 28th May 2015 02:02 Edited at: 28th May 2015 02:07
If you go the other way (getting a direction from X and Y speed components for example), one exception is the function AtanFull().

Then 0 degrees is pointing up, and not to the right. The right is then 90 degrees. So if you use that function, you'd have to subtract 90 (degrees) to get it compatible with the other geometric functions (Sin, Cos etc.). (Why this is so I don't know, it's either a bug or a feature I guess )

You could also use ATan(y/x), which would be "correct" (positive X direction / right = 0 degrees), but then you'd have to add a couple of checks; to avoid divide-by-zero and correct the direction according to the sign of the X component.

Apologies for any typos and strange grammar.
CodeTrasher
12
Years of Service
User Offline
Joined: 18th Jul 2012
Location: Tampere, Finland
Posted: 28th May 2015 12:54
As far as I know, the default direction in AppGameKit for 0 degree angle is {x = 0, y = -1}, which basically points up in 2D AppGameKit space (or any 2D game space, really). So, in order to get your tank sprite to face upwards in 0 degree, you just need to create/draw your sprite image to face upwards. Then you shouldn't have to subtract 90 degrees from the angle but to use your sprite angle directly.

Mah machine: Intel Xeon E3-1231v3 4x3.40GHz | CM Hyper 212 Evo | ASRock H97 Pro4 | 240GB SanDisk SSD | Sapphire Radeon R9 270X Dual-X 2048MB | Seagate HDD 1000GB |
8GB Crucial Ballistix Sport DDR3-1600

Login to post a reply

Server time is: 2024-11-25 21:28:01
Your offset time is: 2024-11-25 21:28:01