This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#1 2007-08-30 06:55:34

dinther
Scratcher
Registered: 2007-08-22
Posts: 19

A real Ship Simulator on Scratch...

With graphics collected from Google Maps on the world port of Rotterdam in the Netherlands and some nifty programming I have been able to create a fun ship simulator that let's you steer a large vessel into the ports of Rotterdam.

I thought I share a little info on how the program has been build:

http://scratch.mit.edu/projects/dinther/32891

The ship simulation engine is rather primitive but still very challenging to control. The whole thing is based around a large lump of mass with a low friction but large inertia. The pivot point is placed forward of the ships center point. The ship is controlled by a propeller on a pivot which means that the stern can be pushed around without any forward speed. There are two parameters that control the ship. Power and Rudder. Power defines the amount of power generated by the engines and rudder defines the angle of the pivoting propeller.

Math: The applied power is split up in rotation and forward components. The lack of Sin and Cos is a bit of an issue but by limiting the propeller to 90 deg angles we can use a simple linear relationship instead. 1-(Rudder/90) gives us a crude factor we can use to multiply with the Power to get the amount of power that is contributing to forward motion while Rudder/90 gives us the factor that contributes to rotational motion. Speed and TurnRate are updated and then applied to the ships forward motion and rotation. Finally the ship needs to settle itself down. The all important friction should slow down the ship when the power is set to 0 and the ships rotation should gradually come to a stop when the rudder is 0.

The result is a rather fun and realistic looking ship simulation that is quite a challenge to control. Many small correction and patience is the key to good control. The program provides a readout for speed and turn-rate. Two very useful parameters that should be monitored closely.

Now we needed a scrolling background. We started out by studying the various scrolling solutions people have come up with in Scratch. We downloaded some, explored the code and ended up writing a slightly modified scrolling solution.

The Scrolling engine makes use of two variables ScrollX and ScrollY. Then these values change a message is send out of all the scroll tiles and each tile know it's position in relation to the ScrollX and ScrollY values. Tiles that are "off the screen" are hidden.

The Scroll amount is a tricky one. Scratch does not provide Sin or Cos operators which would be required to calculate X and Y scroll for a given ship direction but Scratch does allow us to set a sprite direction and move it along in that direction which essentially provides us with the Sin and Cos data. After the ship is moved the position is read and added to the ScrollX and ScrollY values and the scroll engine is updated. After this the ship is placed back at the center of the screen.

The graphics on the scroll tiles have been captured in Google maps zoomed in to the port of Rotterdam. Several screen captures were required which where merged together in a free graphics editor called Paint.Net  . The big job was to remove all the water and moving ships. Mostly a manual process. The water was replaced on a separate layer as a constant color.

Of course no sim is complete if you can't detect a collision. For this we use color detection function in scratch. The problem is that it will sense if any part of the ship is in the water but not that ALL of the ship is in the water. Therefore we created a new boundary layer in a slightly different water color using the boundary tools in Paint.net

The only thing left to do was the painting in the proposed course and a box of a lighter water color where the ship needs fit on. This "sensor pad" allows us to detect if the ship does NOT touch the water in which case the destination has been reached. This final image was experted as a bmp file and sliced up in to 25 equal tiles of 420 x 300 pixels.

This combined with the ship physics simulation becomes the core of the game. The aim is to steer the ship along it's set course into it's mooring place as indicated.

Last edited by dinther (2007-08-30 07:34:06)


Find out what it is like to steer large ships into the Port of Rotterdam in ShipSim http://scratch.mit.edu/projects/dinther/32891

Offline

 

#2 2007-08-30 11:51:40

Sawman3
Scratcher
Registered: 2007-07-19
Posts: 97

Re: A real Ship Simulator on Scratch...

Great game, great programming, and a very original way to think through a sim on Scratch.  bravo!


"These are the old days, the glory days...they're back."

Offline

 

#3 2007-08-31 02:23:26

kevin_karplus
Scratcher
Registered: 2007-04-27
Posts: 1000+

Re: A real Ship Simulator on Scratch...

There is a way to get sin and cos computation in scratch---see how I did it in the RPN calculator:

http://scratch.mit.edu/projects/kevin_karplus/2164

Offline

 

#4 2007-09-01 21:58:57

WiiRocks
Scratcher
Registered: 2007-08-24
Posts: 14

Re: A real Ship Simulator on Scratch...

It's A Great Game Well Done.

Offline

 

Board footer