Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411482 Posts in 69370 Topics- by 58426 Members - Latest Member: shelton786

April 23, 2024, 10:39:39 PM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsDeveloperTechnical (Moderator: ThemsAllTook)Unity 2D Quaternion/Rotation Help
Pages: [1]
Print
Author Topic: Unity 2D Quaternion/Rotation Help  (Read 1340 times)
armedpatriots
Level 0
**


Game Designer/ Programmer


View Profile
« on: September 17, 2014, 12:14:43 PM »

Hey guys, I am trying to make a 2D platformer with a bow as the primary weapon. I am having quite a bit of trouble however trying to figure out how to get my character to aim the bow and shoot towards the mouse position. Can someone help explain to me what would be the most efficient way to accomplish this? Do I use Euler angles? Thanks in advance for the help!
Logged

I have not failed. I've just found 10,000 ways that won't work.
-Thomas Edison
ThemsAllTook
Administrator
Level 10
******



View Profile WWW
« Reply #1 on: September 17, 2014, 01:29:31 PM »

atan2(mousePosition.y - characterPosition.y, mousePosition.x - characterPosition.x)
Logged

Krux
Level 2
**



View Profile
« Reply #2 on: September 17, 2014, 07:52:45 PM »

Quaternions are totally unnecessary in 2D. the 2D equivalent of quternions for 2d are normalized complex numbers and the angle of that complex number is your angle. then multipy complex numbers becomes rotation. But you can also just store the angle and add angles for rotation wich is much more intuitive.
Logged
powly
Level 4
****



View Profile WWW
« Reply #3 on: September 18, 2014, 05:31:07 AM »

Or just stick to vector notation with everything - you can just take the vector dir' = (mouse - player) and normalize that (divide by it's length so it becomes of length 1; dir = dir'/|dir'|). Then when you want to shoot you just say "arrow.velocity = dir * speed" and start simulating the arrow with some kind of time integration. Rendering kind of depends on your API - if you can just specify coordinates of the geometry you can map your homogeneous 2D coordinate (x,y,1) through a linear map of ((dir.x,-dir.y,pos.x),(dir.y,dir.x,pos.y),(0,0,1)) (or, more explicitly, x' = dir.x*x-dir.y*x+pos.x, y' = dir.y*x+dir.x*y+pos.y) and be done with it, if you need an angle you can do "ang = std::atan2(dir.y, dir.x)" - this will take care of all the singularities and sign problems related to arctan.
Logged
Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic