I'm working on a little demo, and this is the first time that I've really used Dark Physics.
Here's some code, so you can see what I'm doing currently:
autocam off
sync on
sync rate 60
make light 1
set directional light 1, -5, -5, 5
position camera 0,0,-50
point camera 0,0,-50
global getWorldX#
global getWorldY#
global getWorldZ#
phy start
` backdrop
MAKE OBJECT plain 1,50,50,50,50
COLOR OBJECT 1, rgb(255,0,0)
ROTATE OBJECT 1,-90,0,0
POSITION OBJECT 1,0,0,0
SET OBJECT wireframe 1,1
` create outer wall
create_outer_wall()
do
` create solid block with left mouse click
if (mouseclick() = 1)
Repeat : Until mouseclick()=0
create_block()
endif
` drop ball with right mouse click
if (mouseclick() = 2)
Repeat : Until mouseclick()=0
drop_ball()
endif
` rotate camera anti-clockwise
if leftkey()
ZROTATE CAMERA 0, CAMERA ANGLE Z (0) - 1
endif
` rotate camera clockwise
if rightkey()
ZROTATE CAMERA 0, CAMERA ANGLE Z (0) + 1
endif
` update gravity - the bit I need help with
` PHY SET gravity x#, y#, z#
` update the physics and the screen
PHY UPDATE
sync
loop
function get_world_coords()
getWorldObjNum#=pick object(mousex(),mousey(),1,200)
if getWorldObjNum#=0
text 0,35, "You are not pointing at any object"
else
pd#=get pick distance()
text 0,45, "You are pointing at object: "+STR$(getWorldObjNum#)
px#=get pick vector x()
py#=get pick vector y()
pz#=get pick vector z()
getWorldX#=px#+camera position x()
getWorldY#=py#+camera position y()
getWorldZ#=pz#+camera position z()
Text 0,55, "World: X = "+STR$(getWorldX#)
text 0,65, "World: Y = "+STR$(getWorldY#)
text 0,75, "World: Z = "+STR$(getWorldZ#)
text 0,85, "Mouse: X = "+STR$(mousex())
text 0,95, "Mouse: Y = "+STR$(mousey())
endif
endfunction
function create_outer_wall()
local blockToCreate as integer
local blockToCreate2 as integer
for x = 0 to 50
` "top"
blockToCreate = FIND FREE object (10,200)
MAKE OBJECT box blockToCreate, 1,1,1
COLOR OBJECT blockToCreate, rgb(255,255,0)
POSITION OBJECT blockToCreate, x-25, 25, 0
phy make rigid body static box blockToCreate
` "bottom"
blockToCreate2 = FIND FREE object (10,200)
MAKE OBJECT box blockToCreate2, 1,1,1
COLOR OBJECT blockToCreate2, rgb(0,255,255)
POSITION OBJECT blockToCreate2, x-25, -25, 0
phy make rigid body static box blockToCreate2
next
for y = 0 to 48
` "left"
blockToCreate = FIND FREE object (10,200)
MAKE OBJECT box blockToCreate, 1,1,1
POSITION OBJECT blockToCreate, -25, y-24, 0
COLOR OBJECT blockToCreate, rgb(0,255,0)
phy make rigid body static box blockToCreate
` "right"
blockToCreate2 = FIND FREE object (10,200)
MAKE OBJECT box blockToCreate2, 1,1,1
POSITION OBJECT blockToCreate2, 25, y-24, 0
COLOR OBJECT blockToCreate2, rgb(0,0,255)
phy make rigid body static box blockToCreate2
next
endfunction
function create_block()
local blockToCreate as integer
local xpos as integer
local ypos as integer
blockToCreate = FIND FREE object (201,301)
MAKE OBJECT box blockToCreate, 1,1,1
xpos=round (getWorldX#)
ypos=round (getWorldY#)
POSITION OBJECT blockToCreate, xpos-0.5, ypos-0.5, 0
phy make rigid body static box blockToCreate
endfunction
function drop_ball()
local ballToCreate as integer
ballToCreate = FIND FREE object (301,320)
MAKE OBJECT sphere ballToCreate, 1
POSITION OBJECT ballToCreate, getWorldX#, getWorldY#, 0
phy make rigid body dynamic sphere ballToCreate
endfunction
function round(number#)
`rounds a number up or down depending on if the first decimal is over .5 or not.
fraction#=number#-int(number#)
if fraction#>0.1 then result=int(number#+1) else result=int(number#)
endfunction result
The plan is to allow the user to left click on the grid to create a line of little blocks. Then right click above them to drop a ball. You should then be able to use the left and right arrow keys to rotate the scene, and the ball will roll down the line of blocks.
For this to happen, gravity constantly needs to be re-vectored so that it points "down", and this is the bit that I don't really understand. I'm pretty sure I won't need a Z component, as there will be no 'in-and-out', I just need to get the X and Y component.
Any help on how to work this out would be appreciated.