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 / get joystick direction

Author
Message
Zeerun
AGK Developer
18
Years of Service
User Offline
Joined: 28th Aug 2005
Location: United Kingdom
Posted: 21st Sep 2012 12:27 Edited at: 21st Sep 2012 12:31
Hi there,
Not at my developing computer at the moment, but been mentally calculating how to solve this problem and failing : -)

The problem: move a top down spaceship sprite go in the direction of the joystick, whilst incorporating a turn rate.

I am making a 2d space game, I just need to finalise the movement control, I think the keyboard version is fine
but onscreen is being a pain

keyboard: W = forward and A & D for left right,
this mean I can introduce turn rate.

but if this behaviour is duplicated with an onscreen joystick it is awkward as you expect to point the joystick and the ship goes there, so need a way of getting turn rate in there.

So what I need is to determine where the joystick is pointing so I can control my ship accordingly.

I am guessing sin and cos ect will be involved against the joystick x and y data, just wondering if anyone has done this who can point me in the right direction : -)

Thanks everyone!

I seek perfection but wear my scars with pride
baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 21st Sep 2012 15:22
I would use the atanfull command if it's an angle you're after. Try experimenting with this:

jx# = GetVirtualJoystickX( index )
jy# = GetVirtualJoystickY( index )
angle# = atanfull(jx#,jy#)

A lot will depend on what angle the ship is in your image so just add or take away 90 / 180 degrees to get the ship pointing in the right direction.


this.mess = abs(sin(times#))
Zeerun
AGK Developer
18
Years of Service
User Offline
Joined: 28th Aug 2005
Location: United Kingdom
Posted: 21st Sep 2012 16:52
Thank you! Tested it in FE and it does just that!

Ship faces forward (up) at angle 0.
Unfortunately just rotating it won't work as I don't want the transition to be instant, but certainly sets me in the right direction!

I will need something along the lines of
while angle is not spite angle
if angle is greater than sprite angle then
sprite angle = sprite angle + turnrate
else
sprite angle = sprite angle - turnrate
endif
endwhile

I seek perfection but wear my scars with pride
baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 21st Sep 2012 16:58 Edited at: 21st Sep 2012 16:58
A small present:



this.mess = abs(sin(times#))
Zeerun
AGK Developer
18
Years of Service
User Offline
Joined: 28th Aug 2005
Location: United Kingdom
Posted: 21st Sep 2012 17:13
Thank you!

That looks awesome

I seek perfection but wear my scars with pride
baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 21st Sep 2012 17:19
The wrap function is necessary as otherwise you sometimes spin over 180 degrees to point in the right direction. I've done a few top down demos and started games that use this kind of steering. In fact I have one that I'd like to get back to in my own time at some point that's a remake of a cool arcade game I once played on holiday in Spain when I was a kid... wish I could remember the name of the game, it was awesome


this.mess = abs(sin(times#))
Marl
12
Years of Service
User Offline
Joined: 19th Nov 2011
Location: Bradford, UK
Posted: 21st Sep 2012 21:50
atanfull() ?

Bah! it was hidden in the core commands section.

I wish I'd seen that before I wrote this;


..and yeah I know it's sloppy code, I was in a rush

on a related note, I use the following for the sprite angle conversion

The wrap angle functions are from the community project.

Login to post a reply

Server time is: 2024-05-04 19:00:25
Your offset time is: 2024-05-04 19:00:25