You always do such great stuff Garbenjamin
I have been unsure what my next project will be but came across your thread and puzzlers globe algorithm
and created this 3d Variant of a Tower Defense not sure what il do with it yet and still need the turrets to
show their aim better. but basically im using puzzlers code for the globe as turret placement it will adventually
have a sphere in the centre with possibly a world texture
// Project: Towerdefense
// Created: 2018-08-22
// show all errors
SetErrorMode(2)
// set window properties
SetWindowTitle( "Tower defence" )
SetWindowSize( 1024, 768, 0 )
SetWindowAllowResize( 1 ) // allow the user to resize the window
// set display properties
SetVirtualResolution( 1024, 768 ) // doesn't have to match the window
SetOrientationAllowed( 1, 1, 1, 1 ) // allow both portrait and landscape on mobile devices
SetSyncRate( 30, 0 ) // 30fps instead of 60 to save battery
SetScissor( 0,0,0,0 ) // use the maximum available screen space, no black borders
UseNewDefaultFonts( 1 ) // since version 2.0.22 we can use nicer default fonts
#constant bulletLife = 1.5
#constant Depth#=10
type _boxes
id
x#
y#
z#
endtype
// the type used for bullets
type bullet
ID as integer
time as float // a time used because bullets die over time
endtype
global boxes as _boxes[]
global empty as _boxes
global myBullet as bullet[0]
global bulletCount=0
box=CreateObjectBox(.1,.1,.8)
SetObjectVisible(box,0)
r#=8
create3DPhysicsWorld()
boxnumber=0
for i#=-180 to 180 step 40 // change these for effect
for j#=-90 to 90 step 40 // change these for effect
x# = r# * sin(i#) * cos(j#)
y# = r# * sin(i#) * sin(j#)
z# = r# * cos(i#)
empty.id = InstanceObject(box)
empty.x#=x#
empty.y#=y#
empty.z#=z#
boxes.insert(empty)
SetObjectVisible(empty.id,1)
SetObjectPosition(boxes[boxnumber].id,boxes[boxnumber].x#,boxes[boxnumber].y#,boxes[boxnumber].z#)
SetObjectLookAt( boxes[boxnumber].id, 0, 0, 0, 0 )
SetObjectColor(boxes[boxnumber].id,random(0,255),random(0,255),random(0,255),255)
FixObjectToObject(boxes[boxnumber].id,box)
inc boxnumber
next
next
mouseBlock=CreateObjectBox(1,1,1)
do
//for num =0 to boxnumber-1
// SetObjectLookAt( boxes[num].id, GetObjectWorldX(mouseBlock),GetObjectWorldY(mouseBlock),GetObjectWorldZ(mouseBlock), 0 )
//next num
moveblockwithmouse(mouseBlock)
if GetPointerPressed() //shoot
for num =0 to boxnumber-1
if getDistance(boxes[num].id,mouseBlock)<15 then shootBullet( boxes[num].id,mouseBlock, 1000.0, 10.0)
next num
endif
for num=1 to bulletCount
if (myBullet[num].time)+bulletLife<timer()
DeleteObject(myBullet[num].ID)
myBullet.remove(num)
dec bulletCount
endif
next num
Step3DPhysicsWorld()
RotateObjectLocalX(box,1)
Print("Move Mouse they will aim at you")
Print("Press mouse button they will shoot")
Print( ScreenFPS() )
Sync()
loop
function shootBullet(objectId as integer,objectID2 as integer,initialSpeed as float, mass as float)
//To move dynamic physics bodies we need to apply velocity to the physics body.
gunPositionVec= CreateVector3(GetObjectWorldX(objectId),GetObjectWorldY(objectId),GetObjectWorldZ(objectId)) //im using the camera position as the gun position
//Create a tempory projectile block to calculate movement vectors of bullets
projectileDirBox as integer
projectileDirBox = CreateObjectBox( 1.0, 1.0, 1.0 )
SetObjectPosition( projectileDirBox, GetVector3X( gunPositionVec ), GetVector3Y( gunPositionVec ), GetVector3Z( gunPositionVec ) )
//setobjectrotation(projectiledirbox,GetObjectWorldAngleX(objectId),GetObjectWorldAngleY(objectId),GetObjectWorldAngleZ(objectId))
SetObjectLookAt( projectileDirBox, GetObjectWorldX(objectID2),GetObjectWorldY(objectID2),GetObjectWorldZ(objectID2), 0 )
MoveObjectLocalZ( projectileDirBox, -1.0 )
projectileDirVec = CreateVector3( GetobjectWorldX( projectileDirBox )-GetVector3X( gunPositionVec ), GetobjectWorldY( projectileDirBox )-GetVector3Y( gunPositionVec ), GetobjectWorldZ( projectileDirBox )-GetVector3Z( gunPositionVec ) )
DeleteObject( projectileDirBox )
bullet as integer
bullet = CreateObjectSphere(.45,.45,.45 )
SetObjectColor( bullet, 255, 255,255,255 )
SetObjectPosition( bullet, GetVector3X( gunPositionVec ), GetVector3Y( gunPositionVec ), GetVector3Z( gunPositionVec ) )
//3D Physics code
Create3DPhysicsDynamicBody( bullet )
SetObjectShapeSphere( bullet )
SetObject3DPhysicsMass( bullet, mass )
SetObject3DPhysicsLinearVelocity( bullet, projectileDirVec, initialSpeed )
myItem as bullet
myItem.ID = bullet //the object id of the bullet
myItem.time = timer() //timer is used to set there life time as bullets will die off after so long
myBullet.insert(myItem) //update bullet arrow as there is unlimeted bullets
inc bulletCount //used for a bullet counter to check if theyve timed out or not
deletevector3(projectileDirVec)
deletevector3(gunPositionVec)
endfunction
function getDistance(objectID as integer,objectID2 as integer)
distance as float
vec1=CreateVector3(GetObjectWorldX(objectID),GetObjectWorldY(objectID),GetObjectWorldZ(objectID))
vec2=CreateVector3(GetObjectWorldX(objectID2),GetObjectWorldY(objectID2),GetObjectWorldZ(objectID2))
distance=GetVector3Distance(vec1,vec2 )
endfunction distance
function moveblockwithmouse(objID as integer)
VectorX# = Get3DVectorXFromScreen( GetPointerX(), GetPointerY() )
VectorY# = Get3DVectorYFromScreen( GetPointerX(), GetPointerY() )
VectorZ# = Get3DVectorZFromScreen( GetPointerX(), GetPointerY()*2 )
// get a vector that points in the direction the camera is looking
VectorX2# = Get3DVectorXFromScreen( GetVirtualWidth()/2.0, GetVirtualHeight()/2.0 )
VectorY2# = Get3DVectorYFromScreen( GetVirtualWidth()/2.0, GetVirtualHeight()/2.0 )
VectorZ2# = Get3DVectorZFromScreen( GetVirtualWidth()/2.0, GetVirtualHeight()/2.0 )
// normalise Vector in the direction of Vector2
Dot# = VectorX#*VectorX2# + VectorY#*VectorY2# + VectorZ#*VectorZ2#
if ( Dot# > 0 )
VectorX# = VectorX# / Dot#
VectorY# = VectorY# / Dot#
VectorZ# = VectorZ# / Dot#
endif
WorldX# = VectorX# * Depth# + GetCameraX(1)
WorldY# = VectorY# * Depth# + GetCameraY(1)
WorldZ# = VectorZ# * Depth# + GetCameraZ(1)
SetObjectPosition(objID,WorldX#, WorldY#,GetObjectZ(objID))
endfunction
hope you don't mind me posting just thought it was relevant by a tower defense and its kinda
a combined idea I got from a few threads
fubar