Author Topic: Calculate angle between camera (w/ viewing direction) and sprite  (Read 1171 times)

Offline andrewjbaker

  • Level 17
  • *
  • Posts: 154
  • Reputation: +2/-0
    • View Profile
    • Fleeting Fantasy
Calculate angle between camera (w/ viewing direction) and sprite
« on: February 28, 2011, 06:09:48 PM »
I know this is a mathematics question but I wasn't quite sure where to post... so here it is.

I'm trying to correct my sprite rendering code for my HTML5 canvas 2.5D landscape renderer.

I need to determine the angle between the camera (the player's viewpoint) and any sprites that might potentially appear within view.

This quick and dirty xfig diagram might help to explain what I need to find (the angle a):



Given that I know the angle of the player's (camera) viewing direction and the x and z values of both the camera and each sprite, how do I calculate angle a?

Maths was never my strong point; somewhat surprised I've got as far as I have. :'(
Currently working on an HTML5 canvas 2.5D landscape renderer and a PBBG that uses it (http://fleetingfantasy.com/). The development blog's at http://fleetingfantasy.wordpress.com/.
What are BBGameZone members working on? See the game list.
irc.freenode.net, #bbg

Offline Chris

  • Game Owner
  • Level 35
  • *
  • Posts: 2,217
  • Reputation: +28/-1
    • View Profile
Re: Calculate angle between camera (w/ viewing direction) and sprite
« Reply #1 on: February 28, 2011, 06:33:05 PM »
I don't remember, but I remember it was easy :D Maybe if it was morning I could even figure it out. Anyway, the solution was connected to triangles (like almost every problem in 3D, except matrices that's it :D)

You could try these to fuel your imagination:
http://en.wikipedia.org/wiki/Triangle
http://easycalculation.com/trigonometry/triangle-angles.php

Offline andrewjbaker

  • Level 17
  • *
  • Posts: 154
  • Reputation: +2/-0
    • View Profile
    • Fleeting Fantasy
Re: Calculate angle between camera (w/ viewing direction) and sprite
« Reply #2 on: March 01, 2011, 04:17:18 AM »
Got a reply on Twitter from Paul King (@nrocy).

Paul's demo. is available at http://nrocy.com/zombiez/.

Gonna' incorporate this evening...
Currently working on an HTML5 canvas 2.5D landscape renderer and a PBBG that uses it (http://fleetingfantasy.com/). The development blog's at http://fleetingfantasy.wordpress.com/.
What are BBGameZone members working on? See the game list.
irc.freenode.net, #bbg

Offline andrewjbaker

  • Level 17
  • *
  • Posts: 154
  • Reputation: +2/-0
    • View Profile
    • Fleeting Fantasy
Re: Calculate angle between camera (w/ viewing direction) and sprite
« Reply #3 on: March 02, 2011, 09:35:32 AM »
OK, I'm almost there...

For those interested in how to calculate the angle... http://www.euclideanspace.com/maths/algebra/vectors/angleBetween/index.htm
Currently working on an HTML5 canvas 2.5D landscape renderer and a PBBG that uses it (http://fleetingfantasy.com/). The development blog's at http://fleetingfantasy.wordpress.com/.
What are BBGameZone members working on? See the game list.
irc.freenode.net, #bbg

Offline DV8

  • Level 10
  • *
  • Posts: 63
  • Reputation: +0/-0
    • View Profile
    • Shadowrun: Corrosion
Re: Calculate angle between camera (w/ viewing direction) and sprite
« Reply #4 on: March 02, 2011, 10:12:07 AM »

 


SimplePortal 2.3.3 © 2008-2010, SimplePortal