Java-Gaming.org Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (762)
Games in Android Showcase (229)
games submitted by our members
Games in WIP (846)
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  
  Changing animation of an AI  (Read 2069 times)
0 Members and 1 Guest are viewing this topic.
Offline JonnyGingerKid

Junior Devvie


Medals: 1
Exp: 3 years



« Posted 2015-04-19 22:19:12 »

Hi, so I'll get right into it. Let's say I have 4 animations, one for each direction, upAnim, downAnim, leftAnim and rightAnim. I have
code that will move my AI sprite towards the player, but how do I tell it which animation should be used? I tried something like

1  
2  
3  
4  
5  
6  
if(vel.x < 0){
    //sprite is moving left
    setAnim(leftAnim);
}else{
    setAnim(rightAnim);
}


However, it get's tricky with all four directions, and the sprite will switch animations rapidly at the wrong times.

What's the best way to get animations to change based on the velocity of the sprite?
Offline chrislo27
« Reply #1 - Posted 2015-04-19 23:11:38 »

It really is all a matter of if statements. However, if the sprite is moving diagonally (e.g.: up and left), you need to choose if it should show the up animation or the left animation.

I'm also assuming you don't want it to show the animation if the entity isn't moving, so you would need another if statement to check if the entity is even moving.

Example code:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
if(vel.x != 0){
    if(vel.x < 0){
        // sprite is moving left
        setAnim(leftAnim);
    }else{
        // sprite is moving right
        setAnim(rightAnim);
    }
}

// the y-check statements are placed after the x-check so moving up/down has "priority" over left/right
// if you want it the other way around, place the y-checks before the x-checks
if(vel.y != 0){
    if(vel.y < 0){
        // sprite is moving up
        setAnim(leftAnim);
    }else{
        // down
        setAnim(rightAnim);
    }
}
Offline Jesse

JGO Coder


Medals: 23



« Reply #2 - Posted 2015-04-20 05:46:37 »

In addition to chrislo27's suggestion, here's another approach you could try. In short, if the entity is moving 'more' up or down than laterally (as determined by the absolute values of the velocity components) you'd use an up or down animation, else you'd use a lateral animation. Pseudocode (may be errors):

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
// If exact comparisons to zero aren't suitable for numerical reasons, or if you
// know a priori when the entity isn't moving, you can use some other method here
// for determining that the entity is stationary.
if (vel.x == 0 && vel.y == 0) {
    animation = "stationary";
} else if (abs(vel.x) > abs(vel.y)) {
    animation = (vel.x < 0) ? "left" : "right";
} else {
    // You might need to invert this, depending on your coordinate system.
    animation = (vel.y < 0) : "down" : "up";
}

If the entity is moving more or less diagonally, you might find that it switches back and forth between two animations frequently in a way that may not be desirable. This can be addressed by applying some hysteresis (this would make the code a little more complex though).
Pages: [1]
  ignore  |  Print  
 
 

 
EgonOlsen (360 views)
2018-06-10 19:43:48

EgonOlsen (362 views)
2018-06-10 19:43:44

EgonOlsen (299 views)
2018-06-10 19:43:20

DesertCoockie (534 views)
2018-05-13 18:23:11

nelsongames (864 views)
2018-04-24 18:15:36

nelsongames (846 views)
2018-04-24 18:14:32

ivj94 (1293 views)
2018-03-24 14:47:39

ivj94 (416 views)
2018-03-24 14:46:31

ivj94 (1079 views)
2018-03-24 14:43:53

Solater (428 views)
2018-03-17 05:04:08
Java Gaming Resources
by philfrei
2017-12-05 19:38:37

Java Gaming Resources
by philfrei
2017-12-05 19:37:39

Java Gaming Resources
by philfrei
2017-12-05 19:36:10

Java Gaming Resources
by philfrei
2017-12-05 19:33:10

List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05
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!