File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Game Development and the fly likes Play Lunar Lander (was: Lunar Lander Left and Right Movement) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Game Development
Bookmark "Play Lunar Lander (was: Lunar Lander Left and Right Movement)" Watch "Play Lunar Lander (was: Lunar Lander Left and Right Movement)" New topic
Author

Play Lunar Lander (was: Lunar Lander Left and Right Movement)

Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15300
    
    6

I'm working on a Lunar Lander game and am having some problems figuring out the best way to handle left and right movement. I have up and down working with gravity and adjusting the velocity. In my Ship objects motion function, which paints the ship at the new location I have the following:



dy posy, gravity, and thrust are just floats initial values. When I press the up arrow key, the thrust is greater than gravity so eventually the ship begins to climb. When released, it eventually begins to fall again.

With regards to my problem of moving left and right I just can't seem to get it. I've tried the following:



The ship will move in the direction I press but way too fast and it immediately moves in the opposite direction when I press that key, there is no gradule change like there should be, the same as up and down.
[ December 13, 2008: Message edited by: Gregg Bolinger ]

GenRocket - Experts at Building Test Data
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Physics aside, I think the main issue here is the use of +/- and positive/negative values. Let 'dx' be positive for motion to the right, and negative for motion to the left. Therefore you want to subtract from dx for left motion, and add to it for right motion. But when you compute the new position, you should always add; i.e., the position in the next frame is always x + dx. dx can be negative (meaning actually moving to the left), 0 (meaning stationary) or positive (meaning moving to the right.) If you're moving fast to the right, and you press left once, then you want dx to become somewhat less positive; we still are moving to the right, but slightly less fast. If there's no horizontal keypress at all, you want to continue to move just as fast to the right as before: you're in space and there's no horizontal damping. So the code could look like


[ December 09, 2008: Message edited by: Ernest Friedman-Hill ]

[Jess in Action][AskingGoodQuestions]
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15300
    
    6

Thanks Ernest. That did the trick.
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15300
    
    6

The reason behind my question:

http://www.greggbolinger.com/blog/2008/12/09/1228885320000.html
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Interesting -- thanks for posting this!

I hadn't learned anything about JavaFX before, so this gave me an excuse to download the SDK. I tried to build your code "cold", without reading any instructions. It too me a number of tries. I was a little surprised to find that the compiler was almost as picky about filenames as is the Java compiler.

So why didn't they base it on Groovy?
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15300
    
    6

Originally posted by Ernest Friedman-Hill:
So why didn't they base it on Groovy?


I've asked the same question. I believe it was because Groovy's syntax isn't IDE friendly enough. They wanted something quite a bit more human readable? Something that non-developers could grasp fairly easily.

You might be interested in this though and Andres told me that since all of JavaFX media capabilities will be available through Java then there is certainly something in the works on the groovy side of things.
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15300
    
    6

Created a page where you can play the game via web start and download the source.

http://www.paradigmcoders.net/projects/javafx/lunarlander/
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4659
    
    5

So is there a McDonalds over the mountain?

I played LL on a DEC GT40 graphics workstation in 77 or so. It was a PDP-11 with a big bitmapped display. You would toggle the front panel switches to control the rocket motor. This was in the big corporate marketing room in Marlboro, the PHB didn't like us playing it during working hours.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Play Lunar Lander (was: Lunar Lander Left and Right Movement)