问题描述:

I have been working on just the fake 3D engine of my game for days now and can't seem to create the view in the video below. I think I have figured out how to scale the sprites correctly. I can't seem to figure out how to rotate the world sprites. I can only slide world sprites on the x axis to the left or right depending on which player object rotation keys are used. I'm a but confused on how to duplicate how the video does full 360 rotation so the player can see objects in front or behind, and how to actually make the world objects look like they are rotating.

Once my facingDir variable hits 360 or -360 it gets reset to 0. I'm not sure if this is part of the problem, but I don't see any other way to fake a full 360 rotation and have accurate readings.

I only want to use 2D math / functions for this project.

View script used by world objects

objectSprite=argument0

//Draw

if x>obj_player.x //Draws sprite if in front of player

{draw_sprite_ext(argument0,0,x+obj_player.facingDir,

sprite_height+400,obj_player.scaling,obj_player.scaling,0,c_white,1)}

//Scaling Size Limit

if obj_player.scaling>=4

{obj_player.scaling=4}

else if obj_player.scaling<=1

{obj_player.scaling=1}

Code used by world objects to access view script

script_execute(scr_view,spr_SPRITE_NAME)

Player create event code

facingDir=0

scaling=0

viewSector=1

Player step event code

//Restart Game

if keyboard_check_pressed(vk_tab)

{game_restart()}

//Rotate Limit

if facingDir>360

{facingDir=0}

else if facingDir<-360

{facingDir=0}

//Rotate Left - Rotates Right View - Negative facingDir

if keyboard_check(ord('A'))

{facingDir-=2}

//Rotate Right - Rotates Left View - Positive facingDir

if keyboard_check(ord('D'))

{facingDir+=2}

//Move Forwards

if keyboard_check(ord('W'))

{motion_set(facingDir,4)

scaling+=0.2}

//Move Backwards

if keyboard_check(ord('S'))

{motion_set(facingDir,-4)

scaling-=0.2}

//Stop Movement

if keyboard_check_released(ord('W')) or keyboard_check_released(ord('S'))

{speed=0}

//View Sector Tracking - Positive

//Positive - North

if facingDir>0 and facingDir<45

{viewSector=1}

//Positive - North West

if facingDir>292.5 and facingDir<315

{viewSector=2}

//Positive - West

if facingDir>225 and facingDir<270

{viewSector=3}

//Positive - South West

if facingDir>202.5 and facingDir<225

{viewSector=4}

//Positive - South

if facingDir>135 and facingDir<180

{viewSector=5}

//Positive - South East

if facingDir>112.5 and facingDir<135

{viewSector=6}

//Positive - East

if facingDir>45 and facingDir<90

{viewSector=7}

//Positive - North East

if facingDir>22.5 and facingDir<67.5

{viewSector=8}

//View Sector Tracking - Negative

//Negative - North

if facingDir<0 and facingDir>-45

{viewSector=1}

//Negative - North East

if facingDir<-22.5 and facingDir>-67.5

{viewSector=2}

//Negative - East

if facingDir<-45 and facingDir>-90

{viewSector=3}

//Negative - South East

if facingDir<-112.5 and facingDir>-135

{viewSector=4}

//Negative - South

if facingDir<-135 and facingDir>-180

{viewSector=5}

//Negative - South West

if facingDir<-202.5 and facingDir>-225

{viewSector=6}

//Negative - West

if facingDir<-225 and facingDir>-270

{viewSector=7}

//Negative - North West

if facingDir<-292.5 and facingDir>-315

{viewSector=8}

//

image_angle=facingDir

Player draw GUI code

//Draw Labels and Variables

draw_text(20,0,'Direction')

draw_text(150,20,'X')

draw_text(200,20,x)

draw_text(300,20,'Y')

draw_text(350,20,y)

draw_text(20,20,facingDir)

draw_text(20,40,'View Sector')

draw_text(20,60,viewSector)

//View Sectors

if viewSector=1

{draw_text(160,40,'North')}

else if viewSector=2

{draw_text(160,40,'North East')}

else if viewSector=3

{draw_text(160,40,'East')}

else if viewSector=4

{draw_text(160,40,'South East')}

else if viewSector=5

{draw_text(160,40,'South')}

else if viewSector=6

{draw_text(160,40,'South West')}

else if viewSector=7

{draw_text(160,40,'West')}

else if viewSector=8

{draw_text(160,40,'North West')}

Fake 3D Test: https://www.youtube.com/watch?v=wuUXPRzPC3E

Project File: http://www.megafileupload.com/tjnN/Raycasting.gmx.zip

相关阅读:
Top