aWeirdo Posted October 22, 2016 Share Posted October 22, 2016 Hi all, I know it isn't really babylon related, but there are a lot of smart guys around so i believe it to be worth a try and ask I have been working on & testing some area calculations, mainly for finding meshes within a certain select area, think "area of effect" game abilities. Examples; For a square area, I simply use two vector3's, one containing minX, minZ and the other one containing maxX and maxZ, thus creating a virtual square which you can use to check if any meshes are found within it. Resulting in a very lightweight & simple solution. For a circular area, I use a starting middel point of the circle (x & z position), and a pre-set radius/distance value and then i calculate & check distance to meshes and select all meshes which are valid and in-range, But now to the issue at hand, Finding a cone area; Basicly, a small select part of a circle area, calculated depending on a player's rotation.y, a pre-set radius/distance value and a pre-set "width" value. Visualization; So in the above visualization, if a mesh was found within the cone area shown in the "width" illustration, it should be a hit, but if it was outside the defined area, it should be ignored, even if the mesh is within the circles radius.. How would you go about doing this? I have setup a PG containing the basic circular calculations, (open browser console for code feeback). http://www.babylonjs-playground.com/#2KMFW6#5 I appreciate any help & ideas.. Cheers. Quote Link to comment Share on other sites More sharing options...
JohnK Posted October 23, 2016 Share Posted October 23, 2016 When I get a chance I will create a PG to test my idea of checking if point is inside cone (actually being pedantic I think you mean sector as you seem to be working with a circle not a sphere). Am attaching a method for checking if a point is inside the minor sector of a circle given that it is already inside the circle. Inside a Sector.pdf If you are working inside a sphere and you do mean a cone then life gets much more complicated. EDIT pdf file corrected to give a calculation that works. Quote Link to comment Share on other sites More sharing options...
aWeirdo Posted October 23, 2016 Author Share Posted October 23, 2016 Hi @JohnK Thanks for the reply, and the pdf, nice read. If you look at my PG, the sphere is actually just the mesh i'm trying to select. (check console for logs) The idea of it is to have a basic function to call and handle all targeting of cone'd attack "abilities". I found this code online; https://jsfiddle.net/5c5uvav3/1/ But it is using start and end "sector's" so i'm struggling how to edit it into using an angle to determine the cone size instead.(i know i wrote "width" in my post above, but it seemed simpler to use angles) So basicly; The cone's direction around the "caster's" position should be based on the caster's rotation.y, and then if lets say the cone should be 15* Degrees wide (about 0.261 rotation units if my math is right), out of the circles 360* Degrees, the cone should be centered on the rotation.y (e.g. offset by 7.5* Degrees) I hope i make sense Edit; Updated PG, it still only contains circular calculations, but i added a box to represent the point of which the cone is "fired" from. Quote Link to comment Share on other sites More sharing options...
adam Posted October 23, 2016 Share Posted October 23, 2016 I would get the dot product of the normalized cone vector to the normalized vector between the cone and the other object. If you get .5 or below then it is 45 degrees or below (.333 == 30 degrees, .1 == 9 degrees). Only do that though if the objects are close enough to hit. Quote Link to comment Share on other sites More sharing options...
JohnK Posted October 23, 2016 Share Posted October 23, 2016 Quote But it is using start and end "sector's" so i'm struggling how to edit it into using an angle to determine the cone size instead. Knowing the start and end lines will give you start and end vectors in the direction of these lines from the centre of the circle. You can find the angle from their dot product and as they are both in xz plane the sign of the y component of their cross product will tell you which is the start line and which is the end line. Quote Link to comment Share on other sites More sharing options...
JohnK Posted October 25, 2016 Share Posted October 25, 2016 @aWeirdo glad to see you solved your problem. I couldn't stop playing with the 2D version and found that my original calculation only worked in some cases and so carried on playing around. Now there is a version that will check to see if the given point is inside a sector in 2D. http://www.babylonjs-playground.com/#Z3FUC#2 aWeirdo 1 Quote Link to comment Share on other sites More sharing options...
aWeirdo Posted October 25, 2016 Author Share Posted October 25, 2016 Hi @JohnK & @adam Thank you both for your suggestions. I completely forgot to update the PG, when i marked the topic as solved Here's my working sample http://www.babylonjs-playground.com/#2KMFW6#12 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.