# 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.

## Recommended Posts

Hello,

I have drawn a circle but converted it to an oval shape in my HTML5 canvas like this:

ctx.save();
ctx.scale(1.5,1);
ctx.beginPath();
ctx.arc(x, y, radius , 0, 2 * Math.PI, false);
ctx.restore();
ctx.lineWidth = 1;
ctx.strokeStyle = 'rgba(255,255,255,0.7)';
ctx.stroke();


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){
}


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 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;

}
`

##### Share on other sites

I think alvaro is right.

http://www.mathopenref.com/coordgeneralellipse.html

this means your inequality should be : (assuming that your circle started as a unit circle)

((x-x0)^2 / 1.5^2) + (y-y0)^2 < 1

if you had a different radius, the inequality is like this :

((x-x0)^2 / (r * 1.5)^2) + (y-y0)^2 / r^2 < 1

Edited by Doublefris

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 17
• 14
• 10
• 9
• 11
• ### Forum Statistics

• Total Topics
634097
• Total Posts
3015505
×