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 / What happened to the Sprite Velocity command?

Author
Message
SirRolo
8
Years of Service
User Offline
Joined: 24th Mar 2016
Location:
Posted: 15th May 2018 19:32
Could someone help me figure out how I am going to get my sprite to follow my mouse, It is not working to good the way I have made it and I cannot find any command to move a sprite that is simple like in dark basic.



Function MoveVehicle()

if GetRawMouseLeftState() = 1

X# = GetRawMouseX()
Y# = GetRawMouseY()
SpX# = GetSpriteX(RED_VEHICLE)
SpY# = GetSpriteY(RED_VEHICLE)

angle# = ATanFull(x# - GetSpriteXByOffset(RED_VEHICLE), y# - GetSpriteYByOffset(RED_VEHICLE))

SetSpriteAngle(RED_VEHICLE, angle#)



if X# <= SpX# and Y# <= SpY#
SetSpriteX ( RED_VEHICLE,SpX# - 4 )
SetSpriteY ( RED_VEHICLE,SpY# - 4 )
Endif

if X# <= SpX# and Y# >= SpY#
SetSpriteX ( RED_VEHICLE,SpX# - 4 )
SetSpriteY ( RED_VEHICLE,SpY# + 4 )
Endif

if X# >= SpX# and Y# <= SpY#
SetSpriteX ( RED_VEHICLE,SpX# + 4 )
SetSpriteY ( RED_VEHICLE,SpY# - 4 )
Endif

if X# >= SpX# and Y# >= SpY#
SetSpriteX ( RED_VEHICLE,SpX# + 4 )
SetSpriteY ( RED_VEHICLE,SpY# + 4 )
Endif

endif

EndFunction

Golelorn
8
Years of Service
User Offline
Joined: 20th Nov 2016
Location:
Posted: 16th May 2018 03:49 Edited at: 16th May 2018 03:58
SirRolo,

If you turn 2d physics on you can use the velocity commands.

https://www.appgamekit.com/documentation/Reference/Sprite.htm

Here is my old code to set a velocity for an arrow where I did not use the 2d physics for velocity ( the 2d physics are bad ass I encourage you to check it out ) . I was offsetting the view port. If you're not offsetting, then its slightly different but you should still be able to work it out.



I also see you are using getspriteXByoffset and setspriteX. These usually are not interchangeable. At least, in the projects I have worked with.

If you just want your sprite to move with your mouse why not:
SirRolo
8
Years of Service
User Offline
Joined: 24th Mar 2016
Location:
Posted: 17th May 2018 01:06
I tried every command I could find using physics. I am just not getting it. I turned on Physics. Turned off Gravity. And I can not get the Sprite to follow my pointer very well. Dark basic just had a velocity command that would just move the sprite till you turned it off. I cannot for the life of me make a sprite do this.
PSY
Developer
8
Years of Service
User Offline
Joined: 3rd Jul 2016
Location: Laniakea Supercluster
Posted: 17th May 2018 09:31 Edited at: 17th May 2018 09:34
Hey,

try this:



Code and media attached to post, just download, unrar and compile!
The greater the distance between mouse and car, the faster the car moves toward the mouse


PSY


PSY LABS Games
Coders don't die, they just gosub without return

Attachments

Login to view attachments
IronManhood
9
Years of Service
User Offline
Joined: 6th Feb 2015
Location: US
Posted: 17th May 2018 16:16 Edited at: 17th May 2018 16:16
Like this?


SirRolo
8
Years of Service
User Offline
Joined: 24th Mar 2016
Location:
Posted: 18th May 2018 02:36
Hey Psy, thank you for the help. The code above does very similar as the same as mine. I am just trying to get my car to follow the mouse and stay at the same speed and stop when it gets to the mouse or when it hits a certain sprite. Isn't there just some easy way to make it do that without it slowing down? I do appreciate the help though.
SirRolo
8
Years of Service
User Offline
Joined: 24th Mar 2016
Location:
Posted: 18th May 2018 02:44
Hey IronManHood, thank you for your help. This is the other way I tried to do it with physics and it won't act right. I just need the sprite to move to the mouse when the mouse is pressed, and stop when it reaches the mouse pointer or when I release the mouse button. Any help would be greatly appreciated.
SirRolo
8
Years of Service
User Offline
Joined: 24th Mar 2016
Location:
Posted: 18th May 2018 05:16
OK, I have gotten a little bit farther. I have taken the X distance and Y distance, calculated it to get the total distance. Once I have the total distance, i divide that into the X and Y distance to get a percentage. I use that percentage to tell it to setspriteposition how much to adjust it on the X and Y. I can get it to move at a constant till it reaches the mouse X and Y. After that I cannot make it stop.


PSY
Developer
8
Years of Service
User Offline
Joined: 3rd Jul 2016
Location: Laniakea Supercluster
Posted: 18th May 2018 06:54
Hey,

If you want constant speed, just normalize the 2D vector.
In order to stop the car when it's near the mouse position, you have to check the x AND the y distance.
Also, your Pythagoras formula is incorrect.

Here's my example from above, slightly modified to suit your needs. Just use the media in the post above:



PSY


PSY LABS Games
Coders don't die, they just gosub without return
SirRolo
8
Years of Service
User Offline
Joined: 24th Mar 2016
Location:
Posted: 18th May 2018 16:28
Thank you very much Psy, I will get to work incorporating this. You do realize that dark basic had just 1 command That will accomplish most of everything I am trying to do, II thought I was missing something and was ready to give up on this AppGameKit but I realize I just need to get better at programming.
SirRolo
8
Years of Service
User Offline
Joined: 24th Mar 2016
Location:
Posted: 18th May 2018 19:26
Hot Dam it works like a charm Psy. You da man. I only had one problem but I messed around with it and it works wonderful. Thank You very much.
PSY
Developer
8
Years of Service
User Offline
Joined: 3rd Jul 2016
Location: Laniakea Supercluster
Posted: 19th May 2018 06:35
Hey SirRolo,

You're very welcome. Glad I could help

I used to code in DarkBasic, too. Still have the original packaging


Have a nice one,
PSY


PSY LABS Games
Coders don't die, they just gosub without return

Login to post a reply

Server time is: 2024-11-23 00:43:05
Your offset time is: 2024-11-23 00:43:05