Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (488)
Games in Android Showcase (112)
games submitted by our members
Games in WIP (553)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Enemy moving towards player with low friction  (Read 1444 times)
0 Members and 1 Guest are viewing this topic.
Offline 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.
Offline 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.
Offline 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.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline 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)
Offline 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))
Offline 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.
Offline 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?
Offline 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
Offline 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?
Offline 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.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline 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?
Offline 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.
Offline TotalSpelNerd

Senior Newbie


Exp: 3 years



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

okay, thanks for the help Cheesy
Offline 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.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

TehJavaDev (17 views)
2014-08-28 18:26:30

CopyableCougar4 (26 views)
2014-08-22 19:31:30

atombrot (39 views)
2014-08-19 09:29:53

Tekkerue (36 views)
2014-08-16 06:45:27

Tekkerue (33 views)
2014-08-16 06:22:17

Tekkerue (22 views)
2014-08-16 06:20:21

Tekkerue (33 views)
2014-08-16 06:12:11

Rayexar (68 views)
2014-08-11 02:49:23

BurntPizza (45 views)
2014-08-09 21:09:32

BurntPizza (36 views)
2014-08-08 02:01:56
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!