Skip to content

Commit fe36195

Browse files
committed
Merge pull request #568 from LennyPenny/master
ported 3d physics examples to lua
2 parents 167c265 + 53d96c6 commit fe36195

File tree

25 files changed

+423
-0
lines changed

25 files changed

+423
-0
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" ?>
2+
<PolycodeProject defaultWidth="640" defaultHeight="480" antiAliasingLevel="0" entryPoint="Scripts/Main.lua" textureFiltering="linear" vSync="false" anisotropyLevel="0" frameRate="60">
3+
<backgroundColor red="0.25" green="0.25" blue="0.25" />
4+
<packedItems>
5+
<item type="folder" path="Resources" />
6+
<item type="folder" path="Scripts" />
7+
</packedItems>
8+
</PolycodeProject>
3.08 KB
Loading
3.12 KB
Loading
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
scene = PhysicsScene(0, Vector3(200, 200, 200))
2+
3+
ground = ScenePrimitive(ScenePrimitive.TYPE_PLANE, 10, 10)
4+
ground:loadTexture("Resources/green_texture.png")
5+
scene:addPhysicsChild(ground, 6, 0.0);
6+
7+
for i = 1, 100 do
8+
local box = ScenePrimitive(ScenePrimitive.TYPE_BOX, 0.5, 0.5, 0.5)
9+
box:loadTexture("Resources/pink_texture.png")
10+
box:Roll(-45 + math.random() % 90)
11+
box:Pitch(-45 + math.random() % 90)
12+
box:setPosition(-2 + math.random() % 4, i*0.5, -2 + math.random() % 4)
13+
scene:addPhysicsChild(box, 0, 1.0)
14+
end
15+
16+
scene:getDefaultCamera():setPosition(7, 7, 7)
17+
scene:getDefaultCamera():lookAt(Vector3(0, 0, 0), Vector3(1, 1, 1))
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" ?>
2+
<PolycodeProject defaultWidth="640" defaultHeight="480" antiAliasingLevel="0" entryPoint="Scripts/Main.lua" textureFiltering="linear" vSync="false" anisotropyLevel="0" frameRate="60">
3+
<backgroundColor red="0.25" green="0.25" blue="0.25" />
4+
<polyarray:packedItems>
5+
<item type="folder" path="Resources" />
6+
<item type="folder" path="Scripts" />
7+
</polyarray:packedItems>
8+
</PolycodeProject>
3.08 KB
Loading
3.12 KB
Loading
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
scene = PhysicsScene(0, Vector3(200, 200, 200))
2+
3+
ground = ScenePrimitive(ScenePrimitive.TYPE_PLANE, 10, 10)
4+
ground:loadTexture("Resources/green_texture.png")
5+
6+
scene:addPhysicsChild(ground, 6, 0)
7+
8+
for i = 1, 1 do
9+
local box = ScenePrimitive(ScenePrimitive.TYPE_BOX, 0.8, 0.8, 0.8)
10+
box:loadTexture("Resources/pink_texture.png")
11+
box:Roll(-45 + math.random() % 90)
12+
box:Pitch(-45 + math.random() % 90)
13+
box:setPosition(-2 + math.random() % 4, i * 0.5, -2 + math.random() % 4)
14+
15+
scene:addPhysicsChild(box, 0, 1)
16+
end
17+
18+
player = ScenePrimitive(ScenePrimitive.TYPE_BOX, 0.5, 1, 0.5)
19+
player:loadTexture("Resources/pink_texture.png")
20+
player:setColor(1, 1, 0, 1)
21+
player:setPosition(2, 1, 2)
22+
23+
playerController = scene:addCharacterChild(player, 10, 1, 0.5)
24+
local walkSpeed = 0
25+
local rotateSpeed = 0
26+
local playerDirection = 0
27+
28+
testBox = ScenePrimitive(ScenePrimitive.TYPE_BOX, 2, 2, 2)
29+
testBox:loadTexture("Resources/pink_texture.png")
30+
testBox:setColor(0.3, 0.5, 1, 0.4)
31+
testBox:setPosition(2,1,-2)
32+
33+
scene:addCollisionChild(testBox, 0)
34+
35+
hud = Scene(Scene.SCENE_2D_TOPLEFT)
36+
onGroundLabel = SceneLabel("Arrow keys to control, spacebar to jump, press R to reset", 16)
37+
onGroundLabel:setAnchorPoint(Vector3(-1, -1, 0))
38+
onGroundLabel:setPosition(0,0)
39+
hud:addChild(onGroundLabel)
40+
onGroundLabel = SceneLabel("On Ground:", 16)
41+
onGroundLabel:setAnchorPoint(Vector3(-1, -1, 0))
42+
onGroundLabel:setPosition(0, 32)
43+
44+
hud:addChild(onGroundLabel)
45+
46+
47+
scene:getDefaultCamera():setPosition(7, 7, 7)
48+
scene:getDefaultCamera():lookAt(Vector3(0, 0, 0), Vector3(1, 1, 1))
49+
50+
function handleKeyEvent(t, e)
51+
if not e:getDispatcher() == CoreServices.getInstance():getCore():getInput() then return end
52+
53+
local inputEvent = safe_cast(e, InputEvent)
54+
55+
local eventKeyCode = e:getEventCode()
56+
if eventKeyCode == InputEvent.EVENT_KEYDOWN then
57+
local keyCode = inputEvent:keyCode()
58+
59+
if keyCode == KEY_r then
60+
playerController:warpCharacter(Vector3(2, 1, 2))
61+
elseif keyCode == KEY_UP then
62+
walkSpeed = 0.05
63+
elseif keyCode == KEY_DOWN then
64+
walkSpeed = -0.05
65+
elseif keyCode == KEY_LEFT then
66+
rotateSpeed = 3
67+
elseif keyCode == KEY_RIGHT then
68+
rotateSpeed = -3
69+
elseif keyCode == KEY_SPACE then
70+
playerController:jump()
71+
end
72+
elseif eventKeyCode == InputEvent.EVENT_KEYUP then
73+
if inputEvent.key == KEY_DOWN then
74+
walkSpeed = 0
75+
elseif inputEvent.key == KEY_RIGHT then
76+
rotateSpeed = 0
77+
end
78+
end
79+
end
80+
81+
CoreServices.getInstance():getCore():getInput():addEventListener(nil, handleKeyEvent, InputEvent.EVENT_KEYDOWN)
82+
CoreServices.getInstance():getCore():getInput():addEventListener(nil, handleKeyEvent, InputEvent.EVENT_KEYUP)
83+
84+
function Update(elapsed)
85+
playerDirection = playerDirection + rotateSpeed * elapsed
86+
87+
player:setYaw(playerDirection * (180 / math.pi))
88+
playerController:setWalkDirection(Vector3(walkSpeed * math.cos(playerDirection), 0, walkSpeed * math.sin(-playerDirection)))
89+
90+
if playerController:onGround() then
91+
onGroundLabel:setText("On Ground: YES")
92+
else
93+
onGroundLabel:setText("On Ground: NO")
94+
end
95+
96+
res = scene:testCollision(player, testBox)
97+
if res.collided then
98+
testBox:setColor(1, 1, 0, 0.5)
99+
else
100+
testBox:setColor(0, 1, 1, 0.5)
101+
end
102+
end
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" ?>
2+
<PolycodeProject defaultWidth="640" defaultHeight="480" antiAliasingLevel="0" entryPoint="Scripts/Main.lua" textureFiltering="linear" vSync="false" anisotropyLevel="0" frameRate="60">
3+
<backgroundColor red="0.25" green="0.25" blue="0.25" />
4+
<polyarray:packedItems>
5+
<item type="folder" path="Resources" />
6+
<item type="folder" path="Scripts" />
7+
</polyarray:packedItems>
8+
</PolycodeProject>
3.08 KB
Loading

0 commit comments

Comments
 (0)