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 / [SOLVED] Rotation doesn't work as expected?!

Author
Message
GunnerJnr
AGK Developer
8
Years of Service
User Offline
Joined: 30th Nov 2015
Location: Bridgwater, UK
Posted: 25th Sep 2018 18:39 Edited at: 25th Sep 2018 18:45
Hi Everyone,

I am trying to make it so that the player rotates to the mouse pointer, this is for a top down style point and shoot kind of game.

It is easier if I show a small GIF demonstrating what happens with the rotation as it will be easier than trying to explain.

Code for RotatePlayer() Function, it is probably also worth mentioning the player is loaded by its offset (so using the center of the image as the anchor point). Also it seems as if the 0 position for rotation is to the right which would make sense as that is the facing direction of the sprite when loaded.

***EDIT***

Forgot to put how I obtain the playerX/playerY and mouseX/mouseY, here they are in another code snippet..



*** END EDIT ***



Here is the GIF demonstrating what happens when the game is running..



Any help or pointing in the right direction would be greatly appreciated.

The author of this post has marked a post as an answer.

Go to answer

puzzler2018
User Banned
Posted: 25th Sep 2018 18:55
Maybe



GunnerJnr
AGK Developer
8
Years of Service
User Offline
Joined: 30th Nov 2015
Location: Bridgwater, UK
Posted: 25th Sep 2018 19:20 Edited at: 25th Sep 2018 19:22
Hi @puzzler2018,

Thanks for the quick response, having reviewed your code and trying it, the only real difference I see between mine and yours is that you use GetPointerX() & GetPointerY() to obtain the mouse position opposed to me using GetRawMouseX() & GetRawMouseY()..

Is there any benefit to doing it this way ? Aside from the obvious it will work for multiple devices, which at the minute I only plan to make for Windows platform.

Having tried it the program acts exactly the same and has no difference in result.

Kind Regards

-------------------------------------------------------------------------------------------------------------------------------------------------------------------

Since the last post I decided to have a little play with the code, and it seems to be a little better in following the mouse pointer although I feel its still a bit buggy..

Updated Code



New GIF demonstrating the new rotation to mouse pointer follow, note if I get close the the player it goes all crazy..



Thanks
GarBenjamin
AGK Developer
7
Years of Service
User Offline
Joined: 30th Nov 2016
Location: USA
Posted: 25th Sep 2018 19:24 Edited at: 25th Sep 2018 19:26
Looks like puzzler got you sorted but anyway if you look over here you will find a small library of useful functions I wrote including SetSpriteFaceSprite, SetSpriteFacePosition and SetSpriteFaceMouse that will simplify it.

Code is right at top of that first post. Think I have some updates I need to get in there too at some point.
puzzler2018
User Banned
Posted: 25th Sep 2018 19:28
Try 180



GunnerJnr
AGK Developer
8
Years of Service
User Offline
Joined: 30th Nov 2015
Location: Bridgwater, UK
Posted: 25th Sep 2018 19:39
@GarBenjamin

Wow thanks these look great, I will take a look for certain. Thanks again for sharing.

---------------------------------------------------------------------------------------

@puzzler2018

I tried 180 and it only seems to point forward with the mouse behind the sprite, it seems having it at 90 (as in the above 2nd GIF) is the only way it almost perfectly follows the mouse pointer, the reason it needs to be at the front is because when the player will click to shoot the bullets will need to go towards the pointer, and if it is sat behind the player at 180 its not going to be good for the player if the enemies are coming at the front.

Thanks for all the help though, greatly appreciated.
puzzler2018
User Banned
Posted: 25th Sep 2018 19:43 Edited at: 25th Sep 2018 19:45
This post has been marked by the post author as the answer.
Your welcome,

it does require a right angled sprite to begin with to make the angle work.

Its a trial and error type of thing on that angle -90 and then -180. but thats on a sprite that created on a 0 angle.

If your sprite is created on a different angle to begin with - try go through all the possible angles between -360 to 360 until you get the perfect angle for your work

But this code will do the trick - just the angles are off for your sprite
puzzler2018
User Banned
Posted: 25th Sep 2018 19:51
Maybe perhaps make use of getspriteangle (PLAYER_IMAGE_INDEX) to get the current angle of that sprite and do an offset angle

Its all new to me this 2D stuff but coming back slowly but surely, so forgive me for not been 100%
GunnerJnr
AGK Developer
8
Years of Service
User Offline
Joined: 30th Nov 2015
Location: Bridgwater, UK
Posted: 25th Sep 2018 20:12
@puzzler2018

I think I see why 90 works well for me as when the sprite is drawn the angle for 0 rotation is actually right facing, which would make sense as that would be the equivalent to 90 degrees if he was drawn facing the top of the window.



I marked yours as the answer, as it is pretty damn close, just needs a bit of tweaking then it should be Okay.
Thanks again for all your help.

Kind Regards
smallg
Valued Member
18
Years of Service
User Offline
Joined: 8th Dec 2005
Location: steam
Posted: 26th Sep 2018 09:43 Edited at: 26th Sep 2018 09:43
Also take care to put the offset of the sprite in a good location, if you leave it at default it will be in the center of the image/sprite but it's more likely you would want it where his body is.
This will have a bigger impact if the sprite is not Square in shape too.
life's one big game
spec= 4ghz, 16gb ram, AMD R9 2700 gpu
GunnerJnr
AGK Developer
8
Years of Service
User Offline
Joined: 30th Nov 2015
Location: Bridgwater, UK
Posted: 26th Sep 2018 10:56
@smallg

smallg wrote: "Also take care to put the offset of the sprite in a good location, if you leave it at default it will be in the center of the image/sprite but it's more likely you would want it where his body is.
This will have a bigger impact if the sprite is not Square in shape too."


Thanks for the added advice dude.

Login to post a reply

Server time is: 2024-11-23 16:09:41
Your offset time is: 2024-11-23 16:09:41