Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Calculate the angle between two vectors (C++)
New Posts  All Forums:Forum Nav:

Calculate the angle between two vectors (C++)

post #1 of 7
Thread Starter 
I need to rotate a tower to face the player in a game made in openGL. v1 is the normalized vector of the player and v2 of the tower. Please consider 2d vectors, x = v[0] and y = v[2]. So this is what I've got:

float calcProd(float v1[3], float v2[3]){
float aux;
float prod = v1[0]*v2[0] + v1[2]*v2[2];
aux = acos(prod);
if(prod < 0)
return asin(prod);
else return aux;
}

It doesn't quite work... Thanks for the help smile.gif
Rubbish
(15 items)
 
  
CPUMotherboardGraphicsRAM
E7400 @ 4ghz 1.376v Asus P5Q3 Sapphire HD6870 Kingston 4GB DDR3 1333MHZ 
Hard DriveHard DriveOSOS
Samsung Spinpoint F3 WD Green Windows 7 64bit Lubuntu 64bit 
PowerCaseMouse
OCZ ModXStream 700W Cooler Master 690 II with side window Razer Deathadder Black Edition 
  hide details  
Reply
Rubbish
(15 items)
 
  
CPUMotherboardGraphicsRAM
E7400 @ 4ghz 1.376v Asus P5Q3 Sapphire HD6870 Kingston 4GB DDR3 1333MHZ 
Hard DriveHard DriveOSOS
Samsung Spinpoint F3 WD Green Windows 7 64bit Lubuntu 64bit 
PowerCaseMouse
OCZ ModXStream 700W Cooler Master 690 II with side window Razer Deathadder Black Edition 
  hide details  
Reply
post #2 of 7
I was honestly expecting this

762efbae29e05488cb5e7e5f3afabb72.jpg
Tardis
(13 items)
 
  
CPUMotherboardGraphicsRAM
|2500k @ 4.4Ghz vcore 1.40| MSI P67A-GD65 290X G.SKILL Ripjaws Series 16GB (4 x 4GB) 1600Mhz 
Hard DriveOSMonitorPower
C300 64GB SSD - 500GB HDD - 840 250GB SSD Windows 7 Ultimate 64bit 3 HP LA2405wg 24'' 1920x1200 @5760x1200 Corsair 850watts 
CaseMouse
NZXT Phantom Black G500 
  hide details  
Reply
Tardis
(13 items)
 
  
CPUMotherboardGraphicsRAM
|2500k @ 4.4Ghz vcore 1.40| MSI P67A-GD65 290X G.SKILL Ripjaws Series 16GB (4 x 4GB) 1600Mhz 
Hard DriveOSMonitorPower
C300 64GB SSD - 500GB HDD - 840 250GB SSD Windows 7 Ultimate 64bit 3 HP LA2405wg 24'' 1920x1200 @5760x1200 Corsair 850watts 
CaseMouse
NZXT Phantom Black G500 
  hide details  
Reply
post #3 of 7
Thread Starter 
Ok make fun of me all you want but please give me an answer. I have to deliever this project tomorrow...
Rubbish
(15 items)
 
  
CPUMotherboardGraphicsRAM
E7400 @ 4ghz 1.376v Asus P5Q3 Sapphire HD6870 Kingston 4GB DDR3 1333MHZ 
Hard DriveHard DriveOSOS
Samsung Spinpoint F3 WD Green Windows 7 64bit Lubuntu 64bit 
PowerCaseMouse
OCZ ModXStream 700W Cooler Master 690 II with side window Razer Deathadder Black Edition 
  hide details  
Reply
Rubbish
(15 items)
 
  
CPUMotherboardGraphicsRAM
E7400 @ 4ghz 1.376v Asus P5Q3 Sapphire HD6870 Kingston 4GB DDR3 1333MHZ 
Hard DriveHard DriveOSOS
Samsung Spinpoint F3 WD Green Windows 7 64bit Lubuntu 64bit 
PowerCaseMouse
OCZ ModXStream 700W Cooler Master 690 II with side window Razer Deathadder Black Edition 
  hide details  
Reply
post #4 of 7
wouldn't you have to do this to actually get the angle?

aux = acos(prod)* 180.0 / PI;

http://www.cplusplus.com/reference/clibrary/cmath/acos/
Fractal Design
(15 items)
 
775 4 life
(15 items)
 
 
CPUMotherboardGraphicsRAM
Intel i7 2600K Biostar TP67XE NVidia GTX 570 Crucial Ballistix 
Hard DriveHard DriveCoolingOS
Crucial C300 RealSSD SDD Samsung F4 2TB Noctua NH-D14 Windows 7 Professional x64 
MonitorMonitorKeyboardPower
Asus VH202T 20'' 1600x900 Acer P244W 24" 1920 x 1080 Apple Keyboard with Numeric Keypad SeaSonic M12II 620W 
CaseMouseAudio
Fractal Design Define XL Titanium Grey Razor Abyssus Creative Sound Blaster X-FI Xtreme Gamer 
CPUMotherboardGraphicsRAM
Intel X3350 3.2Ghz @ 1.25v Gigabyte-GA-P35-DS3L (rev 2) XFX 4870 1GB 4GB OCZ Reaper PC2-6400 
RAMHard DriveHard DriveOptical Drive
2GB Corsair XMS2 PC2-6400 Crucial C300 64GB SSD 2TB Samsung Spinpoint F4 Sony Super Multi 
OSMonitorPowerCase
Windows 7 Professional x64 SP1 Asus VH202T 20'' 1600x900 SeaSonic M12II 620W Cooler Master Centurion 5 
Mouse
Razor Abyssus 
CPUMotherboardGraphicsRAM
Core i5-520M Lenovo 2522BF3 NVIDIA® Quadro® NVS3100M  Ramaxel Technology 4Gb DDR3 
Hard DriveOptical DriveOSMonitor
Samsung SSD 128GB 1.8" Micro SATA  hl-dt-st dvdram gu10n Windows 7 Enterprise (64-bit) 14.1" WXGA (1280x800) display, anti-glare, LED ... 
Power
9-cell plus Slice battery 
  hide details  
Reply
Fractal Design
(15 items)
 
775 4 life
(15 items)
 
 
CPUMotherboardGraphicsRAM
Intel i7 2600K Biostar TP67XE NVidia GTX 570 Crucial Ballistix 
Hard DriveHard DriveCoolingOS
Crucial C300 RealSSD SDD Samsung F4 2TB Noctua NH-D14 Windows 7 Professional x64 
MonitorMonitorKeyboardPower
Asus VH202T 20'' 1600x900 Acer P244W 24" 1920 x 1080 Apple Keyboard with Numeric Keypad SeaSonic M12II 620W 
CaseMouseAudio
Fractal Design Define XL Titanium Grey Razor Abyssus Creative Sound Blaster X-FI Xtreme Gamer 
CPUMotherboardGraphicsRAM
Intel X3350 3.2Ghz @ 1.25v Gigabyte-GA-P35-DS3L (rev 2) XFX 4870 1GB 4GB OCZ Reaper PC2-6400 
RAMHard DriveHard DriveOptical Drive
2GB Corsair XMS2 PC2-6400 Crucial C300 64GB SSD 2TB Samsung Spinpoint F4 Sony Super Multi 
OSMonitorPowerCase
Windows 7 Professional x64 SP1 Asus VH202T 20'' 1600x900 SeaSonic M12II 620W Cooler Master Centurion 5 
Mouse
Razor Abyssus 
CPUMotherboardGraphicsRAM
Core i5-520M Lenovo 2522BF3 NVIDIA® Quadro® NVS3100M  Ramaxel Technology 4Gb DDR3 
Hard DriveOptical DriveOSMonitor
Samsung SSD 128GB 1.8" Micro SATA  hl-dt-st dvdram gu10n Windows 7 Enterprise (64-bit) 14.1" WXGA (1280x800) display, anti-glare, LED ... 
Power
9-cell plus Slice battery 
  hide details  
Reply
post #5 of 7
Code:
Vector3 first = Vector3(1, 0, 0);
Vector3 second = Vector3(0, 1, 0);

first.Unitize(); // AKA Normalize
second.Unitize();

float dotProduct = first.Dot(second);   // Calculate dot product of two unit length vectors

float angle = acos(dotProduct);   // acos of dot product gives angle

This wouldn't give you the direction to rotate though, only the amount. For the direction use this (I wrote it as well):
http://www.xnawiki.com/index.php?title=Vector_Math#Signed_Difference_Angle_Between_Two_Vectors
Edited by lordikon - 4/15/11 at 6:46pm
Foldatron
(17 items)
 
Mat
(10 items)
 
Work iMac
(9 items)
 
CPUMotherboardGraphicsGraphics
i7 950 EVGA x58 3-way SLI EVGA GTX 660ti GTX 275 
RAMHard DriveHard DriveHard Drive
3x2GB Corsair Dominator DDR3-1600 80GB Intel X25-M SSD 2TB WD Black 150GB WD Raptor 
Hard DriveOSMonitorKeyboard
2x 150GB WD V-raptor in RAID0 Win7 Home 64-bit OEM 55" LED 120hz 1080p Vizio MS Natural Ergonomic Keyboard 4000 
PowerCase
750W PC P&C Silencer CoolerMaster 690 
CPUGraphicsRAMHard Drive
Intel Core i5 2500S AMD 6770M 8GB (2x4GB) at 1333Mhz 1TB, 7200 rpm 
Optical DriveOSMonitorKeyboard
LG 8X Dual-Layer "SuperDrive" OS X Lion 27" iMac screen Mac wireless keyboard 
Mouse
Mac wireless mouse 
CPUGraphicsRAMHard Drive
i7-2600K AMD 6970M 1GB 16GB PC3-10600 DDR3 1TB 7200rpm 
Hard DriveOptical DriveOSMonitor
256GB SSD 8x DL "SuperDrive" OS X 10.7 Lion 27" 2560x1440 iMac display 
Monitor
27" Apple thunderbolt display 
  hide details  
Reply
Foldatron
(17 items)
 
Mat
(10 items)
 
Work iMac
(9 items)
 
CPUMotherboardGraphicsGraphics
i7 950 EVGA x58 3-way SLI EVGA GTX 660ti GTX 275 
RAMHard DriveHard DriveHard Drive
3x2GB Corsair Dominator DDR3-1600 80GB Intel X25-M SSD 2TB WD Black 150GB WD Raptor 
Hard DriveOSMonitorKeyboard
2x 150GB WD V-raptor in RAID0 Win7 Home 64-bit OEM 55" LED 120hz 1080p Vizio MS Natural Ergonomic Keyboard 4000 
PowerCase
750W PC P&C Silencer CoolerMaster 690 
CPUGraphicsRAMHard Drive
Intel Core i5 2500S AMD 6770M 8GB (2x4GB) at 1333Mhz 1TB, 7200 rpm 
Optical DriveOSMonitorKeyboard
LG 8X Dual-Layer "SuperDrive" OS X Lion 27" iMac screen Mac wireless keyboard 
Mouse
Mac wireless mouse 
CPUGraphicsRAMHard Drive
i7-2600K AMD 6970M 1GB 16GB PC3-10600 DDR3 1TB 7200rpm 
Hard DriveOptical DriveOSMonitor
256GB SSD 8x DL "SuperDrive" OS X 10.7 Lion 27" 2560x1440 iMac display 
Monitor
27" Apple thunderbolt display 
  hide details  
Reply
post #6 of 7
Thread Starter 
Basically that's what I did in my first solution. The problem is that as I rotate around the tower, the dotProduct is rising until it reaches 1. at that point the tower becomes to rotate in the oposite way as the dotProduct is diminuishing...
Edited by lambecrikas - 4/16/11 at 3:13am
Rubbish
(15 items)
 
  
CPUMotherboardGraphicsRAM
E7400 @ 4ghz 1.376v Asus P5Q3 Sapphire HD6870 Kingston 4GB DDR3 1333MHZ 
Hard DriveHard DriveOSOS
Samsung Spinpoint F3 WD Green Windows 7 64bit Lubuntu 64bit 
PowerCaseMouse
OCZ ModXStream 700W Cooler Master 690 II with side window Razer Deathadder Black Edition 
  hide details  
Reply
Rubbish
(15 items)
 
  
CPUMotherboardGraphicsRAM
E7400 @ 4ghz 1.376v Asus P5Q3 Sapphire HD6870 Kingston 4GB DDR3 1333MHZ 
Hard DriveHard DriveOSOS
Samsung Spinpoint F3 WD Green Windows 7 64bit Lubuntu 64bit 
PowerCaseMouse
OCZ ModXStream 700W Cooler Master 690 II with side window Razer Deathadder Black Edition 
  hide details  
Reply
post #7 of 7
Doing a single dot product will not give you a direction (whether or not your player needs to turn left or right), which is why I provided the link in my post.

Additionally, your dot product will not be useful for rotations unless your vectors are normalized/unitized (which they may have been, I'm not sure what form they were in when they are being used in your example). There's definitely not need for asin to do rotations in games. acos should do what you need, assuming you pass a dot product in the range of -1 to 1 to it, which is what should happen as long as your vectors are of unit length.
Foldatron
(17 items)
 
Mat
(10 items)
 
Work iMac
(9 items)
 
CPUMotherboardGraphicsGraphics
i7 950 EVGA x58 3-way SLI EVGA GTX 660ti GTX 275 
RAMHard DriveHard DriveHard Drive
3x2GB Corsair Dominator DDR3-1600 80GB Intel X25-M SSD 2TB WD Black 150GB WD Raptor 
Hard DriveOSMonitorKeyboard
2x 150GB WD V-raptor in RAID0 Win7 Home 64-bit OEM 55" LED 120hz 1080p Vizio MS Natural Ergonomic Keyboard 4000 
PowerCase
750W PC P&C Silencer CoolerMaster 690 
CPUGraphicsRAMHard Drive
Intel Core i5 2500S AMD 6770M 8GB (2x4GB) at 1333Mhz 1TB, 7200 rpm 
Optical DriveOSMonitorKeyboard
LG 8X Dual-Layer "SuperDrive" OS X Lion 27" iMac screen Mac wireless keyboard 
Mouse
Mac wireless mouse 
CPUGraphicsRAMHard Drive
i7-2600K AMD 6970M 1GB 16GB PC3-10600 DDR3 1TB 7200rpm 
Hard DriveOptical DriveOSMonitor
256GB SSD 8x DL "SuperDrive" OS X 10.7 Lion 27" 2560x1440 iMac display 
Monitor
27" Apple thunderbolt display 
  hide details  
Reply
Foldatron
(17 items)
 
Mat
(10 items)
 
Work iMac
(9 items)
 
CPUMotherboardGraphicsGraphics
i7 950 EVGA x58 3-way SLI EVGA GTX 660ti GTX 275 
RAMHard DriveHard DriveHard Drive
3x2GB Corsair Dominator DDR3-1600 80GB Intel X25-M SSD 2TB WD Black 150GB WD Raptor 
Hard DriveOSMonitorKeyboard
2x 150GB WD V-raptor in RAID0 Win7 Home 64-bit OEM 55" LED 120hz 1080p Vizio MS Natural Ergonomic Keyboard 4000 
PowerCase
750W PC P&C Silencer CoolerMaster 690 
CPUGraphicsRAMHard Drive
Intel Core i5 2500S AMD 6770M 8GB (2x4GB) at 1333Mhz 1TB, 7200 rpm 
Optical DriveOSMonitorKeyboard
LG 8X Dual-Layer "SuperDrive" OS X Lion 27" iMac screen Mac wireless keyboard 
Mouse
Mac wireless mouse 
CPUGraphicsRAMHard Drive
i7-2600K AMD 6970M 1GB 16GB PC3-10600 DDR3 1TB 7200rpm 
Hard DriveOptical DriveOSMonitor
256GB SSD 8x DL "SuperDrive" OS X 10.7 Lion 27" 2560x1440 iMac display 
Monitor
27" Apple thunderbolt display 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Calculate the angle between two vectors (C++)