System Moderator


Posts: 65 Join date: 2009-05-09 Age: 14 Location: Florida
 | Subject: Basic 3D tutorial [Pro] Sat Jul 04, 2009 9:25 am | |
| Im totally bored so im making a quick tutorial on D3D... To make a 3D game you NEED gamemaker pro. You must also know a good amount of GML because all of 3D use script. Now, lets start. Create and object. It can have any sprite you want, But i perfer you use a ball sprite, 16x16, and make sure it is centered. Now back to the object. In the "Step Event" create this code. | Code: | if keyboard_check(ord('A')) {direction += 3;} if keyboard_check(ord('D')) {direction -= 3;} if keyboard_check(ord('W')) {if (speed < 2) speed = min(2,speed+0.4);} if keyboard_check(ord('S')) {if (speed > -2) speed = max(-2,speed-0.4);}
|
this will make you character turn directions with A and D, and you can go foward of backwards with W and S.if you want to use the arrows keys then remove the ord('') part and replace it with vk_left,vk_right,vk_up and vk_down.
Now for the actually 3D... place this code in the "Create Event" for you player...
| Code: | { friction = 0.2; d3d_start(); d3d_set_fog(true,background_color,300,495) }
|
Now your game is in 3D mode. But it doesnt really look 3D yet... Now place this code in the "Draw Event" for your object.
| Code: | { d3d_set_projection(x,y,10, x+cos(direction*pi/180),y-sin(direction*pi/180),10, 0,0,1); draw_set_alpha(1); draw_set_color(c_white); d3d_draw_floor(0,0,0,room_height,room_width,0,background_get_texture(TEXTURE GOES HERE),room_height,room_width/19); }
|
the alpha must be 1 and the color must be white. the projection is the code that allows you to see in the room. the last code, that is creating the floor in which you walk apon.
Now lets make walls... making Flat walls gets very annoying... So we are going to make our "Walls" 16x16 boxes... create a square sprite, 16x16.Center it.Now create another object, we can call it obj_wall; anyways, in the "Draw Event" for obj_wall place this code.
| Code: | if (point_distance(x,y,obj_player.x,obj_player.y) > 550) exit; d3d_draw_block(x-8,y-8,0,x+8,y+8,16,background_get_texture(TEXTURE GOES HERE),1,1)
|
This code will only draw the box when the player is close enough to see it. this will reduce lag.
Now for the collision...Collision is a little different...put this code in the collision event for the player to the wall...
| Code: | x=xprevious; y=ypervious;
|
When you collide with the wall you are not colliding with the wall you can see. you are colliding with the sprite under it. the sprite you gave to the the wall is set under it. the wall is 16x16 and so is the sprite, so the sprite is the exact length as the wall. you you had set a much bigger sprite, you would collide with the wall before you touched the actually 3D wall.
Now, increase the dementions of the ROOM they objects are in to say...Larger than 640x480... Make the camera follow the object in which the projection function is held. in this case we make the camera follow the obj_player, NOT THE WALL. Make its Hbor=640 and its vbor=480. now the view in the room is equal to the dementions of the projection.
Now lets place something on the screen, say stats... or maybe a picture or something. in 2D you can simply do draw_text(x,y,string) but with 3D you first need to set an orthograph. first create ANOTHER object... call it..."object"...actually we will call it "fred".MAKE FREDS DEPTH -50, If any object is drawn with a depth lower than fed it will be placed on the screen, Like if you set the walls depth to -51, all the walls would be draw on the screen. In the draw event for "fred" place this code.
| Code: | { d3d_set_projection_ortho(0,0,640,480,0); d3d_set_hidden(false); draw_set_alpha(1); } |
Now to add text you put this code after Alpha(1); and before the } in the code above
| Code: | draw_text(0,0,"Hello world!")
|
DO NOT USE X,Y in codes that are setting sprites on the orthograph. Like most codes it is read from top to bottom...so if you create the code
| Code: | set_set_color(c_red)
|
the everything bellow this code would be red and everything above this code would be black, unless you set another color code before this one. try using this code in the "Draw Event" of "fred"
| Code: | { d3d_set_projection_ortho(0,0,640,480,0); d3d_set_hidden(false); draw_set_alpha(1); draw_set_color(c_red) draw_text(0,0,"This text is red") draw_set_color(c_blue) draw_text(0,16,"This text is blue") }
|
That is all I have time for. You can use d3d_draw_cone/ellipsiod/wall/cylinder and all that to create different 3D shapes. I hope that helped, if you want to know how or why any of these scripts do what they do, then you can PM me or post it here. If I left out any scripts that are key to 3D tell me. You'll be able to know when something is working correctly.
Last edited by System on Sun Jul 05, 2009 2:01 pm; edited 1 time in total |
|
SuperSonic064 Moderator


Posts: 64 Join date: 2009-05-05 Age: 16 Location: Canada
 | Subject: Re: Basic 3D tutorial [Pro] Sun Jul 05, 2009 11:06 am | |
| Pretty good tutorial. I see you forgot a line in the step event code, I'm sure there was meant to be an "if keyboard_check(ord('W'))" in there. |
|
System Moderator


Posts: 65 Join date: 2009-05-09 Age: 14 Location: Florida
 | Subject: Re: Basic 3D tutorial [Pro] Sun Jul 05, 2009 2:00 pm | |
| | SuperSonic064 wrote: | | Pretty good tutorial. I see you forgot a line in the step event code, I'm sure there was meant to be an "if keyboard_check(ord('W'))" in there. |
Yeah, my keyboards alt+C hasn't been working. Thanks. |
|