Java-Gaming.org Hi !
 Featured games (90) games approved by the League of Dukes Games in Showcase (753) Games in Android Showcase (228) games submitted by our members Games in WIP (842) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
Pages: [1]
 ignore  |  Print
 How to add knockback? :S  (Read 3464 times) 0 Members and 1 Guest are viewing this topic.
Delta517

Senior Devvie

Medals: 4
Projects: 2

 « Posted 2013-05-16 17:08:40 »

Hi again! So Im currently trying to figure out how to add knockback to my game, but I'm stuck I searched around a bit, but the only thing I could find was this:

 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19  20  21 `  public void knockback(float r, float force){      r = r - 180;            force *= knockbackResistance; // how heavy the entity is (so lesser knock back)            knockbackTimer = force * 8;      knockbackTo = new Vector2((force * (float) MathUtils.sin(r * MathUtils.degreesToRadians)),                         (-force * (float) MathUtils.cos(r * MathUtils.degreesToRadians)))                                       .add(position);                  }//Then I have this on the update loop for the entity   public void update(){      if(knockbackTimer > 0){         position = Tools.lerp(position, knockbackTo, 0.2f);         knockbackTimer -= Gdx.graphics.getDeltaTime() * 1000;      }        }`

To be honest, I really don't understand it Help, please?
Opiop
 « Reply #1 - Posted 2013-05-16 18:41:40 »

Ok first off, format your code, I don't feel like trying to read that mess. Second, just think about the problem and how you can solve it. Is your game a side scroller? If so, you'll need to know a little trig to add a parabola shaped knock back flight path. You need to then just move the entity along the path. It really isn't that hard and I think if you can't figure this out by yourself, then you need to think a little harder on it. Programming is all about problem solving. Not re-using old code from other people.
Andre Lopes
 « Reply #2 - Posted 2013-05-18 20:45:47 »

Hey m8, well if you are using 2D, maybe you need some Newton laws?

As far as i know, all you really need to know is this :

ยบ REACH THE QUANTITY OF PIXELS YOU NEED TO MOVE BACK ( FORCE )

So you need reach something like this :

int force = (? + - ?? * ?? / ??)

Just a little example.

Anyway, to reach that, you will need physics, which im not good at it. I got a good physics teacher in college, if you are in college maybe you should ask him ?

Well, maybe this post can help a little.

xD
Delta517

Senior Devvie

Medals: 4
Projects: 2

 « Reply #3 - Posted 2013-05-21 16:54:32 »

Ok, so I have been able to make the knockback work for when the player hits a enemy, but how do I make the collision for when a enemy hit another enemy work? I tried doing this:

 1  2  3  4  5 `public void checkCollisionBetweenEnemies(Rectangle rectEnemy){      if (rectEnemy.intersects(rectEnemy)){         System.out.println("Collision between enemies!");         }      }`

But that makes it think that it is always a collision It always displays the message since its always hitting it self :/ Note: Im using a superclass called Enemy and I currently have one subclass which is called Zombie
Jimmt
« League of Dukes »

JGO Kernel

Medals: 167
Projects: 5
Exp: 6 years

 « Reply #4 - Posted 2013-05-21 17:14:39 »

You're checking if it intersects itself, so obviously it always will be true...try something like
 1  2  3  4  5 `public void checkCollisionBetweenEnemies(Enemy a, Enemy b){     if(a.getRectangle().intersects(b.getRectangle()){     System.out.println("Collision");   }}`

in your collision manager, and then modify/use as needed.
Delta517

Senior Devvie

Medals: 4
Projects: 2

 « Reply #5 - Posted 2013-05-21 19:44:45 »

Ahh, thanks! Got it working now! ^^

Since I'm still a newbie at this, I feel it's important to learn what I actually did, so here's another question for you Java experts out there!

Here is the code for my entity controller class:

 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19  20  21  22 `public void tick(){      for(int i = 0; i < entityList.size(); i++){         tempEntity = entityList.get(i);                  tempEntity.tick();                  if(entityList.size() >= 2){            for(int j = 0; j < entityList.size(); j++){                              tempEntity2 = entityList.get(j);                              if(tempEntity != tempEntity2){                                    if (tempEntity.getRectEnemy().intersects(tempEntity2.getRectEnemy())){                                          System.out.println("Collision between enemies!");                  }               }            }         }      }   }`

My question is that to get this working I had to go the Enemy superclass and remove the "static" modifier that was on my rectangle around my enemy (don't know what it was doing there in the first place :S ), but what did I really do? If I remember correctly, static is used when you have to use something in a class you haven't initialised yet, but I'm sure you use it for different stuff
ags1

JGO Kernel

Medals: 363
Projects: 7

Make code not war!

 « Reply #6 - Posted 2013-05-21 20:47:33 »

static means that the method or variable applies to the whole class and not to an individual instances - all instances of the class share the same value. So in your example, when the rect was static entity 1 and entity 2 had the same rect value, because it was static.

It might be helpful for you to work through the first parts of the Java tutorial (which covers the fundamentals of the language):

http://docs.oracle.com/javase/tutorial/

Delta517

Senior Devvie

Medals: 4
Projects: 2

 « Reply #7 - Posted 2013-05-27 14:37:52 »

Okay, so I have almost got everything working, except that I have one small problem left I want the enemies to not go through eachother, but I just can't find a solution I tried this code:
Note: I'm using it on the player for testing purposes!

 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19 `if(player.isMovingRight() == true){            player.setX(x - 22 + speedX);         }else if(player.isMovingLeft() == true){            player.setX(x + 22 - speedX);         }                  if(player.isMovingDown() == true){            player.setY(y - 22 + speedY);         }else if(player.isMovingUp() == true){            player.setY(y + 22 - speedY);         }                  if(player.getSpeedX() == 0 && player.getSpeedY() == 0){            if(speedX > 0 && speedX < 3) player.setX(x + 22 - speedX);            if(speedX < 0 && speedX > -3) player.setX(x - 22 + speedX);            if(speedY > 0 && speedY < 3) player.setY(y + 22 - speedY);            if(speedY < 0 && speedY > -3) player.setY(y - 22 + speedY);         }`
kalkitus

Junior Devvie

Medals: 2
Exp: 6 years

 « Reply #8 - Posted 2013-05-27 16:07:23 »

Hey that's my weird knockback code!

When there are a lot of enemies, this solution makes them all shake and do funky stuff, but here it is anyway to get you going somewhere.

 1  2  3  4  5 `if(entityRect.intersects(otherEntityRect)){      Vector2f translatedDifference = Tools.calcMinTranslationDistance(entityRect, otherEntityRect);   entity.addPosition(translatedDifference.x, translatedDifference.y);}`

I didn't write all this myself and I don't remember who exactly did (sorry). It basically just gives you the distance to stop two rectangles from overlapping:
 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36  37  38  39  40  41  42  43  44  45  46  47 `public static Vector2f calcMinTranslationDistance(Rectangle rect1, Rectangle rect2){      float difference;   float minTranslateDistance;   short axis;   short side;   Vector2f translatedDifference = new Vector2f(0,0);      // Left   difference = (rect1.getX() + rect1.getWidth()) - (rect2.getX());   minTranslateDistance = difference;   axis = 0;   side = -1;      // Right   difference = (rect2.getX() + rect2.getWidth()) - rect1.getX();   if(difference < minTranslateDistance){      minTranslateDistance = difference;      axis = 0;      side = 1;   }      // Down   difference = (rect1.getY() + rect1.getHeight()) - rect2.getY();   if(difference < minTranslateDistance){      minTranslateDistance = difference;      axis = 1;      side = -1;   }      // Up   difference = (rect2.getY() + rect2.getHeight()) - rect1.getY();   if(difference < minTranslateDistance){      minTranslateDistance = difference;      axis = 1;      side = 1;   }      // Y   if(axis == 1)         translatedDifference.y = side * minTranslateDistance;   // X   else      translatedDifference.x = side * minTranslateDistance;      return translatedDifference;}`

Pages: [1]
 ignore  |  Print

 ivj94 (586 views) 2018-03-24 14:47:39 ivj94 (49 views) 2018-03-24 14:46:31 ivj94 (383 views) 2018-03-24 14:43:53 Solater (63 views) 2018-03-17 05:04:08 nelsongames (110 views) 2018-03-05 17:56:34 Gornova (159 views) 2018-03-02 22:15:33 buddyBro (704 views) 2018-02-28 16:59:18 buddyBro (93 views) 2018-02-28 16:45:17 xxMrPHDxx (494 views) 2017-12-31 17:17:51 xxMrPHDxx (734 views) 2017-12-31 17:15:51
 KaiHH 13x SHC 10x ByerN 10x NuclearPixels 10x Zemlaynin 10x Guerra2442 6x Damocles 6x VaTTeRGeR 5x Spasi 5x ags1 4x orangepascal 4x philfrei 4x mesterh 3x princec 3x ndnwarrior15 3x Springrbua 2x
 Java Gaming Resourcesby philfrei2017-12-05 19:38:37Java Gaming Resourcesby philfrei2017-12-05 19:37:39Java Gaming Resourcesby philfrei2017-12-05 19:36:10Java Gaming Resourcesby philfrei2017-12-05 19:33:10List of Learning Resourcesby elect2017-03-13 14:05:44List of Learning Resourcesby elect2017-03-13 14:04:45SF/X Librariesby philfrei2017-03-02 08:45:19SF/X Librariesby philfrei2017-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