3

How might I generalize the following WASD question such that it works with any Graphics3D object and includes q and e for z(up and down) movement? For example qweasd[gr_].

Please comment if the question is too general or needs editing. Current code.

William
  • 7,595
  • 2
  • 22
  • 70

1 Answers1

1

Here is my attempt.

Begin["esdf`"];
worldwidth = 300;
prmVIEWERHEIGHT = 2.5;
prmMOUSESENS = 0.01;
prmKEYSENS = 2;

pos = {-146.2715891902948, -154.52068004641723, 2.5}(*{0.,-200,prmVIEWERHEIGHT}*); moveDir = {0.6365371822219685, 0.7712460149971068, 0.}({0,1,0}); viewDir = moveDir; strafeDir = {0.7712460149971068, -0.6365371822219685, 0.`}({1,0,0});

cube = {Gray, EdgeForm[None], CapForm[None], Tube[{{-150, -150, 0}, {-50, 0, 0}, {50, 0, 0}}, 20], Blue // Lighter, Translate[ ExampleData[{"Geometry3D", "SpaceShuttle"}, "PolygonObjects"]~ Scale~3, {0, 0, 6}]}; floor = {Texture@ExampleData[{"ColorTexture", "GreenMarble"}], EdgeForm@None, Polygon[{{-w, -w, 0}, {-w, w, 0}, {w, w, 0}, {w, -w, 0}}, VertexTextureCoordinates -> {{0, 0}, {1, 0}, {1, 1}, {0, 1}}]} /. w -> worldwidth;

rotTrans := Function[x, Evaluate[RotationTransform[x, {0., 0., 1.}]]]; mp0 = MousePosition[];

mouse := (mousePos = MousePosition[]; {mouseDx, mouseDy} = mousePos - mp0; mp0 = mousePos; viewDir = RotationTransform[-prmMOUSESENS mouseDy, strafeDir]@viewDir; {moveDir, strafeDir, viewDir} = rotTrans[-prmMOUSESENS mouseDx] /@ {moveDir, strafeDir, viewDir};);

keys := Switch[CurrentValue["EventKey"], ".", pos += prmKEYSENS moveDir, "e", pos -= prmKEYSENS moveDir, "o", pos -= prmKEYSENS strafeDir, "i", pos += prmKEYSENS strafeDir];

actions = {"MouseMoved" :> mouse, "KeyDown" :> keys};

SetOptions[ CreateDocument[{Dynamic@ EventHandler[ Style[Graphics3D[{{Gray, EdgeForm@None, CapForm@None, Tube[{{-150, -150, 0}, {-50, 0, 0}, {50, 0, 0}}, 20], Blue // Lighter, Translate[ ExampleData[{"Geometry3D", "SpaceShuttle"}, "PolygonObjects"]~Scale~3, {0, 0, 6}]}, floor}, ViewVector -> {pos, pos + viewDir}, ViewRange -> {0.1, 300}, Lighting -> "Neutral", Boxed -> False, ImageSize -> Full, Background -> LightBlue, PlotRangePadding -> 0], Selectable -> False, Editable -> False], actions]}, CellMargins -> 0, ShowCellBracket -> False, "TrackCellChangeTimes" -> False, WindowElements -> {}, WindowFrame -> "Normal", WindowMargins -> Automatic], NotebookEventActions -> actions];

End[];

William
  • 7,595
  • 2
  • 22
  • 70