Java-Gaming.org Hi !
 Featured games (84) games approved by the League of Dukes Games in Showcase (595) Games in Android Showcase (168) games submitted by our members Games in WIP (646) 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 1451 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?
opiop65
 « 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: 161
Projects: 5
Exp: 3 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 Wizard

Medals: 108
Projects: 3
Exp: 5 years

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

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

 deepthought (47 views) 2015-06-30 15:39:44 deepthought (50 views) 2015-06-30 15:39:09 deepthought (62 views) 2015-06-30 15:36:52 Za\'Anzabar (36 views) 2015-06-29 05:44:54 TritonDreyja (46 views) 2015-06-24 17:10:40 CopyableCougar4 (46 views) 2015-06-23 00:34:45 BurntPizza (51 views) 2015-06-21 20:36:46 cookiecompiler (93 views) 2015-06-11 15:42:53 cookiecompiler (55 views) 2015-06-11 15:41:14 NegativeZero (80 views) 2015-06-11 09:49:18
 princec 30x wessles 22x BurntPizza 19x CopyableCougar4 19x opiop65 17x Riven 15x nsigma 15x EgonOlsen 14x ags1 14x theagentd 12x KaiHH 12x SauronWatchesYou 11x KevinWorkman 11x Archive 11x sunburn 11x NegativeZero 10x
 How 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:21Resources for WIP gamesby kpars2014-12-18 10:26:14
 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