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
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  [libGDX] [box2D] Body with no bounce  (Read 929 times)
0 Members and 1 Guest are viewing this topic.
Offline SrFurias

Senior Newbie





« Posted 2013-07-18 18:07:57 »

Hi, I go to business, I need dot a body do not bounce when it colide with anything.

I tryied putting restitution to 0 but nothing happend.

How can I remove the bounce force of a body?

Thanks.

I'm spanish, thanks for try understand me.  persecutioncomplex
Offline DazKins
« Reply #1 - Posted 2013-07-18 19:07:14 »

We need to know a bit more information first.

Maybe give us a code example, tell us which library you are using or show a screenshot of the issue

(No era bueno InglĂ©s, pero entendĂ­ lo suficiente)   Cheesy

DazKins

Check out my Dev Blog: http://dazkins.tumblr.com
Offline SrFurias

Senior Newbie





« Reply #2 - Posted 2013-07-18 19:18:49 »

I put always the library dot I use, but I don't know why not this time.  Yawn

I'm using libGDX and physics Box2D.

I have a class for the player control:

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  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
158  
159  
160  
161  
162  
163  
164  
165  
166  
package com.unclain.udevelop.prueba1.utils;

import java.util.List;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.graphics.FPSLogger;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.Contact;
import com.badlogic.gdx.physics.box2d.Fixture;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.World;
import com.badlogic.gdx.physics.box2d.WorldManifold;

public class CharacterController {
   
   private boolean jump = false;
   private boolean grounded = false;
   
   private final static float MAX_VELOCITY = 7f;
   
   float stillTime = 0;
   long lastGroundTime = 0;
   
   public Body bodyPlayer;
   private FixtureDef fixtureDefPlayer;
   private Fixture fixturePlayer;
   private MovingPlatform groundedPlatform = null;
   
   private ContactCheck checker;
   
   private World world;
   
   private FPSLogger fps;
   
   private BitmapFont font;
   
   public CharacterController(World world, Body bodyPlayer, Fixture fixturePlayer, FixtureDef fixtureDefPlayer){
      this.bodyPlayer = bodyPlayer;
      this.fixtureDefPlayer = fixtureDefPlayer;
      this.fixturePlayer = fixturePlayer;
      this.world = world;
      addKeyControls();
      fps = new FPSLogger();
      System.out.println("Jump = " + jump);
      checker = new ContactCheck(world, bodyPlayer, fixturePlayer);
      font = new BitmapFont();
   }
   
   public void addKeyControls(){
      Vector2 vel = bodyPlayer.getLinearVelocity();
      Vector2 pos = bodyPlayer.getPosition();
     
      grounded = isGrounded(Gdx.graphics.getDeltaTime());
     
      fixtureDefPlayer.density = 0.0f;
      fixtureDefPlayer.restitution = 0.00000f;

      if(grounded){
         lastGroundTime = System.nanoTime();
      } else {
         if(System.nanoTime() - lastGroundTime < 100000000){
            grounded = true;
         }
      }
     
      if(grounded && stillTime > 0.1){
         
      }
     
      if(Math.abs(vel.x) > MAX_VELOCITY){
         vel.x = Math.signum(vel.x) * MAX_VELOCITY;
         bodyPlayer.setLinearVelocity(vel.x, vel.y);
      }
     
      if(!Gdx.input.isKeyPressed(Keys.A) && !Gdx.input.isKeyPressed(Keys.D)) {        
         stillTime += Gdx.graphics.getDeltaTime();
         bodyPlayer.setLinearVelocity(vel.x * 0.9f, vel.y);
      }
      else {
         stillTime = 0;
      }
     
      if(!grounded) {        
         fixturePlayer.setFriction(100f);
      } else {
         if(!Gdx.input.isKeyPressed(Keys.A) && !Gdx.input.isKeyPressed(Keys.D) && stillTime > 0.2) {
            fixturePlayer.setFriction(100f);
         }
         else {
            fixturePlayer.setFriction(0.2f);
         }

         if(groundedPlatform != null && groundedPlatform.dist == 0) {
            bodyPlayer.applyLinearImpulse(0, -24, pos.x, pos.y);            
         }
      }
     
      if(Gdx.input.isKeyPressed(Keys.A) && vel.x > -MAX_VELOCITY) {
         bodyPlayer.applyLinearImpulse(-2f, 0, pos.x, pos.y);
      }
     
      if(Gdx.input.isKeyPressed(Keys.D) && vel.x < MAX_VELOCITY) {
         bodyPlayer.applyLinearImpulse(2f, 0, pos.x, pos.y);
      }
     
      if(Gdx.input.isKeyPressed(Keys.W)){
         jump = true;
      }
     
      if(jump) {        
         jump = false;
         if(grounded == true) {
            bodyPlayer.setLinearVelocity(vel.x, 0);        
            System.out.println("jump before: " + bodyPlayer.getLinearVelocity());
            bodyPlayer.setTransform(pos.x, pos.y + 0.01f, 0);
            bodyPlayer.applyLinearImpulse(0, 10, pos.x, pos.y);        
            System.out.println("jump, " + bodyPlayer.getLinearVelocity());            
         }
      }
   }
   
   public boolean isGrounded(float deltaTime) {            
      groundedPlatform = null;
      List<Contact> contactList = world.getContactList();
      for(int i = 0; i < contactList.size(); i++) {
         Contact contact = contactList.get(i);
         if(contact.isTouching() && (contact.getFixtureA() == fixturePlayer)) {            

            Vector2 pos = bodyPlayer.getPosition();
            WorldManifold manifold = contact.getWorldManifold();
            boolean below = true;
            for(int j = 0; j < manifold.getNumberOfContactPoints(); j++) {
               below &= (manifold.getPoints()[j].y < pos.y - 1.5f);
            }

            if(below) {
               if(contact.getFixtureA().getUserData() != null && contact.getFixtureA().getUserData().equals("p")) {
                  groundedPlatform = (MovingPlatform)contact.getFixtureA().getBody().getUserData();                    
               }

               if(contact.getFixtureB().getUserData() != null && contact.getFixtureB().getUserData().equals("p")) {
                  groundedPlatform = (MovingPlatform)contact.getFixtureB().getBody().getUserData();
               }                                
               return true;
            }

            return true;
         }
      }
      return false;
   }
   
   public void drawFont(SpriteBatch batch){
      bodyPlayer.setAwake(true);
      batch.begin();
      font.drawMultiLine(batch, "friction: " + fixturePlayer.getFriction() + "\ngrounded: " + grounded + "\njump: " +
      jump + "\nFPS: " + Gdx.graphics.getFramesPerSecond() + "\nrestitution: " + fixtureDefPlayer.restitution +
      "\ndensity: " + fixtureDefPlayer.density + "\nawake: " + bodyPlayer.isAwake() + "\nstilltime: " + stillTime, 15, Gdx.graphics.getHeight() - 15);
      batch.end();
   }

}


P.D: It's the first time dot anybody reply me with spanish, hahahaha. Your spanish answer it's perfect, but before English you need put "el", we are at peace.  Grin

Thanks for reply me. Good luck with your projects.  Cheesy
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Jimmt
« League of Dukes »

JGO Kernel


Medals: 138
Projects: 4
Exp: 3 years



« Reply #3 - Posted 2013-07-18 19:31:46 »

Do it when you create the FixtureDef:
1  
2  
3  
4  
5  
FixtureDef fd = new FixtureDef();
fd.shape = bodyBox;
fd.density = 1.0f;
fd.friction = 1.0f;
fd.restitution = 0.0f;

I don't really remember, but I'm pretty sure it's the density that disables the bouncing. I left the other stuff there just in case.
Offline SrFurias

Senior Newbie





« Reply #4 - Posted 2013-07-18 19:53:56 »

I'm stupid. I only put it at CharacterController class, so for this it didn't work.

Thanks, you save me.  Grin
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 (29 views)
2014-11-22 12:13:56

xFryIx (71 views)
2014-11-13 12:34:49

digdugdiggy (50 views)
2014-11-12 21:11:50

digdugdiggy (44 views)
2014-11-12 21:10:15

digdugdiggy (38 views)
2014-11-12 21:09:33

kovacsa (62 views)
2014-11-07 19:57:14

TehJavaDev (67 views)
2014-11-03 22:04:50

BurntPizza (64 views)
2014-11-03 18:54:52

moogie (80 views)
2014-11-03 06:22:04

CopyableCougar4 (80 views)
2014-11-01 23:36:41
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

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
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!