Discuss Scratch

meowmoo
Scratcher
500+ posts

How To Make A 3D Game

paskal06 wrote:

B1ue123 wrote:

I have a project that shows simple 3D. It isn't perfect by any means, but you might be able to do something with it.
The URL is http://scratch.mit.edu/projects/21572717/#player

That,s not 3D. 3D is something which you can see at all positions.
no, that is just wireframe 3D
nwolf2012
Scratcher
20 posts

How To Make A 3D Game

meowmoo wrote:

dm_games wrote:

it's very complicated maths. Scratch doesn't have a feature to change the sprites 3d rotation
not really, look:

meowmoo wrote:

actual script that will go to a 3D position that is rotatable based on two variables:

define3Dx:Xy:Yz:ZsetxtosinofturnX*X+sinofturnX*ZsetytocosofturnY*Y+cosofturnY*Z

whenclickedforeverset3Deffectto3D
meowmoo
Scratcher
500+ posts

How To Make A 3D Game

nwolf2012 wrote:

meowmoo wrote:

dm_games wrote:

it's very complicated maths. Scratch doesn't have a feature to change the sprites 3d rotation
not really, look:

meowmoo wrote:

actual script that will go to a 3D position that is rotatable based on two variables:

define3Dx:Xy:Yz:ZsetxtosinofturnX*X+sinofturnX*ZsetytocosofturnY*Y+cosofturnY*Z

whenclickedforeverset3Deffectto3D
what?
nwolf2012
Scratcher
20 posts

How To Make A 3D Game

meowmoo wrote:

nwolf2012 wrote:

meowmoo wrote:

dm_games wrote:

it's very complicated maths. Scratch doesn't have a feature to change the sprites 3d rotation
not really, look:

meowmoo wrote:

actual script that will go to a 3D position that is rotatable based on two variables:

define3Dx:Xy:Yz:ZsetxtosinofturnX*X+sinofturnX*ZsetytocosofturnY*Y+cosofturnY*Z

whenclickedforeverset3Deffectto3D
what?

That was just a joke.
thirdlion
Scratcher
20 posts

How To Make A 3D Game

guys I thank you! SO!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! MUCH FOR YOUR HELP!!!!!!!!!!!!!!!!!!! :{])
meowmoo
Scratcher
500+ posts

How To Make A 3D Game

thirdlion wrote:

guys I thank you! SO!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! MUCH FOR YOUR HELP!!!!!!!!!!!!!!!!!!! :{])
your welcome!
the_pumpkin_pi
Scratcher
100+ posts

How To Make A 3D Game

Just re-mix other peoples projects. I got my first fav and love by doing that.

define3Dforeversayjust copy,N,pastesayI'm always right on scratch :D
supercreeper2005
Scratcher
100+ posts

How To Make A 3D Game

Airtoum wrote:

I JUST WANT THE 3D FORMULA! I NEED IT! NOW!
Try this
whenup arrowkeypressedchangesizeby-10changeyby10changexby-3
1337_5CR47CH3R
New Scratcher
27 posts

How To Make A 3D Game

There's no “magic” 3D formula. It requires math no matter what.
The basics are:

Projection
If you have some point(s) with coordinates (x, y, z) then to project them onto the screen, you'll need to use some math. Consider the following (Bill Nye lol):

As you can see, it is a pinhole camera. Only light rays that go through the hole form the picture on the wall. Basically, this projects a 3D scene onto a 3D surface. We can model this to make 3D in scratch. Of course, no pinhole camera provides a perfectly sharp image because the pinhole has to be greater than 0 size. However, when we model this mathematically, we can make the pinhole be 0 size. Of course, there is another problem: the projected image is upside down! To fix this we move the viewing place in front of the pin, which doesn't make sense in real life, but works perfectly well mathematically and in code.

