Java-Gaming.org Hi !
 Featured games (83) games approved by the League of Dukes Games in Showcase (522) Games in Android Showcase (127) games submitted by our members Games in WIP (590) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
Pages: [1]
 ignore  |  Print
 Entity knockback  (Read 1030 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: 835
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: 835
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.

 Add your game by posting it in the WIP section, or publish it in Showcase. The first screenshot will be displayed as a thumbnail.
 trollwarrior1 (27 views) 2014-11-22 12:13:56 xFryIx (69 views) 2014-11-13 12:34:49 digdugdiggy (48 views) 2014-11-12 21:11:50 digdugdiggy (42 views) 2014-11-12 21:10:15 digdugdiggy (36 views) 2014-11-12 21:09:33 kovacsa (60 views) 2014-11-07 19:57:14 TehJavaDev (64 views) 2014-11-03 22:04:50 BurntPizza (62 views) 2014-11-03 18:54:52 moogie (77 views) 2014-11-03 06:22:04 CopyableCougar4 (77 views) 2014-11-01 23:36:41
 theagentd 29x basil_ 27x HeroesGraveDev 23x BurntPizza 22x Riven 19x Spasi 18x princec 17x KevinWorkman 13x kpars 11x SHC 11x gouessej 10x Grunnt 10x Nate 9x Longarmx 9x LiquidNitrogen 8x CopyableCougar4 8x
 Understanding 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:27Resources for WIP games2014-08-01 16:20:17Resources for WIP games2014-08-01 16:19:50List of Learning Resources2014-07-31 16:29:50List of Learning Resources2014-07-31 16:26:06
 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