Advertisement Jump to content
Sign in to follow this  

Calculate if NPC is inside an oval shape

This topic is 1824 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts



I have drawn a circle but converted it to an oval shape in my HTML5 canvas like this:;
ctx.arc(x, y, radius , 0, 2 * Math.PI, false);
ctx.lineWidth = 1;
ctx.strokeStyle = 'rgba(255,255,255,0.7)';

Now in a separate function i have some calculations to set the location of my NPC. 


First i get the location of the oval center and the NPC position and get the distance like this:


var vectorX = NPC.X - locationX,
    vectorY = NPC.Y - locationY,
    distance = Math.sqrt(vectorX*vectorX + vectorY*vectorY);

if(distance < raidus){
   alert('You have entered the oval');

The problem lies with the if statement, because its an oval i don't totally know how i check if the NPC has entered accurately, its confusing me because its not a simple circle.

Edited by thefollower

Share this post

Link to post
Share on other sites

Try dividing or multiplying vectorX by 1.5 (I am not sure what the semantics of `scale' are).




You don't need to take square roots to make that comparison: Compare the square of the distance instead:


var vectorX = (NPC.X - locationX) / 1.5, # or multiplied by 1.5
vectorY = NPC.Y - locationY,
distance_squared = vectorX*vectorX + vectorY*vectorY;

if(distance_squared < radius*radius){
  alert('You have entered the oval');

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!