Ok, so now we have our viewing plane in front of the hole. The hole now becomes your eye, and the plane is the screen! Now all we need to do is figure out where stuff in 3D goes on the plane. Let's go back to the example of the pinhole camera. Each light ray that bounces from a distinct point in 3D space and goes through the pinhole gets projected onto the viewing plane. So all we need to do is draw some rays:

As you can see, the point (x, y, z) is now projected onto the plane at 2D point (x', y') (say: x prime, y prime). Using some basic triangle similarity, and assuming the lengths indicated, we can see that y' = y/z, and we can use the same argument to say x' = x/z
Of course, this assumes that the plane is square. What if it's not? Well, since Scratch is 480x360, we need to shorten the horizontal coordinates, x, to compensate. This just means before calculating x' = x/z, multiply x by 360/480, to shrink the scale along the x-axis to match the scale along the y-axis. Basically what this is doing is first dividing x by 480 to transform the range of x values, to then multiplying by 360 to transform the range to match the range of y values, .

Rotation
Translation is easy, all you need to do is add or subtract from the x, y, or z values to move along in some direction. Rotation is harder, however, and requires trigonometry. Rotation is defined by 2 axes, so in 3D there are 3 ways to rotate, rotation around the x-axis, y-axis, or z-axis. Every type works the same way, all you need to do is swap out x, y or y, z or x, z. To start, I'll deal with just changing x and y, aka rotating around the z-axis.

Here we have (x, y) which we want to transform to (x', y') (note: not the same x' and y' as in projection. x' and y' is just a system of notation).
So how do we rotate in these coordinates? It's kind of hard. Luckily, there's another type of coordinate system, polar coordinates. Instead of being defined by x and y, polar coordinates are defined by angle and radius. Take a look:

Whoa. Now all we need to do is increase the angle! But how do we change between cartesian (x, y) and polar (r, O) coordinates? Easy, use trigonometry. atan means arc-tangent, which is the inverse of tangent, which is sine / cosine. You should know this already.
There are 2 ways of expressing the inclination of a line. Slope (y/x) and angle(O). Tangent can help us convert between the two.
y/x = atan(O)
O = tan(y/x)
So to convert (x, y) to (r, O), the first step is O = atan(y/x)
Next is r, which is easy, according to the Pythagorean Theorem. So we have
r = sqrt(x*x + y*y)
O = atan(y/x)
Now we just increase the angle to make it O+a, where a is the angle you want to rotate by (important note: angles in polar coordinates are counterclockwise, so a positive a will be counterclockwise, and negative will be clockwise).
Now convert (r, O+a) back to cartesian coordinates. We can do this with the definition of sine and cosine.
x' = r*cos(O)
y' = r*sin(O)
Now let's put it all together:
x' = sqrt(x*x+y*y)*cos(atan(y/x)+a)
y' = sqrt(x*x+y*y)*sin(atan(y/x)+a)
Using trigonometry identities we have
x' = sqrt(x*x+y*y)*(cos(atan(y/x))*cos(a) - sin(atan(y/x))*sin(a))
y' = sqrt(x*x+y*y)*(sin(atan(y/x))*cos(a) + cos(atan(y/x))*sin(a))
sin(atan(m)) = m/sqrt(1+m*m) and cos(atan(m)) = 1/sqrt(1+m*m) so
x' = sqrt(x*x+y*y)*((1/sqrt((x*x + y*y)/x*x))*cos(a) - (y/(x*sqrt((x*x + y*y)/x*x)))*sin(a))
y' = sqrt(x*x+y*y)*((y/(x*sqrt((x*x + y*y)/x*x)))*cos(a) + (1/sqrt((x*x + y*y)/x*x))*sin(a))
take …/x*x and …/y*y out of the sqrt to make sqrt(…)/x and sqrt(…)/y and then cancel the sqrt(x*x+y*y) (if you don't get it just trust me on this
I don't want to type out all the steps in between because it's messy)
x' = ((1/1/x)*cos(a) - (y/(x/x))*sin(a))
y' = ((y/(x/x))*cos(a) + (1/1/x)*sin(a))
moar simplification gets us
x' = x*cos(a) - y*sin(a)
y' = y*cos(a) + x*sin(a)

