I came across baxslash's 2D to 3D demo and I thought that would be great to play around with
so I create a plain and 4 box sides and several balls im trying to get the balls to bounce off the walls
SetPhysicsDebugOn()
#constant DeviceWidth GetDeviceWidth()
#constant DeviceHeight GetDeviceHeight()
SetWindowTitle( "3D Stuff" )
SetScreenResolution( DeviceWidth, DeviceHeight ) `Set the size of the application window
SetVirtualResolution( DeviceWidth, DeviceHeight ) `Set the "virtual" screen size
SetPrintSize( 24 )
Create3DPhysicsWorld()
// create a render image to render 2D scene onto
renderImage = CreateRenderImage( 1024, 512, 0, 0 ) `Grab the render image
`The resolution rendered depends on the size of this image
// create a 3D box to draw to
box = CreateObjectPlane( DeviceWidth, DeviceHeight ) `Create a plane object to apply the rendered image to
SetObjectPosition( box, DeviceWidth*.5, DeviceHeight*0.5, 0 ) `Set it's position
SetObjectImage( box, renderImage, 0 )
Create3DPhysicsStaticBody(box)
SetObject3DPhysicsRestitution(box,1)
SetObjectLightMode( box,0 ) `Apply the render image to the object (no need to re-apply it)
leftSide=CreateObjectBox(10,DeviceHeight,100)
SetObjectPosition(leftSide,(DeviceWidth*0.5)+5,0,5)
SetObjectColor(leftSide,255,255,255,255)
Create3DPhysicsStaticBody(leftSide)
SetObject3DPhysicsRestitution(leftSide,1)
FixObjectToObject( leftSide,box )
rightSide=CreateObjectBox(10,DeviceHeight,100)
SetObjectPosition(rightSide,-(DeviceWidth*0.5)-5,0,5)
SetObjectColor(rightSide,255,255,255,255)
Create3DPhysicsStaticBody(rightSide)
SetObject3DPhysicsRestitution(rightSide,1)
FixObjectToObject( rightSide,box )
topSide=CreateObjectBox(DeviceWidth+20,10,100)
SetObjectPosition(topSide,0,(DeviceHeight*0.5)+5,5)
SetObjectColor(topSide,255,255,255,255)
Create3DPhysicsStaticBody(topSide)
SetObject3DPhysicsRestitution(topSide,1)
FixObjectToObject(topSide,box )
bottomSide=CreateObjectBox(DeviceWidth+20,10,100)
SetObjectPosition(bottomSide,0,-(DeviceHeight*0.5)-5,5)
SetObjectColor(bottomSide,255,255,255,255)
Create3DPhysicsStaticBody(bottomSide)
SetObject3DPhysicsRestitution(bottomSide,1)
FixObjectToObject(bottomSide,box )
cx#=DeviceWidth*.5
cy#=DeviceHeight*.5
cz#=DeviceHeight
SetCameraPosition( 1, cx#, cy#, cz# )
SetCameraLookAt( 1, cx#, cy#, 0, 0 )
//SetCameraRange( 1, 1, DeviceHeight*4 )
// create some Physics Sprites
// first set the physics scale
SetPhysicsScale( 0.01 )
img=createEllipse()
img2=createCatcher()
img3=createTexture(32,32, makecolor(100,100,100),155)
CreateSprite(201,0)
CreateSprite(202,0)
SetSpriteSize(201,20,200)
SetSpriteSize(202,20,200)
SetSpriteColor(201,0,255,0,255)
SetSpriteColor(202,255,0,0,255)
SetSpritePosition(201,DeviceWidth-25,5)
SetSpritePosition(202,DeviceWidth-25,5)
for i=1 to 100
//CreateSprite(i,0 )
diameter=random(20,30)
CreateObjectSphere(i,diameter,5,5)
SetObjectColor(i,Random(20,255),Random(20,255),Random(20,255),255)
x=Random(0,DeviceWidth):y=Random(0,DeviceHeight )
SetObjectPosition(i,x,y,diameter*0.5)
Create3DPhysicsDynamicBody(i)
SetObject3DPhysicsRestitution(i,1)
SetObject3DPhysicsCanSleep(i,0)
SetObjectShapeSphere(i, diameter )
next
spr = CreateSprite( 0 )
size=100
SetSpriteSize( spr,size,size )
SetSpriteImage(spr,img2)
SetSpriteColor( spr,255,255,255,255)
x=DeviceWidth/2:y=DeviceHeight/2
totalnum=4
SetSpritePhysicsOn(spr,1)
setSpriteShape(spr,0)
AddSpriteShapeBox(spr,-(size/2),-(size/2),-(size/2)+5,size/2,0 )
AddSpriteShapeBox(spr,-(size/2),(size/2)-5,(size/2),size/2,0 )
AddSpriteShapeBox(spr,(size/2)-5,-(size/2),(size/2),size/2,0 )
SetSpritePositionByOffset( spr,x,y )
spr = CreateSprite( 0 )
sizeX=90:sizeY=10
SetSpriteSize( spr,sizeX,SizeY )
SetSpriteImage(spr,img3)
SetSpriteColor(spr,0,0,0,255)
SetSpriteShapeBox(spr,-44,-14,44,14,0)
SetSpritePositionByOffset(spr,x,y+40)
SetSpritePhysicsOff(spr)
counter=0:ammount=0:Time#=0
// main game loop
do
Print( "Make the balls fall in box by using the gyroscope" )
//RotateObjectLocalX(box,moveY#*.026 )
//RotateObjectLocalZ(box,moveX#*.026 )
elapsedTime#=timer()-time#
if ammount<=getSpriteHeight(201) and elapsedTime#>=.25
inc ammount:time#=timer()
SetSpriteSize(202,20,ammount)
else
if ammount>=getSpriteHeight(201)
//timer ran out
endif
endif
if counter<50
// Get a value to represent how much the device is tilting in the X direction
//dirx# = dirx#+GetDirectionX() * 5.0
//dx# = dirx# - moveX#
//moveX# = moveX# + (dx#*0.1)
// Apply the mouse movement to the object
//SetObjectRotation( box, 0,0, moveX# )
for i = 1 to 100
if GetSpriteCollision(i,Spr)=1 and getSpriteVisible(i)=1 //sprite has collided with new sprite
SetSpriteVisible(i,0):SetSpritePhysicsOff(i)
SetSpritecolor(spr,Random(20,255),Random(20,255),Random(20,255),255)
inc counter
if mod(counter,5)=0
sizeY=sizeY+4
SetSpriteSize(spr,sizeX,SizeY )
SetSpritePositionByOffset(spr,x,y+45-(sizeY/2))
endif
endif
next i
else
//container is full spin box
dirx# = dirx#+4
dx# = dirx# - moveX#
moveX# = moveX# + (dx#*0.1)
SetObjectRotation( box,0,0, moveX# )
for i=1 to 100
SetSpriteVisible(i,1):SetSpritePhysicsOn(i,2)
next i
if GetObjectAngleZ(box)>175 and GetObjectAngleZ(box)<185 //if box has emptied the balls then
counter=0::sizeY=10:ammount=0:time#=timer()
SetSpriteSize(spr,sizeX,SizeY )
SetSpritePositionByOffset(spr,x,y+45-(sizeY/2))
endif
endif
//SetPhysicsGravity(Sin(moveX#)*DeviceWidth,Cos(moveX#)*DeviceWidth )
dirx# =dirx#+GetDirectionX() * 5.0:diry#=diry#+GetDirectionY() * 5.0
dx# = dirx# - moveX#:dy# = diry# - moveY#
moveX# = moveX# + (dx#*0.1):moveY# = moveY# + (dy#*0.1)
x#=x#+GetDirectionX()*5.0:y#=y#+GetDirectionY()*5.0
//SetPhysicsGravity(x#,y# )
Set3DPhysicsGravity(x#,y#,0)
Step3DPhysicsWorld() //required for updating physics.
// First Render 2D scene to an image
depthImage = -1 `not required as we are not rendering depth, just a 2D scene
SetRenderToImage( renderImage, depthImage ) `This tells AGK to render only to the render image applied to the box as a texture
SetClearColor( 128, 128, 128 ) `Setting the clear / background colour for the 2D render
ClearScreen() `Clear the render image before rendering
Update2D( GetFrameTime() ) `Update 2D scene (including physics)
Render2DFront() `Render only 2D front items (in this case just our skeleton)
// Now Render 3D
SetRenderToScreen() `This tells AGK to render back to the screen
SetClearColor( 0, 0, 0 ) `Setting the clear colour for the final 3D render to white
Update3D( GetFrameTime() ) `Update 3D objects only
Render3D() `Render only 3D objects (our box)
Swap() `Send the final render to the screen
ClearScreen() `Clear the screen ready for the next loop
loop
end
function createEllipse()
Render()
DrawEllipse(50,50,25,25,MakeColor(255,255,255),MakeColor(100,100,100),1)
Swap()
img = getimage(0,0,100,100)
sync()
endfunction img
function createCatcher()
SetClearColor(0,0,0)
Render()
DrawBox(0,0,5,100,MakeColor(255,255,255),MakeColor(255,255,255),MakeColor(255,255,255),MakeColor(255,255,255),1)
DrawBox(95,0,100,100,MakeColor(255,255,255),MakeColor(255,255,255),MakeColor(255,255,255),MakeColor(255,255,255),1)
DrawBox(0,95,100,100,MakeColor(255,255,255),MakeColor(255,255,255),MakeColor(255,255,255),MakeColor(255,255,255),1)
Swap()
img = getimage(0,0,100,100)
sync()
endfunction img
function createTexture(sizex# as float, sizey# as float, color, density as integer)
swap()
drawbox(0,0,sizex#, sizey#, color, color,color,color, 1)
render()
img = getimage(0,0,sizex#, sizey#)
memblockid = CreateMemblockFromImage (img)
imgwidth = GetMemblockInt(memblockid, 0)
imgheight = GetMemblockInt(memblockid, 4)
size=GetMemblockSize(memblockid)
for offset=12 to size-4 step 4
r=GetMemblockByte(memblockid, offset)
g=GetMemblockByte(memblockid, offset+1)
b=GetMemblockByte(memblockid, offset+2)
a=GetMemblockByte(memblockid, offset+3)
strength=random(1,density)
SetMemblockByte (memblockid, offset, r-strength)
SetMemblockByte (memblockid, offset+1, g-strength)
SetMemblockByte (memblockid, offset+2, b-strength )
SetMemblockByte (memblockid, offset+3, a-strength)
next
deleteimage (img)
img = CreateImageFromMemblock(memblockid)
DeleteMemblock(memblockid)
endfunction img
The balls move with the arrow keys which I plan later to set just with the gyro Set3DPhysicsGravity(GetDirectionX(),GetDirectionY(),0)
instead of how I got it to test under windows. At the moment the balls move and the collisions seem to work with each other but not
the static objects on the side. Which i want to use as boundaries to keep them in place i may later add a top invisible plane to keep the
balls from escaping and i may do something fance with the plane thats currently theer ATM
Thanks in Advance for any help
fubar