Java-Gaming.org Hi !
 Featured games (90) games approved by the League of Dukes Games in Showcase (754) Games in Android Showcase (229) 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
 Entity knockback  (Read 2349 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

« JGO Overlord »

Medals: 1339
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: 26
Exp: 10 years

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

« JGO Overlord »

Medals: 1339
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

 DesertCoockie (20 views) 2018-05-13 18:23:11 nelsongames (68 views) 2018-04-24 18:15:36 nelsongames (65 views) 2018-04-24 18:14:32 ivj94 (748 views) 2018-03-24 14:47:39 ivj94 (79 views) 2018-03-24 14:46:31 ivj94 (579 views) 2018-03-24 14:43:53 Solater (95 views) 2018-03-17 05:04:08 nelsongames (168 views) 2018-03-05 17:56:34 Gornova (362 views) 2018-03-02 22:15:33 buddyBro (1022 views) 2018-02-28 16:59:18
 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