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 / AGK v2 - Point Arcing Projectile Towards Direction of Movement

Author
Message
crazy_asian
9
Years of Service
User Offline
Joined: 13th Jul 2015
Location:
Posted: 29th Jul 2015 00:23
Hey Guys,

I have a sidescroller where my player can shoot arrows. The arrows are using physics to properly arc from the player.

Initially the angle of the arrow points towards the mouse pointer. However, while the arrow is travelling, it stays pointed in that direction the entire time which looks a bit goofy

Think Towerfall perhaps? In that game the arrows always point towards the direction they're travelling.

Any ideas? Do I need to maybe calculate the arrow's position every few frames to get direction, and then calculate angle based on that? That sounds processor intensive and not very efficient. Maybe there's already an existing function that points sprite towards movement direction - but I haven't been able to find anything yet.

Any suggestions would be appreciated.

Thanks Guys!

In the meantime, I'll keep at it. If I find a solution, I'll post a follow up.

Tim
Uncle Martin
18
Years of Service
User Offline
Joined: 9th Jul 2006
Location: Tampa, Florida, USA
Posted: 31st Jul 2015 08:51
Tim, I have not tried it, but I would think you can use the following approach to get a rotation angle based on the sprite velocity components. No doubt, you will have to adjust how you apply the Angle to your sprite rotation, but this should allow you to point the sprite in the direction of its "velocity vector".

Xvel# = GetSpritePhysicsVelocityX( iSpriteIndex )
Yvel# = GetSpritePhysicsVelocityY( iSpriteIndex )

Angle# = ATan2(Yvel#, Xvel#)

Enjoy.

Code every line like it might be your last...
Someday it will be.
BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 31st Jul 2015 12:29 Edited at: 31st Jul 2015 12:30
Having battled with angles over the last few days, I would agree this is a very neat solution!

And...I am probably going to replace some of my overly complicated code with this.

Quidquid latine dictum sit, altum sonatur
TutCity is being rebuilt
crazy_asian
9
Years of Service
User Offline
Joined: 13th Jul 2015
Location:
Posted: 31st Jul 2015 16:50
Agreed. Wow, I have been way overthinking this. Thanks for the very clean solution!

Hopefully I can try this out tonight when the kids are in bed.
crazy_asian
9
Years of Service
User Offline
Joined: 13th Jul 2015
Location:
Posted: 1st Aug 2015 20:30
Uncle Martin,

I tried this last night and it worked like a charm!

It was awesome to see that even when I shot my arrows straight up, they would flip down when they were falling. So sweet.

Thanks again!

Tim
Uncle Martin
18
Years of Service
User Offline
Joined: 9th Jul 2006
Location: Tampa, Florida, USA
Posted: 2nd Aug 2015 05:26
My pleasure nephew Tim, I knew with a little help you would make it awesome.

Code every line like it might be your last...
Someday it will be.

Login to post a reply

Server time is: 2024-11-25 19:36:05
Your offset time is: 2024-11-25 19:36:05