Personal computing discussed

Moderators: renee, SecretSquirrel, just brew it!

 
Kurotetsu
Gerbil Elite
Topic Author
Posts: 548
Joined: Sun Dec 09, 2007 12:13 pm

Moving Objects in Java

Sat Apr 09, 2011 2:30 am

In my ongoing quest to get a handle on Java, I decided to undertake a personal project involving objects (dots) moving across a window. We've been discussing mobility models for MANETs in my networking class and it suggested to me that trying to implement the Random Waypoint mobility model (the most basic of them) would be a good exercise. The algorithm is as follows:

1). Pick a random position in on the map, and jump to it.
2). Loop:

- Pause for P0 seconds.
- Pick a random position X on the map.
- Pick a random speed s that is between some minimum (usually 0) and some maximum.
- Move towards X at constant speed s.

3). Jump to step 2.

So I've been taking this in steps. First figuring out how to draw multiple objects to the screen, then figuring how to get those objects to move around. So far it works, except the objects simply teleport around the screen rather than actually moving to their next location at some constant speed. The code I have so far, after initializing all the objects/placing them on the map, is as follows:

http://pastebin.com/5X2KUck2

Now this is clearly pretty damn basic, as the object is merely moved a pixel at a time until it reaches its destination. However, the way it is now its just a bunch of dots teleporting around the screen. For what I want, I know that the parameters I'll need are pause time and speed in pixels/millisecond. I also know I'll need linear interpolation and the passage of time (the difference between when the loop started and the current time) to determine how far the dot should move at any given point. I've made several attempts so far but I only get a program that hangs forever. So I'm making this post now in the hopes I can be steered in the right direction. If you need any more information, I'll be more than happy to provide it.
Under Construction Forever~~~
 
UltimateImperative
Gerbil First Class
Posts: 138
Joined: Sat Sep 19, 2009 12:48 am
Location: The Enn See Arrr, Central-Eastern Canuckisthan.

Re: Moving Objects in Java

Sat Apr 09, 2011 4:38 pm

IIRC, we usually did this using sort sort of Clock object, that would send a tick every N milliseconds. Every time the clock ticks, you send an update() message to every object in the World. For a point, update() would calculate its new position, given its last position, its speed and the time elapsed since the last update(). Then you send a draw() message to every object so that it draws itself on the canvass.

I haven't touched that in the last ten years, but that's the gist of what we did. You might want to look up the "MVC" pattern.
i5-750 @ 3.3 GHz; Asus P7P55D-E Pro; Thermalright Silver Arrow; Antec P183; 4 GB; 2x Radeon HD 6950; Corsair HX650.
 
Kurotetsu
Gerbil Elite
Topic Author
Posts: 548
Joined: Sun Dec 09, 2007 12:13 pm

Re: Moving Objects in Java

Sat Apr 09, 2011 5:17 pm

UltimateImperative wrote:
IIRC, we usually did this using sort sort of Clock object, that would send a tick every N milliseconds. Every time the clock ticks, you send an update() message to every object in the World. For a point, update() would calculate its new position, given its last position, its speed and the time elapsed since the last update(). Then you send a draw() message to every object so that it draws itself on the canvass.

I haven't touched that in the last ten years, but that's the gist of what we did. You might want to look up the "MVC" pattern.


I think Timer and TimerTask might be what you're talking about? I just discovered it and it could be exactly what I need, and it appears to be what you're talking about.
Under Construction Forever~~~
 
UltimateImperative
Gerbil First Class
Posts: 138
Joined: Sat Sep 19, 2009 12:48 am
Location: The Enn See Arrr, Central-Eastern Canuckisthan.

Re: Moving Objects in Java

Sun Apr 10, 2011 5:56 pm

Sorry about the misdirection; What you want is really a game loop. Since your model is so simple, you could just use a fixed fps.
i5-750 @ 3.3 GHz; Asus P7P55D-E Pro; Thermalright Silver Arrow; Antec P183; 4 GB; 2x Radeon HD 6950; Corsair HX650.
 
Kurotetsu
Gerbil Elite
Topic Author
Posts: 548
Joined: Sun Dec 09, 2007 12:13 pm

Re: Moving Objects in Java

Fri Apr 22, 2011 1:11 am

Most of you probably already know all about this, but I found a pretty nice tutorial on game programming in Java (it helps that the tutorial is teaching exactly what I want to do):

http://www.ntu.edu.sg/home/ehchua/progr ... Balls.html

Very informative.
Under Construction Forever~~~

Who is online

Users browsing this forum: No registered users and 1 guest
GZIP: On