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 / [SOLVED] help with bullets moving in the direction player aims (2d game)

Author
Message
dandrews
3
Years of Service
User Offline
Joined: 26th Jun 2020
Location:
Posted: 29th Nov 2021 04:25
im having trouble with the math of how to get a bullet to move in the direction a player aims with a 2d sprite. i can get the player sprite to rotate to the correct angle but when it come time for the bullets to move i cant get it down. i was using a virtual joystick

but i would like to convert it over to use dragangle

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

Go to answer

MadBit
VIP Member
Gold Codemaster
14
Years of Service
User Offline
Joined: 25th Jun 2009
Location: Germany
Posted: 29th Nov 2021 04:52 Edited at: 29th Nov 2021 04:53
When you shoot your bullet. Then save the direction in which the bullet should fly as follows.

move_x = sin(angle_player) * bullet_speed
move_y = -cos(angle_player) * bullet_speed


When moving, you only add the current position of your bullet with the direction and speed of movement.
SetSpritePosition(bullet,GetSpriteX(bullet)+move_x, GetSpriteY(bullet)+move_y)

I hope this helps.
Share your knowledge. It\'s a way to achieve immortality. (Tenzin Gyatso)
Virtual Nomad
Moderator
18
Years of Service
Recently Online
Joined: 14th Dec 2005
Location: SF Bay Area, USA
Posted: 29th Nov 2021 05:08 Edited at: 29th Nov 2021 05:12
This post has been marked by the post author as the answer.
what madbit said (while i was writing this):

meanwhile, next time show a short snippet of what you're trying to achieve vs "all that other stuff" ? i couldn't bother to sift through it but had an idea of what you were trying to do
blink0k
Moderator
11
Years of Service
User Offline
Joined: 22nd Feb 2013
Location: the land of oz
Posted: 29th Nov 2021 05:30
This uses a technique of taking a point within the bullet sprite and using that to move the bullet.
Note that using the AppGameKit angle calculations, it assumes that the sprite, when at angle zero, is pointing directly up

dandrews
3
Years of Service
User Offline
Joined: 26th Jun 2020
Location:
Posted: 30th Nov 2021 18:35 Edited at: 30th Nov 2021 18:36
thank you all for the help! i did edit the code provides by nomad and was able to get it working

i know it looks a little sloppy but i didnt want to just copy and paste code im trying to understand it all and experimenting with it but i think im getting a hold on it, again thank you all
MadBit
VIP Member
Gold Codemaster
14
Years of Service
User Offline
Joined: 25th Jun 2009
Location: Germany
Posted: 1st Dec 2021 04:53
I would like to suggest a small improvement.
Instead of saving the angle when shooting the bullet, I would immediately calculate the direction.
Since that should not change after shooting.
Unless the bullet had some kind of target tracking .

so you would not have to do the same sin / cos calculation for each bullet in each frame.
Theoretically you could also multiply the speed (Rate#).
Then this calculation would happen only once.
Share your knowledge. It\'s a way to achieve immortality. (Tenzin Gyatso)

Login to post a reply

Server time is: 2024-04-19 00:24:45
Your offset time is: 2024-04-19 00:24:45