Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (541)
Games in Android Showcase (133)
games submitted by our members
Games in WIP (603)
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  
  [Fixed] Degrading velocity  (Read 875 times)
0 Members and 1 Guest are viewing this topic.
Offline Mads

JGO Ninja


Medals: 26
Projects: 3
Exp: 6 years


One for all!


« Posted 2011-07-07 19:07:19 »

One more silly problem: I'm degrading my velocities like this:
1  
2  
3  
4  
5  
6  
7  
8  
9  
if (input.isKeyDown(Input.KEY_UP)) {
       player.setXV(player.getXVelocity() +
      Math.cos(Math.toRadians(player.getRotation())));
       player.setYV(player.getYVelocity() +
      Math.sin(Math.toRadians(player.getRotation())));
   } else {
       player.setXV(player.getXVelocity()*0.95);
       player.setYV(player.getYVelocity()*0.95);
}


However, when I've just moved and my vehicle has gotton to the point of stopping, I see this is my terminal:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
XVelocity: 1.8496787833130738E-16 YVelocity: 1.5520647848744412E-16
XVelocity: 1.75719484414742E-16 YVelocity: 1.474461545630719E-16
XVelocity: 1.6693351019400487E-16 YVelocity: 1.400738468349183E-16
XVelocity: 1.5858683468430463E-16 YVelocity: 1.3307015449317238E-16
XVelocity: 1.506574929500894E-16 YVelocity: 1.2641664676851377E-16
XVelocity: 1.4312461830258492E-16 YVelocity: 1.2009581443008806E-16
XVelocity: 1.3596838738745567E-16 YVelocity: 1.1409102370858365E-16
XVelocity: 1.291699680180829E-16 YVelocity: 1.0838647252315446E-16
XVelocity: 1.2271146961717873E-16 YVelocity: 1.0296714889699673E-16
XVelocity: 1.1657589613631978E-16 YVelocity: 9.781879145214689E-17


I'm also experiencing some "pulls" in the vehicle when it's slowing down. For example, when moving diagonally down/left, it would suddenly drag the vehicle a lot more to the left than it would under normal physics. I think these two are related.

In my head multiplying something by 0.95 seems perfectly fine for degrading something slowly. Although it's never hit zore fully.. Meh..
Anyone got any other options on degrading, that doesn't spawn weird bugs? Tongue

Thanks
   

Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #1 - Posted 2011-07-07 19:13:14 »

I usually just add in:

1  
2  
xVel = xVel > -0.01 && xVel < 0.01 ? 0 : xVel;
yVel = yVel > -0.01 && yVel < 0.01 ? 0 : yVel;


Basically just cutting off very small values. Your results look correct if you don't have that logic.

Also rather than directly adding sin and cos results, I'd add sin * speed and cos * speed, and if you're using a delta in your game loop do sin * speed * delta. What happens when you press the Left key? The code you posted above shouldn't have any issues.

See my work:
OTC Software
Offline Mads

JGO Ninja


Medals: 26
Projects: 3
Exp: 6 years


One for all!


« Reply #2 - Posted 2011-07-07 20:01:04 »

I usually just add in:

1  
2  
xVel = xVel > -0.01 && xVel < 0.01 ? 0 : xVel;
yVel = yVel > -0.01 && yVel < 0.01 ? 0 : yVel;


Basically just cutting off very small values. Your results look correct if you don't have that logic.

Also rather than directly adding sin and cos results, I'd add sin * speed and cos * speed, and if you're using a delta in your game loop do sin * speed * delta. What happens when you press the Left key? The code you posted above shouldn't have any issues.

I'm still just fooling around to the speed parameter will have to wait. Now the variable are reset proporly, but the problem persists.
When slowing down, the vehicle gets slightly pulled in a direction.

I'll just post my render method:
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  
public void update(GameContainer container, int delta)
       throws SlickException {
   Input input = container.getInput();
   Actor player = model.getPlayer();
   
   // Input handling
   if (input.isKeyDown(Input.KEY_LEFT)) {
       player.rotate(-5);
   }
   if (input.isKeyDown(Input.KEY_RIGHT)) {
       player.rotate(5);
   }
       
   if (input.isKeyDown(Input.KEY_UP)) {
       player.setXV(player.getXVelocity() +
      Math.cos(Math.toRadians(player.getRotation())));
       player.setYV(player.getYVelocity() +
      Math.sin(Math.toRadians(player.getRotation())));
   } else {
       if (player.getXVelocity() > -0.01 && player.getXVelocity() < 0.01) {
      player.setXV(0);
       }
       if (player.getYVelocity() > -0.01 && player.getYVelocity() < 0.01) {
      player.setYV(0);
       }
       player.setXV(player.getXVelocity()*0.95);
       player.setYV(player.getYVelocity()*0.95);
   }
   
   player.setX(player.getX()+(int)player.getXVelocity());
   player.setY(player.getY()+(int)player.getYVelocity());
   
   System.out.println("XVelocity: "+ player.getXVelocity() + " YVelocity: "+player.getYVelocity());
   
    }


..and my Actor class:
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  
package com.javadaemon.asteroids.model;

public class Actor {
   
    private int worldX, worldY; // Pixel coordinates - the middle of the actor
    private double rotation; // The amount this actor is rotated
   
    private double xVelocity;
    private double yVelocity;
   
    public Actor(int worldX, int worldY) {
   this.worldX = worldX;
   this.worldY = worldY;
   
   rotation = 0;
    }
   
    public void rotate(double amount) {
   rotation += amount;
   if (rotation > 360) {
       rotation = 0;
   } else if (rotation < 0) {
       rotation = 360;
   }
    }
   
    public void setXV(double xv) {
   xVelocity = xv;
    }
   
    public void setYV(double yv) {
   yVelocity = yv;
    }
   
    public void setX(int x) {
   worldX = x;
    }
   
    public void setY(int y) {
   worldY = y;
    }
   
    public double getRotation() {
   return rotation;
    }
   
    public int getX() {
   return worldX;
    }
   
    public int getY() {
   return worldY;
    }
   
    public double getXVelocity() {
   return xVelocity;
    }
   
    public double getYVelocity() {
   return yVelocity;
    }
}


Should I upload a video?  Clueless

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Mads

JGO Ninja


Medals: 26
Projects: 3
Exp: 6 years


One for all!


« Reply #3 - Posted 2011-07-07 20:23:41 »

Fixed. It was rounding. Now all my variables are doubles, and i round them upon render.  Cool

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.

TheDudeFromCI (12 views)
2014-12-27 02:14:49

Mr.CodeIt (25 views)
2014-12-23 03:34:11

rwatson462 (55 views)
2014-12-15 09:26:44

Mr.CodeIt (46 views)
2014-12-14 19:50:38

BurntPizza (91 views)
2014-12-09 22:41:13

BurntPizza (113 views)
2014-12-08 04:46:31

JscottyBieshaar (83 views)
2014-12-05 12:39:02

SHC (94 views)
2014-12-03 16:27:13

CopyableCougar4 (102 views)
2014-11-29 21:32:03

toopeicgaming1999 (161 views)
2014-11-26 15:22:04
Resources for WIP games
by kpars
2014-12-18 10:26:14

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