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.

Newcomers AppGameKit Corner / [Tier 1] How to move a sprite to mouse position with fixed speed?

Author
Message
Multicoder
AGK Developer
4
Years of Service
User Offline
Joined: 25th Oct 2014
Location: Germany
Posted: 30th Apr 2019 04:00 Edited at: 30th Apr 2019 04:04
Hey there

I am not exactly new here, but it's kind of a newcomer question, I think.

I have a sprite which moves via SetSpritePhysicsVelocity() to the mouse position when I left click (or hold).
Currently the speed at which the sprite moves is dependant on how far away the mouse is.

How can I make it so the sprite always moves at the same speed no matter how far away the mouse pointer is?

Here is a video showcasing my problem: https://youtu.be/R2ECjS7rbL4

This is my code to move the sprite (i_spr_player is the ID of the player sprite):


~ Daniel
Yes, I am aware that I sound like a caveman to native english speakers
xtremi
User Offline
Joined: 26th Aug 2018
Location:
Posted: 30th Apr 2019 09:40 Edited at: 30th Apr 2019 09:52
So it looks like the speed you assign to the sprite is equal to the distance between the mouse and the sprite.

You should normalize the vector from the sprite to the pointer, then multiply it by your desired speed.

(to normalize; divide the x-distance and y-distance by sqrt(x-distance^2 + y-distance^2))

EDIT:
Something like this:

Multicoder
AGK Developer
4
Years of Service
User Offline
Joined: 25th Oct 2014
Location: Germany
Posted: 30th Apr 2019 10:57
Thank you very much xtremi
That is exactly what I was looking for
Yes, I am aware that I sound like a caveman to native english speakers

Login to post a reply

Server time is: 2019-07-23 13:37:55
Your offset time is: 2019-07-23 13:37:55