Java-Gaming.org    
Featured games (78)
games approved by the League of Dukes
Games in Showcase (426)
Games in Android Showcase (89)
games submitted by our members
Games in WIP (466)
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  
  How to add knockback? :S  (Read 774 times)
0 Members and 1 Guest are viewing this topic.
Offline Delta517

Senior Member


Medals: 2
Projects: 1



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

Hi again! So Im currently trying to figure out how to add knockback to my game, but I'm stuck Sad 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 Sad Help, please? Smiley
Offline opiop65

JGO Kernel


Medals: 128
Projects: 7
Exp: 3 years


Team Alluminum


« Reply #1 - Posted 2013-05-16 20: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.

Offline Andre Lopes
« Reply #2 - Posted 2013-05-18 22:45:47 »

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

http://answers.unity3d.com/questions/242648/force-on-character-controller-knockback.html

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 = (Huh? + Huh - ?? * ?? / Huh??)

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
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Delta517

Senior Member


Medals: 2
Projects: 1



« Reply #3 - Posted 2013-05-21 18: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? Sad 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 Sad 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 Tongue
Offline Jimmt
« League of Dukes »

JGO Kernel


Medals: 121
Projects: 4
Exp: 3 years



« Reply #4 - Posted 2013-05-21 19: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.
Offline Delta517

Senior Member


Medals: 2
Projects: 1



« Reply #5 - Posted 2013-05-21 21: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! Wink

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? Tongue 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 Tongue
Online ags1

JGO Knight


Medals: 29
Projects: 2
Exp: 5 years


Make code not war!


« Reply #6 - Posted 2013-05-21 22: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/

Offline Delta517

Senior Member


Medals: 2
Projects: 1



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

Okay, so I have almost got everything working, except that I have one small problem left Sad I want the enemies to not go through eachother, but I just can't find a solution Sad 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);
         }
Offline kalkitus

Senior Newbie


Medals: 1
Exp: 6 years



« Reply #8 - Posted 2013-05-27 18: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.

Add to update loop:
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.

 

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

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (75 views)
2014-04-15 18:08:23

BurntPizza (68 views)
2014-04-15 03:46:01

UprightPath (80 views)
2014-04-14 17:39:50

UprightPath (65 views)
2014-04-14 17:35:47

Porlus (81 views)
2014-04-14 15:48:38

tom_mai78101 (105 views)
2014-04-10 04:04:31

BurntPizza (165 views)
2014-04-08 23:06:04

tom_mai78101 (261 views)
2014-04-05 13:34:39

trollwarrior1 (210 views)
2014-04-04 12:06:45

CJLetsGame (220 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
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!