Java-Gaming.org Hi !
Featured games (81)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (119)
games submitted by our members
Games in WIP (576)
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  
  Grasshopper4k  (Read 2213 times)
0 Members and 1 Guest are viewing this topic.
Offline soothsayer

Senior Newbie


Projects: 1



« Posted 2009-02-18 10:02:14 »



I always wanted to write some car racing game. Not really small size game, but finally this is the issue. Now everyone can create a game with an almost realistic car. I got the idea from APE engine however I simplified collision detection because it was too difficult.

Until deadline 10 days have remained yet. Try to write a better game, for instance  better car or terrain generation etc.

http://users.atw.hu/cppanddelphi/grasshopper4k/A.java

See below the physics model. Joints and constraints. Maybe this is the best arrangement, but you can experience with the structure.

Click to Play
Offline zeroone
« Reply #1 - Posted 2009-02-18 15:30:56 »

Can you provide a better explanation of how your physics engine works?  This is really cool stuff.
Offline soothsayer

Senior Newbie


Projects: 1



« Reply #2 - Posted 2009-02-18 16:51:47 »

First of all the surface, the particle and the constraint parts should remain unchanged. These are essential parts.

One can create model structure on simple way.

See the variables

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  
 // Surfaces (n pieces depends terrain)
  private float[][] S = new float[65535][6];
  // 0 = point1X
  // 1 = point1Y
  // 2 = point2X
  // 3 = point2Y
  // 4 = surface normal X
  // 5 = surface normal Y

  // Particles (7 pieces)
  private float[][] P = new float[7][14];
  // 0 = is active
  // 1 = current position X
  // 2 = current position Y
  // 3 = previous position X
  // 4 = previous position Y
  // 5 = effective force X
  // 6 = effective force Y
  // 7 = particle radius
  // other auxiliary variables
  // 8 = tempX
  // 9 = tempY
  // 10 = closestPointX
  // 11 = closestPointY
  // 12 = correctionX
  // 13 = correctionY

  // Constraints (18 pieces)
  private float[][] C = new float[18][4];
  // 0 = index1
  // 1 = index2
  // 2 = restlength
  // 3 = stiffness


If you create a surface just give 2 points. Be careful, the normal of surface determines a half plane. A surface is effective if the the particle is on the same side of this half plane. Particles and constraints should be above the surfaces.

If you create a particle just give a particle number, x and y coordinates and a radius. Every particle is active by default. In the given code the first particle not active, so collision detection skip it.

If you create a constraint just give a constraint number and particles number, between these particles the constraint is situated. At last the stiffness value.

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  
    particleCreate(0, -65,  -30, 1.15f);
    particleCreate(1,   0,  -30, 0.5f); P[1][0] = 0;
    particleCreate(2,  65,  -30, 1.15f);
    particleCreate(3,  65,  -70, 0.85f);
    particleCreate(4,  30, -100, 1.15f);
    particleCreate(5, -30, -100, 1.15f);
    particleCreate(6, -65,  -70, 0.85f);

    constraintCreate(0, 0, 1, 0.25f);
    constraintCreate(1, 1, 2, 0.25f);
    constraintCreate(2, 2, 3, 0.25f);
    constraintCreate(3, 3, 4, 0.25f);
    constraintCreate(4, 4, 5, 0.25f);
    constraintCreate(5, 5, 6, 0.25f);
    constraintCreate(6, 6, 0, 0.25f);

    constraintCreate(7, 0, 3, 0.15f);
    constraintCreate(8, 0, 4, 0.15f);
    constraintCreate(9, 0, 5, 0.15f);
    constraintCreate(10, 1, 3, 0.15f);
    constraintCreate(11, 1, 4, 0.15f);
    constraintCreate(12, 1, 5, 0.15f);
    constraintCreate(13, 1, 6, 0.15f);
    constraintCreate(14, 2, 4, 0.15f);
    constraintCreate(15, 2, 5, 0.15f);
    constraintCreate(16, 2, 6, 0.15f);
    constraintCreate(17, 3, 6, 0.15f);


