Angles and coordinates
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 =)
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
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.
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.
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.
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.
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
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
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement