Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (581)
games submitted by our members
Games in WIP (500)
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  
  Make a train turn?  (Read 1425 times)
0 Members and 1 Guest are viewing this topic.
Offline aslan

Senior Newbie




Java games rock!


« Posted 2003-04-13 11:54:29 »

Am creating a train simulation, but am having problems with how to turn the train? The turning has to be done while the train is moving.

The train:
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  
class Tog extends JPanel
{
   Graphics2D g2;
   Rectangle bounds;
   int x;
   int y;
   int x2;
   int y2;  
   int antalVogne;
   AffineTransform r;
   
   Tog(int x,int y,int antalVogne)
   {
        this.x=x;
        this.y=y;
        x2=x;
        y2=y;
        this.antalVogne=antalVogne;
       
        r = new AffineTransform();
   }
   public void update( Graphics g)
   {
       paint(g);
   }
   
   public void paint(Graphics g)
   {
      g2 = (Graphics2D) g;

      g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
     
      bounds = getBounds();
      g2.clearRect(0,0,(int)bounds.getWidth(),(int)bounds.getHeight());      
     
      g2.setColor(Color.yellow);
      g2.fill3DRect(x,y,60,20,true);
     
      if(antalVogne>0)
      {
         x2+=20;
         
         for(int i=0;i<antalVogne;i++)
         {
            x2+=50;
           
            g2.setColor(Color.red);
            g2.fill3DRect(x2,y2,40,20,true);
            g2.drawLine(x2-10,y2+10,x2,y2+10);
         }      
      }
           
   }
   public void opdater(int x, int y)
   {
        this.x=x;
        this.y=y;
        x2=x;
        y2=y;        
   }
   public void roter()
   {
        // this is where the turning should be
  }
}


The class where thread is created:

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  
......
t = new Tog(x,y,2);
......
   public void run()
   {
      while(true)
      {
         
         repaint();
         
         if(x>=600)
         {
             x-=1;
         }
         else if(x<600 && x>500)
         {
             t.roter(); //the train should rotate here
            x-=1;
             y+=1;
         }
         else if(x<=500 && x>=400)
         {
             x-=1;
         }      
         
         t.opdater(x,y);
         
         try{
         Thread.sleep(50);
         }catch(InterruptedException a){}      
      }
   }
Offline javatypo

Junior Member





« Reply #1 - Posted 2003-04-13 23:07:29 »

I think youll have to rotate each piece of the train separatly..

or make images for each way the train can turn.. but thats a wast of time..
Offline aslan

Senior Newbie




Java games rock!


« Reply #2 - Posted 2003-04-14 10:20:59 »

How is the rotation done? When I try like this:

  public void roter()
  {
     g2.rotate(-1.0, x, y);
     g2.fill3DRect(x,y,60,20,true);
     g2.rotate(1.0, -x, -y);  
  }

The turning is  done ones, then the train returns to start position again?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline javatypo

Junior Member





« Reply #3 - Posted 2003-04-14 19:57:20 »

make each square have a rotation variable so u can keep its rotation value so itll stay the way its supposed to be.
Offline aslan

Senior Newbie




Java games rock!


« Reply #4 - Posted 2003-04-15 09:26:30 »

<make each square have a rotation variable

javatypo, can U be more specific ? I dont have any idea of , how I can do that?
Offline javatypo

Junior Member





« Reply #5 - Posted 2003-04-16 19:57:26 »

For each part of the train that u have, make an Image obj with a graphics 2d object for it. Make sure that the Image is big enough so that the train part can rotate 360 degrees and always be shown.

Then make an int for each of these images to record their current radius.

You could use arrays of graphics contexts, and images and ints.

Then when u are moving the train it has an up/down/left/right speed.

So say u are turning moving up, and are going to turn left. You will slowly decrease the up speed, and increase the left speed. And as you do that, you want to rotate the image so that it looks like it is actaully turning.

so u could have some code like: (when drawing)

1  
2  
3  
4  
5  
g2d1.rotate(Math.toRadians(angle));
g2d1.drawImage(img,center,of_the_image,this);
...

g.drawImage(traincar,xpos,ypos,this);


So that the image that is paints is rotated.


Is that more of a better explanation? If you have anymore questions just post again. I think I know what you may ask... Cool
Offline javatypo

Junior Member





« Reply #6 - Posted 2003-04-16 19:58:48 »

btw, that method of doing things is based on that fact that you are making this game a top down view..
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.

xsi3rr4x (55 views)
2014-04-15 18:08:23

BurntPizza (53 views)
2014-04-15 03:46:01

UprightPath (66 views)
2014-04-14 17:39:50

UprightPath (49 views)
2014-04-14 17:35:47

Porlus (66 views)
2014-04-14 15:48:38

tom_mai78101 (90 views)
2014-04-10 04:04:31

BurntPizza (151 views)
2014-04-08 23:06:04

tom_mai78101 (246 views)
2014-04-05 13:34:39

trollwarrior1 (204 views)
2014-04-04 12:06:45

CJLetsGame (211 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
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!