Java-Gaming.org Hi !
 Featured games (84) games approved by the League of Dukes Games in Showcase (576) Games in Android Showcase (155) games submitted by our members Games in WIP (626) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
Pages: [1]
 ignore  |  Print
 Entity knockback  (Read 1220 times) 0 Members and 1 Guest are viewing this topic.
Mayhaw

Junior Newbie

The Bugger

 « Posted 2013-04-20 17:54:34 »

Hi, can someone please tell me how to add knock-back? My entities have x,y,velocityX and velocityY variables, and i have just no idea how to add it. I can like add 5 to velocityX for example, but how do i decrease it? And if the entity has bumped into something and it's velocity is set to 0, how do i make it so that it stops decreasing?Maybe add separate fields, but that destroys the purpose of one velocity vector, doesn't it? Please help me :c

PS: Using Artemis.
kalkitus

Junior Devvie

Medals: 2
Exp: 6 years

 « Reply #1 - Posted 2013-04-20 19:34:53 »

It's probably a bad way of doing it, but this is my code for knockback to give you an idea

 1  2  3  4  5  6  7  8  9  10  11 `   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

 1  2  3  4  5  6 `   public void update(){      if(knockbackTimer > 0){         position = Tools.lerp(position, knockbackTo, 0.2f);         knockbackTimer -= Gdx.graphics.getDeltaTime() * 1000;      }        }`
Mayhaw

Junior Newbie

The Bugger

 « Reply #2 - Posted 2013-04-21 09:11:53 »

Whoa i didn't expect code! Thanks  ! I think i will add knockbackTimer an knockbackTo to the Velocity component and do the logic in some system. Thanks again  !

PS : r is radians , right? Or degrees?
CaptainJester

JGO Knight

Medals: 12
Projects: 2
Exp: 14 years

Make it work; make it better.

 « Reply #3 - Posted 2013-04-21 14:54:24 »

I would recommend against having knockback specifically in the entity. Instead you should have an acceleration attribute. You could just use a vector for acceleration. You then apply acceleration to speed every update. Then you apply friction to acceleration right after. This way anything that affects the entity can just change acceleration.

Riven
« League of Dukes »

« JGO Overlord »

Medals: 955
Projects: 4
Exp: 16 years

 « Reply #4 - Posted 2013-04-21 15:19:03 »

Please note that applying friction to acceleration makes no sense. Apply it to velocity. Acceleration (caused by a force) is immediately zero after it is processed, as the knockback force is instantaneous.

Instead of having an acceleration vector as field(s), I'd do this:
 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16 `class Entity{   Vec2 position;   Vec2 velocity;   float mass;   Vec2 force;   float drag;   public void tick() {      Vec2 acceleration = new Vec2(force).div(mass);      force.set(0,0,0);      velocity.add(acceleration);      position.add(velocity);      velocity.mul(1.0f - drag);   }}`

Whether this fits in your game logic, is another matter

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings!
ClickerMonkey

JGO Coder

Medals: 20

Game Engineer

 « Reply #5 - Posted 2013-04-21 15:26:03 »

According to Riven's logic, also ensure tick() is being called at a consistent rate (like 30fps or something).

Riven
« League of Dukes »

« JGO Overlord »

Medals: 955
Projects: 4
Exp: 16 years

 « Reply #6 - Posted 2013-04-21 15:26:45 »

Ah, yes. I have this assumption that everybody is sensible enough to have a fixed logic rate, and a dynamic render rate.

(Please don't let this derail in a fixed/delta step discussion!)

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings!
Pages: [1]
 ignore  |  Print

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

 ClaasJG (40 views) 2015-04-30 20:33:25 Riven (24 views) 2015-04-30 13:25:12 ClaasJG (45 views) 2015-04-27 13:36:51 BurntPizza (51 views) 2015-04-23 03:42:11 theagentd (52 views) 2015-04-22 16:23:07 Riven (65 views) 2015-04-16 10:48:47 Duke0200 (76 views) 2015-04-16 01:59:01 Fairy Tailz (59 views) 2015-04-14 20:13:12 Riven (63 views) 2015-04-12 21:36:37 bus hotdog (88 views) 2015-04-10 02:39:32
 theagentd 24x BurntPizza 19x Spasi 16x kingroka123 11x wessles 11x alwex 11x Hanksha 10x Olo 8x ra4king 8x Ecumene 8x chrislo27 7x Riven 6x kevglass 6x princec 6x EgonOlsen 5x trollwarrior1 5x
 How 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:14Understanding relations between setOrigin, setScale and setPosition in libGdx2014-10-09 22:35:00Definite guide to supporting multiple device resolutions on Android (2014)2014-10-02 22:36:02List of Learning Resources2014-08-16 10:40:00List of Learning Resources2014-08-05 19:33:27
 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