Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (475)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (530)
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  
  Drift for 2D Space Game  (Read 936 times)
0 Members and 1 Guest are viewing this topic.
Offline Slyth2727
« Posted 2013-07-06 08:16:16 »

GUYS IM NOT DEAD. My computer just crashed and I managed to install ubuntu on it to start programming again. I went 2 months without programming D:! Anyways, remember those old top down racing games where you just guided that car along the track and get as much "drift" as possible? How would I implement that in my libgdx game? Just in case you need it, here's the spaceship 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  
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  
package com.slyth.spaceGame.Models;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;

public class Ship extends MovableEntity {

   public boolean left = false, right = false, up = false, down = false, isKeyDown = false;
   float rotateSpeed = 30, health = 100, SPEEDORIG;
   Rectangle pickupBounds;
   boolean isUpgradedSpeed = false;
   public int usT = 0, usP = 400;

   public Ship(Vector2 position, float width, float height, float rotation, float SPEED) {
      super(SPEED, rotation, width, height, position);
      this.SPEEDORIG = SPEED;
      pickupBounds = new Rectangle(position.x - ((width * 3) / 2), position.y - ((height * 3) / 2), width * 3, height * 3);
   }

   public void update() {
      if (isUpgradedSpeed) {
         if (usT >= usP) {
            isUpgradedSpeed = false;
            SPEED = SPEEDORIG;
            usP = 400;
            usT = 0;
         } else {
            SPEED = SPEEDORIG + 2;
            usT++;
         }
      }
      pickupBounds = new Rectangle(position.x - ((width * 10) / 2.5f), position.y - ((height * 10) / 2.5f), width * 10, height * 10);
      if (left) {
         if (rotation <= 360) {
            rotation += .1f * rotateSpeed - 0.03;
         } else {
            rotation = 0;
            rotation += .1f * rotateSpeed - 0.03;
         }

      } else if (right) {
         if (rotation >= 0) {
            rotation += -.1f * rotateSpeed - 0.03;
         } else {
            rotation = 360;
            rotation += -.1f * rotateSpeed - 0.03;
         }
      }
      if (up) {
         float yP = MathUtils.sinDeg(rotation + 90) * SPEED;
         float xP = MathUtils.cosDeg(rotation + 90) * SPEED;

         velocity.y = yP;
         velocity.x = xP;
      } else if (!up && !down) {
         velocity.x *= .95;
         velocity.y *= .95;
      } else if (down) {
         float yP = MathUtils.sinDeg(rotation + 90) * SPEED;
         float xP = MathUtils.cosDeg(rotation + 90) * SPEED;

         velocity.y = -yP;
         velocity.x = -xP;
      }
      if(!isKeyDown){

      }
      position.add(velocity.tmp().mul(Gdx.graphics.getDeltaTime() * SPEED));

      bounds.x = position.x;
      bounds.y = position.y;
   }

   public float getHealth() {
      return health;
   }

   public Rectangle getPickupBounds() {
      return pickupBounds;
   }

   public void setHealth(float am) {
      health = am;
   }

   public void loseHealth(float am) {
      health -= am;
   }

   public void heal(float am) {
      health += am;
   }

   public void upgradeSpeed() {
      if (!isUpgradedSpeed) {
         isUpgradedSpeed = true;
      } else {
         usP += 400;
      }
   }

   public boolean isSpeedUpgraded() {
      return isUpgradedSpeed;
   }
}


Thanks
-cMp

Was I before Chuang Tzu who dreamt about being a butterfly, or am I now a butterfly who dreams about being Chuang Tzu?
Offline HeroesGraveDev

JGO Kernel


Medals: 238
Projects: 11
Exp: 2 years


┬─┬ノ(ಠ_ಠノ)(╯°□°)╯︵ ┻━┻


« Reply #1 - Posted 2013-07-06 08:21:54 »

Drifting is caused when you change the direction you're facing, without changing the direction of your motion (at least temporarily).

Think about that for a bit.

Offline Slyth2727
« Reply #2 - Posted 2013-07-06 08:28:20 »

so I should do some sort of time step after I rotate to move in the same direction for a small bit? From what I am thinking that actually would make it like a racing game. I didn't make it clear, I need the effect of 0 gravity so that it would drag like a racing game, but still move in the direction you are facing.

Was I before Chuang Tzu who dreamt about being a butterfly, or am I now a butterfly who dreams about being Chuang Tzu?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline HeroesGraveDev

