# Calculate if NPC is inside an oval shape

This topic is 2095 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

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 13
• 18
• 15
• 11
• 9
×

## Important Information

GameDev.net 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!