Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (499)
Games in Android Showcase (118)
games submitted by our members
Games in WIP (568)
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  
  Entity knockback  (Read 965 times)
0 Members and 1 Guest are viewing this topic.
Offline 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.
Offline kalkitus

Senior Newbie


Medals: 1
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;
      }
        }
Offline Mayhaw

Junior Newbie




The Bugger


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

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

PS : r is radians , right? Or degrees?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline 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.

Offline Riven
« League of Dukes »

JGO Overlord


Medals: 802
Projects: 4
Exp: 16 years


Hand over your head.


« 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 Smiley

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline 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).

Offline Riven
« League of Dukes »

JGO Overlord


Medals: 802
Projects: 4
Exp: 16 years


Hand over your head.


« 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.

Pippogeek (39 views)
2014-09-24 16:13:29

Pippogeek (30 views)
2014-09-24 16:12:22

Pippogeek (20 views)
2014-09-24 16:12:06

Grunnt (45 views)
2014-09-23 14:38:19

radar3301 (28 views)
2014-09-21 23:33:17

BurntPizza (64 views)
2014-09-21 02:42:18

BurntPizza (34 views)
2014-09-21 01:30:30

moogie (42 views)
2014-09-21 00:26:15

UprightPath (50 views)
2014-09-20 20:14:06

BurntPizza (54 views)
2014-09-19 03:14:18
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!