One engine step is fulfilled the following way. Go through all particles and constraints with particleVerlet() and constraintResolve() procedure.

Finally check particles and surfaces collision. If there are a lot of surface then collision detection is very time consuming. It worths to perform some space partitioning procedure.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
        int delta = (int) (System.currentTimeMillis() - lastLoopTime);
        lastLoopTime = System.currentTimeMillis();

        for (k = 0; k < delta / 15; k++)
        {
          // timestep
          for (i = 0; i < 7; i++)  // particles (7)
            particleVerlet(i);
          for (i = 0; i < 18; i++)  // constraints (18)
            constraintResolve(i);
          for (j = 0; j < surfaceCount; j++)  // surfaces (surfaceCount)
            for (i = 0; i < 7; i++)  // particles (7)
              particleCheckCollision(j, i);
        }


One thing is missing. The forces.
Forces must take effect parallel with vector which indicate form point 0 to point 2.

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  
  private void HandleKeyEvent()
  {
    float dist, vX, vY;

    dist = (float)distance(P[0][1], P[0][2], P[2][1], P[2][2]);
    vX = (P[2][1] - P[0][1]) / dist;
    vY = (P[2][2] - P[0][2]) / dist;

    if (K[KeyEvent.VK_UP])
    {
      P[0][5] = vX * 2.75f;
      P[0][6] = vY * 2.75f;
      P[2][5] = vX * 2.75f;
      P[2][6] = vY * 2.75f;
    };
    if (K[KeyEvent.VK_DOWN])
    {
      {
        P[0][5] = vX * -1.75f;
        P[0][6] = vY * -1.75f;
        P[2][5] = vX * -1.75f;
        P[2][6] = vY * -1.75f;
      }
    };
    if (! K[KeyEvent.VK_DOWN] && ! K[KeyEvent.VK_UP])
    {
      P[0][5] = 0;
      P[0][6] = 0;
      P[2][5] = 0;
      P[2][6] = 0;
    };
  }


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

JGO Ninja


Medals: 52



« Reply #3 - Posted 2009-02-19 08:59:08 »

Doesn't seem to do anything here with one cpu core 100% load.

JDK 6 u12 (32Bit)
Windows Vista Business (64Bit)
Core 2 Duo P9500 2.53Ghz
9800M GTS
Forceware 185.20



Mathias - I Know What [you] Did Last Summer!
Offline soothsayer

Senior Newbie


Projects: 1



« Reply #4 - Posted 2009-02-19 10:20:06 »

I am afraid cylab I do not know what is the problem.

Perhaps run the program without antialiasing.
Offline cylab

JGO Ninja


Medals: 52



« Reply #5 - Posted 2009-02-19 10:38:50 »

Thats very odd. I ran the code in the Netbeans profiler and it rund fine, while running it normally, it stalls...

Mathias - I Know What [you] Did Last Summer!
Offline cylab

JGO Ninja


Medals: 52



« Reply #6 - Posted 2009-02-19 10:42:20 »

Adding a Thread.sleep(10) at the end of the main loop did the trick. It seems your tight loop has eaten away the AWT-Thread.

Mathias - I Know What [you] Did Last Summer!
Offline soothsayer

Senior Newbie


Projects: 1



« Reply #7 - Posted 2009-02-19 12:59:42 »

Thank you very much. I will complete the code soon.
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.

Longarmx (41 views)
2014-10-17 03:59:02

Norakomi (33 views)
2014-10-16 15:22:06

Norakomi (26 views)
2014-10-16 15:20:20

lcass (30 views)
2014-10-15 16:18:58

TehJavaDev (59 views)
2014-10-14 00:39:48

TehJavaDev (60 views)
2014-10-14 00:35:47

TehJavaDev (50 views)
2014-10-14 00:32:37

BurntPizza (66 views)
2014-10-11 23:24:42

BurntPizza (38 views)
2014-10-11 23:10:45

BurntPizza (80 views)
2014-10-11 22:30:10
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!