I am trying to somewhat independently learn how to use lua and love, though I have used lua a bit in the past i am confused about some stuff, namely calculating the vector for a projectile and some other stuff, i have followed skyvaultgames' first love tutorial for the shooting part, it does work but very oddly and weidly, and getting close to the edges makes it act really weird and shoot randomly, can someone help and explain this stuff as well as tell me how i could improve everything else? Please have some patience with me.
edit: modified the formula to the attached one and now it targets perfectly, it was my user error, still would appreciate some advice on the code
Can someone help a beginner with some math?
Can someone help a beginner with some math?
- Attachments
-
- TopDown.zip
- (29.36 KiB) Downloaded 143 times
Re: Can someone help a beginner with some math?
Welcome to the forums.
Taking the atan2 for passing it to sine and cosine is something that is unfortunately seen very frequently. These are unnecessary calculations when all you need is a unit vector, needing just a square root.
(Edit: Fix typo)
Taking the atan2 for passing it to sine and cosine is something that is unfortunately seen very frequently. These are unnecessary calculations when all you need is a unit vector, needing just a square root.
Code: Select all
local vx, vy = targetx - x, targety - y
local len = math.sqrt(vx*vx + vy*vy)
if len > 1e-15 then
-- length is reasonable; divide by length to get a unit vector, then multiply it by speed.
vx = vx / len * speed
vy = vy / len * speed
else
-- target extremely close; it's hard to tell the actual direction. Point right in that case.
vx = speed
vy = 0
end
Last edited by pgimeno on Sun Aug 01, 2021 7:13 pm, edited 1 time in total.
Re: Can someone help a beginner with some math?
Normally x is cos and y is sin.
Re: Can someone help a beginner with some math?
Yeah, and atan2 requires y,x instead of x,y which is why it still works.
Re: Can someone help a beginner with some math?
Thank you! I will need to study math in this sense a bit more, this works wonders, was also wondering, if I were to need bullet spread for this how could I implement it without the spread being dependent on how far away the target/mouse is? just aiming 10 pixels above and below with each bullet for example will result in bullets spreading more when the target is close and less when its far.pgimeno wrote: ↑Sun Aug 01, 2021 12:33 am Welcome to the forums.
Taking the atan2 for passing it to sine and cosine is something that is unfortunately seen very frequently. These are unnecessary calculations when all you need is a unit vector, needing just a square root.
(Edit: Fix typo)Code: Select all
local vx, vy = targetx - x, targety - y local len = math.sqrt(vx*vx + vy*vy) if len > 1e-15 then -- length is reasonable; divide by length to get a unit vector, then multiply it by speed. vx = vx / len * speed vy = vy / len * speed else -- target extremely close; it's hard to tell the actual direction. Point right in that case. vx = speed vy = 0 end
Re: Can someone help a beginner with some math?
Here's the vector math solution:Razvii wrote: ↑Tue Aug 03, 2021 1:30 am Thank you! I will need to study math in this sense a bit more, this works wonders, was also wondering, if I were to need bullet spread for this how could I implement it without the spread being dependent on how far away the target/mouse is? just aiming 10 pixels above and below with each bullet for example will result in bullets spreading more when the target is close and less when its far.
Code: Select all
local ux, uy = targetx - x, targety - y
local vx, vy
local len = math.sqrt(ux*ux + uy*uy)
if len > 1e-15 then
-- length is reasonable; divide by length to get a unit vector
ux = ux / len
uy = uy / len
-- now apply spread (the variable 'spread' should probably be at the top)
local spread = 0.2 -- units of spread per each unit of distance; e.g. 1 means 45° to each side
-- calculate a horizontal vector with length approximately equal to speed
vx, vy = speed, speed*(love.math.random()*2*spread-spread)
-- rotate it to align it to ux, uy
vx, vy = ux * vx + uy * vy, uy * vx - ux * vy
else
-- target extremely close; it's hard to tell the actual direction. Point right in that case.
vx = speed
vy = 0
end
- Attachments
-
- bulletspreaddemo.love
- (885 Bytes) Downloaded 117 times
Who is online
Users browsing this forum: No registered users and 148 guests