00:00
00:00
Newgrounds Background Image Theme

LOCKdev just joined the crew!

We need you on the team, too.

Support Newgrounds and get tons of perks for just $2.99!

Create a Free Account and then..

Become a Supporter!

If enemy sees hero, then fire.(AI)

530 Views | 12 Replies
New Topic Respond to this Topic

Hey guys, I have an enemy ai question.

If(hero is between goblins x pos and visual pos(where he can't see))
Then fire

If not then idle

I want it so the enemy fires when the hero comes close to the goblin.

now i can do this

if(hero.hitTestPoint(goblin,x + 100, goblin.y, true)
//fire

But then if the player is in the middle and not touching the x pos, when I have failed. What do you guys reckon, any tips :D?!

Response to If enemy sees hero, then fire.(AI) 2014-04-11 23:09:02


At 4/11/14 10:59 PM, moynzy wrote: But then if the player is in the middle and not touching the x pos, when I have failed. What do you guys reckon, any tips :D?!

Stop using hitTestPoint and start using math.

If(hero is between goblins x pos and visual pos(where he can't see))

Turn what you just said into a math equation. It helps to draw it on paper for a visualization.

Response to If enemy sees hero, then fire.(AI) 2014-04-11 23:28:24


At 4/11/14 11:09 PM, MSGhero wrote: Stop using hitTestPoint and start using math.

WWPD

What would Pythagoras do?

Response to If enemy sees hero, then fire.(AI) 2014-04-12 00:09:34


At 4/11/14 11:09 PM, MSGhero wrote:
At 4/11/14 10:59 PM, moynzy wrote: But then if the player is in the middle and not touching the x pos, when I have failed. What do you guys reckon, any tips :D?!
Stop using hitTestPoint and start using math.

If(hero is between goblins x pos and visual pos(where he can't see))
Turn what you just said into a math equation. It helps to draw it on paper for a visualization.

Gradients and Normals?
Quadratic Roots?
Curves?
Vector Regions?

I know Maths and Programming goes well together, I'm not a mathematician, but I'm willing to learn in order to develop and create better programs, so please do enlighten me :D!

Response to If enemy sees hero, then fire.(AI) 2014-04-12 00:25:18


At 4/12/14 12:09 AM, moynzy wrote: I know Maths and Programming goes well together, I'm not a mathematician, but I'm willing to learn in order to develop and create better programs, so please do enlighten me :D!

You don't need quadratic roots to figure out if a number is between 2 other numbers. This is addition and subtraction, inequalities and equations. "If the hero's x is between the enemy's x and the enemy's x plus his vision range, then attack" is pretty much what you said.

Response to If enemy sees hero, then fire.(AI) 2014-04-12 10:08:38


At 4/12/14 12:25 AM, MSGhero wrote:
At 4/12/14 12:09 AM, moynzy wrote: I know Maths and Programming goes well together, I'm not a mathematician, but I'm willing to learn in order to develop and create better programs, so please do enlighten me :D!
You don't need quadratic roots to figure out if a number is between 2 other numbers. This is addition and subtraction, inequalities and equations. "If the hero's x is between the enemy's x and the enemy's x plus his vision range, then attack" is pretty much what you said.

if (_character.x > redGoblin.x && _character.x < redGoblin.x - 300)

I'm working with a different co ordination, not the stages, so I'm getting confused.

But I've tested this and no result/ trace.

Response to If enemy sees hero, then fire.(AI) 2014-04-12 12:59:38


At 4/12/14 10:08 AM, moynzy wrote: if (_character.x > redGoblin.x && _character.x < redGoblin.x - 300)

These two things can't be true at the same time. Like I said, draw it out. If your hero is at x=200 and the enemy at x=100, is the hero within vision range and why or why not?

Response to If enemy sees hero, then fire.(AI) 2014-04-12 20:02:22


Direction/magnitude independant :

var dist:Number = Math.sqrt(Math.pow(x2-x1,2)+Math.pow(y2-y1,2));
if (dist <= 100) {
    // attack
}

Response to If enemy sees hero, then fire.(AI) 2014-04-12 21:34:12


At 4/12/14 08:02 PM, slugrail wrote: var dist:Number = Math.sqrt(Math.pow(x2-x1,2)+Math.pow(y2-y1,2));
if (dist <= 100) {
// attack
}

slightly faster, since Math.pow is incredibly slow.

var dist:Number = Math.sqrt((x2*x2-x1*x1) + (y2*y2-y1*y1));
if (dist <= 100) {
    // attack
}

Programming stuffs (tutorials and extras)

PM me (instead of MintPaw) if you're confuzzled.

thank Skaren for the sig :P

BBS Signature

Response to If enemy sees hero, then fire.(AI) 2014-04-13 05:23:15


At 4/12/14 09:34 PM, egg82 wrote: slightly faster, since Math.pow is incredibly slow.

var dist:Number = Math.sqrt((x2*x2-x1*x1) + (y2*y2-y1*y1));
if (dist <= 100) {
// attack
}

Faster still, eliminate the expensive call to Math.sqrt:

var dist:Number = (x2*x2-x1*x1) + (y2*y2-y1*y1);
if (dist <= 100 * 100) {
    // attack
}

Response to If enemy sees hero, then fire.(AI) 2014-04-13 07:46:22


At 4/13/14 05:23 AM, Sandremss128 wrote: Faster still, eliminate the expensive call to Math.sqrt:

var dist:Number = (x2*x2-x1*x1) + (y2*y2-y1*y1);
if (dist <= 100 * 100) {
// attack
}

So it's turning into an optimization battle huh? Hardcoding:

var dist:Number = (x2*x2-x1*x1) + (y2*y2-y1*y1);
 if (dist <= 10000) {
     // attack
 }

Or better yet:

if (false <= true) {
  // attack
}

Response to If enemy sees hero, then fire.(AI) 2014-04-13 09:39:26


At 4/13/14 07:46 AM, slugrail wrote: So it's turning into an optimization battle huh? Hardcoding:

var dist:Number = (x2*x2-x1*x1) + (y2*y2-y1*y1);
if (dist <= 10000) {
// attack
}

If the compiler is worth 2 cents it will automatically hardcode that for you.

Response to If enemy sees hero, then fire.(AI) 2014-04-13 11:59:54


At 4/13/14 07:46 AM, slugrail wrote: if (false <= true) {
// attack
}

NEXT LEVEL

// attack