Can someone help a beginner with some math?

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
Razvii
Prole
Posts: 5
Joined: Sat Jul 31, 2021 6:14 pm

Can someone help a beginner with some math?

Post by Razvii »

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
image_2021-08-01_011134.png
image_2021-08-01_011134.png (19.09 KiB) Viewed 3344 times
Attachments
TopDown.zip
(29.36 KiB) Downloaded 85 times
User avatar
pgimeno
Party member
Posts: 2789
Joined: Sun Oct 18, 2015 2:58 pm

Re: Can someone help a beginner with some math?

Post by pgimeno »

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.

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
(Edit: Fix typo)
Last edited by pgimeno on Sun Aug 01, 2021 7:13 pm, edited 1 time in total.
User avatar
darkfrei
Party member
Posts: 396
Joined: Sat Feb 08, 2020 11:09 pm

Re: Can someone help a beginner with some math?

Post by darkfrei »

Normally x is cos and y is sin.
:awesome: Falling in LÖVE :awesome:
I Löve Lua and Love2D
User avatar
pgimeno
Party member
Posts: 2789
Joined: Sun Oct 18, 2015 2:58 pm

Re: Can someone help a beginner with some math?

Post by pgimeno »

Yeah, and atan2 requires y,x instead of x,y which is why it still works.
Razvii
Prole
Posts: 5
Joined: Sat Jul 31, 2021 6:14 pm

Re: Can someone help a beginner with some math?

Post by Razvii »

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.

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
(Edit: Fix typo)
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.
User avatar
pgimeno
Party member
Posts: 2789
Joined: Sun Oct 18, 2015 2:58 pm

Re: Can someone help a beginner with some math?

Post by pgimeno »

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.
Here's the vector math solution:

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
Example attached. Arrows to move player, space to shoot.
Attachments
bulletspreaddemo.love
(885 Bytes) Downloaded 56 times
Post Reply

Who is online

Users browsing this forum: Bing [Bot], pgimeno and 10 guests