JGO Kernel


Medals: 238
Projects: 11
Exp: 2 years


┬─┬ノ(ಠ_ಠノ)(╯°□°)╯︵ ┻━┻


« Reply #3 - Posted 2013-07-06 08:30:29 »

so I should do some sort of time step after I rotate to move in the same direction for a small bit? From what I am thinking that actually would make it like a racing game. I didn't make it clear, I need the effect of 0 gravity so that it would drag like a racing game, but still move in the direction you are facing.

That would be fine. I recommend doing that.

But for a more realistic effect it would be a good idea to learn basic kinematics.

Offline Slyth2727
« Reply #4 - Posted 2013-07-06 08:36:01 »

Will do, thanks. Also, if you know of an easier/better way to do time steps in libGDX instead of the standard
1  
2  
3  
4  
5  
6  
if(firstVar >= secVar){
var++:
firstVar=0;
}else{
firstVar++:
}


could you tell me?

Was I before Chuang Tzu who dreamt about being a butterfly, or am I now a butterfly who dreams about being Chuang Tzu?
Offline HeroesGraveDev

JGO Kernel


Medals: 238
Projects: 11
Exp: 2 years


┬─┬ノ(ಠ_ಠノ)(╯°□°)╯︵ ┻━┻


« Reply #5 - Posted 2013-07-06 08:37:37 »

What do you mean by time step?

Offline Slyth2727
« Reply #6 - Posted 2013-07-06 08:43:10 »

the process where you increase or decrease a variable over time in the code. An example is up there in the ship class where if the ship is not being moved I do velocity.x *= .98f to make the ship slow down over time. However the type I meant was where you have a first variable (varOne), and a second (varTwo), and compare them to eachother to edit a variable over time. Hard to explain I guess Tongue

Was I before Chuang Tzu who dreamt about being a butterfly, or am I now a butterfly who dreams about being Chuang Tzu?
Offline HeroesGraveDev

JGO Kernel


Medals: 238
Projects: 11
Exp: 2 years


┬─┬ノ(ಠ_ಠノ)(╯°□°)╯︵ ┻━┻


« Reply #7 - Posted 2013-07-06 08:45:49 »

So every X ticks, you increment a variable by one?

In that case:

1  
2  
3  
4  
5  
6  
firstVar--;
if(firstVar == 0)
{
    firstVar = secVar;
    var++;
}

Kind of does the same thing. Not sure if it's more optimised. It's just easier to code.

Offline Slyth2727
« Reply #8 - Posted 2013-07-06 08:49:13 »

Ah ok, thanks. I have been searching the kinematics you suggested and I haven't found anything related to drifting. Is there some simple math that you can write down in just a couple seconds if it doesn't bother you? Thanks

Was I before Chuang Tzu who dreamt about being a butterfly, or am I now a butterfly who dreams about being Chuang Tzu?
Offline HeroesGraveDev

JGO Kernel


Medals: 238
Projects: 11
Exp: 2 years


┬─┬ノ(ಠ_ಠノ)(╯°□°)╯︵ ┻━┻


« Reply #9 - Posted 2013-07-06 09:01:01 »

1  
2  
3  
4  
5  
6  
7  
// Vector2 facingVector
// Vector2 movingVector
// float inertia
// float acceleration

movingVector -= multiply(normalize(movingVector), inertia);
movingVector += multiply(normalize(facingVector), acceleration);


Requires some knowledge of vector math.
Can probably be optimised.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Slyth2727
« Reply #10 - Posted 2013-07-06 09:03:53 »

Gotcha, thanks!

Was I before Chuang Tzu who dreamt about being a butterfly, or am I now a butterfly who dreams about being Chuang Tzu?
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.

ctomni231 (34 views)
2014-07-18 06:55:21

Zero Volt (30 views)
2014-07-17 23:47:54

danieldean (25 views)
2014-07-17 23:41:23

MustardPeter (27 views)
2014-07-16 23:30:00

Cero (42 views)
2014-07-16 00:42:17

Riven (44 views)
2014-07-14 18:02:53

OpenGLShaders (32 views)
2014-07-14 16:23:47

Riven (33 views)
2014-07-14 11:51:35

quew8 (30 views)
2014-07-13 13:57:52

SHC (66 views)
2014-07-12 17:50:04
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
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!