Wow. Very neat and simple.
As I said, replace (x, y) with (x, z) or (y, z) to rotate around the y and x axes, respectively.

Conclusion
3D is a very complicated subject that involves a lot of math. I demonstrated the math behind 2 concepts: projection, and rotation. This should enable you to create some basic 3D (it's up to you to implement the math in code). You can rotate the 3D scene with the rotation math I proved, translate by adding or subtracting x, y, or z, and then project it onto the screen using the projection math I proved. If you need more help or don't understand, you can always make another forum post or do a google search. If you don't think you're ready to make 3D yet, by all means don't make 3D. 3D code can quickly get out of hand and you will end up not understanding anything. I hope you will eventually make great 3D projects though!

This concludes this tutorial.
HTH
nwolf2012
Scratcher
20 posts

How To Make A 3D Game

whenclickedgotox:131y:635z:121category=motionsethealthto100setmaplengthtopickrandom50to1000000category=lookssetmapwidthtopickrandom50to1000000category=lookssetmapheighttopickrandom50to1000000category=looksforeverfacetowardsmouse pointercategory=motionifkeywpressed?thenchangezby9category=motion ifkeyspressed?thenchangezby9category=motion ifkeyapressed?thenchangexby-9category=motion ifkeydpressed?thenchangexby9category=motion ifkeyspacepressed?thenjump10category=motion
Villager303
Scratcher
3 posts

How To Make A 3D Game

Try this XD

whenclickedgotox:10y:10z:10get3dmodelhttp://static.fjcdn.com/pictures/Well+hello+there....+Cake+or+pie_48e3b4_3636572.jpgsetcameraposx:0y:0z:0setcamerapointofinterestx:10y:10z:10render3dobjectschangecamreaposx:.5y:10z:-10endendend

Last edited by Villager303 (July 26, 2014 09:58:20)

beaple
Scratcher
16 posts

How To Make A 3D Game

people i will make history on scratch and make a good graphic 3d gameabout time stampy v.s 2 comes out i will make an advanced game
paskal06
Scratcher
17 posts

How To Make A 3D Game

Try this project. The framework 3d is so fast. http://scratch.mit.edu/projects/11731611/
WO997
Scratcher
64 posts

How To Make A 3D Game

Ok so i know how to make 3d projects just see my profile, formula for 3d is: X=virtualx/virtualz, Y=virtualy/virtualz SIZE OF SPRITE*=virtualsize/virtualz, any questions? *or pen
SNUGGINBUNNY
Scratcher
13 posts

How To Make A 3D Game

add3D Is Awesometo 3D Game
letsplayally
Scratcher
100+ posts

How To Make A 3D Game

Hey I gotta a beta 3D engine which is easy to use and it really looks like 3D!: http://scratch.mit.edu/projects/24874034/ If that doesn't work I can give you the mathematical formula (the hard way) but it is very very long and hard to make in scratch.
nwolf2012
Scratcher
20 posts

How To Make A 3D Game

SNUGGINBUNNY wrote:

add3D Is Awesometo 3D Game

Again, SNUGGINBUNNY..!
meowmoo
Scratcher
500+ posts

How To Make A 3D Game

@dadOfMrlog has an amazing 3D turoial: http://scratch.mit.edu/studios/202972/ and a 3D environment: http://scratch.mit.edu/projects/25217786/
epicsandwich123
Scratcher
100+ posts

How To Make A 3D Game

hey, no offence, but 3d is probably WAY to advanced for you! it's nothing like the 3d head you made. You'll need to use the pen tools. http://scratch.mit.edu/projects/25217786/

a great project, look at the script.

Powered by DjangoBB