Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ernest. That did the trick.
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The reason behind my question:

http://www.greggbolinger.com/blog/2008/12/09/1228885320000.html
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4678
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic