• Advertisement
Sign in to follow this  

Angles and coordinates

This topic is 3826 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am making a game and im trying to get a bullet wehn shot to be sent shooting in the direction the player is facing. well the thing is the draw function only asks for a x and y, i cant just put the angle i want it to go and make it go that way, how would i make it so that it will send the Bullet flying in the direction of the mouse at the time it is shot? heres my code. Its in Blitz3d " Function Bullets() For Bullet.Bullet=Each Bullet Bullet\x=Bullet\x Bullet\y=Bullet\y If Bullet\y<0 Delete Bullet Else DrawImage Bullet\image,Bullet\x,Bullet\y DrawImage Bullet\image,Bullet\x,Bullet\y EndIf Next End Function " I need an x and y. but like "Bullet\x=Bullet\x" with a + or - of a number to send it in that direction. I hope someone understands this lol. thanks in advance =)

Share this post


Link to post
Share on other sites
Advertisement

vector to mouse = mouse position - object position




that's your firing direction.


To figure out where do draw the bullet this frame:


current position = velocity*time + position last frame



current position will be an x,y coordinate

velocity is basically your speed (but a directional speed); so a vector in the direction of "vector to mouse" scaled to the length of the bullet speed.

time is time since last frame

"position last frame" is where the bullet was last frame.



You'll likely have to learn a bit of Linear Algebra for this to make more sense.

-me

Share this post


Link to post
Share on other sites
Thanks man, Im actually pretty good at thinking things through so i pretty much understand this. But i never thaught about it this way, thank you man =). Ill edit if it works.

turns out theres no velocity function in blitz 3d...
I understand the first part thats like

"atan2(mousey-Player.Actor\y,mousey-Player.Actor\x)
"

But the velocity part i cant do without a velocity function.

Share this post


Link to post
Share on other sites
Quote:
Original post by Barkanaxen
turns out theres no velocity function in blitz 3d...


Quote:
Original post by Palidine
velocity is basically your speed (but a directional speed); so a vector in the direction of "vector to mouse" scaled to the length of the bullet speed.

Share this post


Link to post
Share on other sites
well, ok i know he said that but i dont completely get it. I am constantly trying to understand it but i need help understanding it.

Share this post


Link to post
Share on other sites
Here's a 2D example. Say your mouse is at position Mx, My. Now, Say your object is at Ox, Oy. The vector V from object to mouse is (Mx-Ox, My-Oy). Lets call these new values Vx and Vy. The length of V is the square root of Vx^2 + Vy^2. Divide Vx and Vy by this length to normalize V, such that it's length is now one. Now, let's say your bullets have a speed S units per frame. This means that every frame, you should take your bullet's position (Bx, By) and add to it (Vx*S, Vy*S), ie, every frame Bx = Bx + Vx * S, By = By + Vy * S.

In pseudo-code:
V = M - O
V = V / |V|
each frame:
B = B + V * S

Edit: Or, if you have a variable frame time:
B = B + V * t
where V is in units/second and t is the number of seconds since last update

Share this post


Link to post
Share on other sites
Jeez i have been working with ur code forever, not being offensive, and i like am coming up with the most random results. u explained it great but when i try it im just not getting it right.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement