Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Newcomers AppGameKit Corner / [3D] Get Angle between two objects (or two points)

Author
Message
Blendman
10
Years of Service
User Offline
Joined: 17th Feb 2014
Location: Arkeos
Posted: 20th Nov 2015 09:08
Hi

I know how to get angle betwen two points in 2D, with :


But I don't know how I can get the angle between two "points" (x,y,z) in 3D.
Someone would have a function to get the angle ?

I would like to send a bullet in direction of a monster in my game ^^.

Thanks a lot.
http://www.dracaena-studio.com
29 games
18
Years of Service
User Offline
Joined: 23rd Nov 2005
Location: not entirely sure
Posted: 20th Nov 2015 09:22
You don't need to calculate the angle.

Just use the SetObjectLookAt command to point the bullet at the monster and then move the bullet along its local Z axis using MoveObjectLocalZ.
Invaders of the 29th Dimension - available now on Google Play
Find me on indieDB
Van B
Moderator
22
Years of Service
User Offline
Joined: 8th Oct 2002
Location: Sunnyvale
Posted: 20th Nov 2015 09:58
You could use vectors as well, which would be pretty much the same as what 29 suggests, except you could affect the velocities for some nice effects.

If you calculate the difference between the points, say DX#, DY# and DZ# - you can work out the distance of this vector...

D#=sqrt((DX#*DX#)+(DY#*DY#)+(DZ#*DZ#))

Then use that to normalize the vector...

DX#=DX#/D#
DY#=DY#/D#
DZ#=DZ#/D#

And then DX#,DY# and DZ# hold the velocity for your bullet, add these to the bullet position but multiply them by the desired velocity - the equivalent of how much the object should move. This method would allow you to apply gravity to the bullet, by reducing the DY# variable - would also let you deflect shots off walls, when doing a polygon line intersect check for example, between the 2 points you could work out the normal vectors and have the bullet deflect. If you recalculate the vector each frame and change the bullet velocity then you have a deadly homing missile, if you change the velocity gradually then you have winding homing missile that'll circle around targets etc like in the Worms games. There's some fun to be had with vectors!

No matter if you go for vectors or pointing the object and moving, it's worth storing that previous location and relying on intersection collision checks - it's a nice easy way to get accurate bullet collision.

Login to post a reply

Server time is: 2024-11-21 20:54:53
Your offset time is: 2024-11-21 20:54:53