Java-Gaming.org Hi !
 Featured games (84) games approved by the League of Dukes Games in Showcase (604) Games in Android Showcase (171) games submitted by our members Games in WIP (653) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
Pages: [1]
 ignore  |  Print
 Enemy moving towards player with low friction  (Read 2530 times) 0 Members and 1 Guest are viewing this topic.
TotalSpelNerd

Senior Newbie

Exp: 3 years

 « Posted 2013-07-09 12:40:33 »

So, I've just started working on my AI in my game and I just got stuck with one simple thing, making a enemy move towards me with barely any friction.

To explain it a little bit more I want the enemy move towards the player. If the player moves away from the "attack" of the moving enemy, the enemy will slowly speed down and change direction and aim for the player again.

Kinda similar to the Demon Eye in Terraria.

I do know how to make the enemy move towards me without taking the friction into account.

Hope you understood the question and thanks for answers.

If you need further information or code, just ask.
h3ckboy

JGO Coder

Medals: 5

 « Reply #1 - Posted 2013-07-09 12:50:22 »

Off the top of my head, you could have ddx and ddy values and then change the dx and dy values accordingly.
TotalSpelNerd

Senior Newbie

Exp: 3 years

 « Reply #2 - Posted 2013-07-09 12:54:52 »

What I have right now is kinda like that I think. I have a position (x,y) and two speeds (speedX,speedY). But the problem isn't to implement the friction, it is how to calculate the speed in x and y that the enemy is going to slow down with each frame.
Troncoso

JGO Coder

Medals: 20

 « Reply #3 - Posted 2013-07-09 13:11:10 »

That's less a friction thing and more just acceleration. Simply give your enemy an acceleration variable (the rate it slows down/speeds up) and when your player "moves" just decrease the enemy's velocity by that acceleration value each frame. Velocity being the distance you move the enemy each frame. So to illustrate:

a = 2
v = 10         v = 8     v = 6    v = 8       v = 10       v = 12
---------->-------->------>-------->---------->------------>

You see, for 3 frames, v is lowered by 2, and then it speeds back up. (Of course you'll want to use much smaller numbers)
h3ckboy

JGO Coder

Medals: 5

 « Reply #4 - Posted 2013-07-09 13:17:05 »

Yes, sorry, I wasn't very clear I assumed the notation would make sense. When I say ddx I mean the rate of change of the x velocity (ie the rate of change of x displacement from the origin (0,0))
Troncoso

JGO Coder

Medals: 20

 « Reply #5 - Posted 2013-07-09 13:18:17 »

Yes, sorry, I wasn't very clear I assumed the notation would make sense. When I say ddx I mean the rate of change of the x velocity (ie the rate of change of x displacement from the origin (0,0))

Another name for "the rate of change of velocity" is acceleration.
TotalSpelNerd

Senior Newbie

Exp: 3 years

 « Reply #6 - Posted 2013-07-09 13:19:24 »

okay, thanks. Just one more thing. How can I calculate the acceleration in x and y if I have a set acceleration for the diagonal movement?
Troncoso

JGO Coder

Medals: 20

 « Reply #7 - Posted 2013-07-09 13:24:05 »

There is no "calculating acceleration". It's a constant value of your choosing. To change the acceleration individually, just use a separate variable for x acceleration and y acceleration. Then it's as simple as

enemy.xVelocity += xAcceleration
enemy.yVelocity += yAcceleration
TotalSpelNerd

Senior Newbie

Exp: 3 years

 « Reply #8 - Posted 2013-07-09 13:29:53 »

But how can I get the xAcceleration and yAcceleration so that the enemy will get to the player from both axis at the same time?
h3ckboy

JGO Coder

Medals: 5

 « Reply #9 - Posted 2013-07-09 13:42:26 »

yes there is calculating it.

for example if you want to be on the new course in 2 seconds and you want to have a finaldx of 6, and your startdx is 2

then ddx = (6-2)/2 = 2

then its just
enemy.xVelocity += xAcceleration
enemy.yVelocity += yAcceleration

for 2 seconds.

The reason I phrased ddx how I did was so that you would realize the above on your own, if you view acceleration as the rate of change of velocity then this is very obvious.
TotalSpelNerd

Senior Newbie

Exp: 3 years

 « Reply #10 - Posted 2013-07-09 13:49:22 »

then ddx = (6-2)2 = 2

which 2 is which? Also (6-2)*2 = 8 right?
Troncoso

JGO Coder

Medals: 20

 « Reply #11 - Posted 2013-07-09 14:04:19 »

I'm sure he meant (6-2) / 2 = 2 as he's calculating the distance (6 - 2) and then he's dividing by how many seconds he wants to take to travel that distance. So, it comes out to 2 pixels per second...then again, I feel I'm wrong about what he meant, because that's velocity, not acceleration.

In that case, yes you need to calculate the acceleration, but the original question was nothing more than how to decelerate his enemy, which would simply require choosing an acceleration value that slows down the enemy to get the effect he wants.
TotalSpelNerd

Senior Newbie

Exp: 3 years

 « Reply #12 - Posted 2013-07-09 14:06:20 »

okay, thanks for the help
h3ckboy

JGO Coder

Medals: 5

 « Reply #13 - Posted 2013-07-09 14:27:46 »

its acceleration.

(m/s-m/s)/s = m/s^2 aka acceleration

yeah, sorry it wasn't very clear witht he multiple 2's.... But I hope it all works for you.
Pages: [1]
 ignore  |  Print

You cannot reply to this message, because it is very, very old.

 SHC (27 views) 2015-08-01 03:58:20 Jesse (19 views) 2015-07-29 04:35:27 Riven (40 views) 2015-07-27 16:38:00 Riven (21 views) 2015-07-27 15:35:20 Riven (24 views) 2015-07-27 12:26:13 Riven (14 views) 2015-07-27 12:23:39 BurntPizza (36 views) 2015-07-25 00:14:37 BurntPizza (46 views) 2015-07-24 22:06:39 BurntPizza (30 views) 2015-07-24 06:06:53 NoxInc (37 views) 2015-07-22 22:16:53
 theagentd 52x wessles 49x basil_ 35x KaiHH 26x orangepascal 25x Riven 20x ags1 20x mooman219 17x bornander 15x KudoDEV 13x princec 11x Jesse 11x CelestialCreator 11x klaus 10x pquiring 10x Spasi 8x
 List of Learning Resourcesby gouessej2015-07-09 11:29:36How Do I Expand My Game?by bashfrog2015-06-14 11:34:43List of Learning Resources2015-05-31 05:37:30Intersection Methodsby Roquen2015-05-29 08:19:33List of Learning Resources2015-05-05 10:20:32How to: JGO Wikiby Mac702015-02-17 20:56:162D Dynamic Lighting2015-01-01 20:25:42How do I start Java Game Development?by gouessej2014-12-27 19:41:21
 java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org