Java-Gaming.org Hi !
 Featured games (90) games approved by the League of Dukes Games in Showcase (753) Games in Android Showcase (228) games submitted by our members Games in WIP (842) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
Pages: [1]
 ignore  |  Print
 Determining if a circle is in your field of view in 2D "without" trig functions.  (Read 5538 times) 0 Members and 1 Guest are viewing this topic.
ClickerMonkey

JGO Coder

Medals: 26
Exp: 10 years

Game Engineer

 « Posted 2013-06-28 13:27:38 »

http://pastebin.com/yeESVkhZ

Given your direction vector is normalized, and you store FOV as cos/sin, you can pretty quickly determine if a circle is within your view (either partially or entirely).

This works by creating vectors that extend from the origin to the sides of their view port - rotating them by the view's direction. Using these vectors it can rotate the vector between the circle's center and the object down to the x-axis. Once you do this, the y-component of that vector is the signed distance of the circle to that extent.

This also works where FOV >= 90 (and by FOV, I'm talking half of your actual field of view, in radians).

This is some code from a library I'm working on called [Steerio](https://github.com/ClickerMonkey/steerio).

If someone can figure out a more efficient method, I challenge thee!

Peace!

RobinB

JGO Ninja

Medals: 45
Projects: 1
Exp: 3 years

Spacegame in progress

 « Reply #1 - Posted 2013-06-28 15:24:14 »

Why is this function better then:

circle.x - circle.width/2 <= rect.x + rect.width && circle.x + circle.width >= rect.x (same with y)
ClickerMonkey

JGO Coder

Medals: 26
Exp: 10 years

Game Engineer

 « Reply #2 - Posted 2013-06-28 15:31:39 »

By "your" FOV, I mean something in your game like a unit.

Imagine that on the head of a unit of yours, and it want's to know everything it can see based on it's direction, location, and FOV.

RobinB

JGO Ninja

Medals: 45
Projects: 1
Exp: 3 years

Spacegame in progress

 « Reply #3 - Posted 2013-06-28 15:36:28 »

Oh awesome i was looking for that a while ago!
Pages: [1]
 ignore  |  Print

 ivj94 (584 views) 2018-03-24 14:47:39 ivj94 (48 views) 2018-03-24 14:46:31 ivj94 (382 views) 2018-03-24 14:43:53 Solater (62 views) 2018-03-17 05:04:08 nelsongames (109 views) 2018-03-05 17:56:34 Gornova (159 views) 2018-03-02 22:15:33 buddyBro (703 views) 2018-02-28 16:59:18 buddyBro (92 views) 2018-02-28 16:45:17 xxMrPHDxx (493 views) 2017-12-31 17:17:51 xxMrPHDxx (733 views) 2017-12-31 17:15:51
 Java Gaming Resourcesby philfrei2017-12-05 19:38:37Java Gaming Resourcesby philfrei2017-12-05 19:37:39Java Gaming Resourcesby philfrei2017-12-05 19:36:10Java Gaming Resourcesby philfrei2017-12-05 19:33:10List of Learning Resourcesby elect2017-03-13 14:05:44List of Learning Resourcesby elect2017-03-13 14:04:45SF/X Librariesby philfrei2017-03-02 08:45:19SF/X Librariesby philfrei2017-03-02 08:44:05
 java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org