ok i lied, 1 more quick fix to allow it to work on all 3 objects - i just didn't take into account the world offset of the objects when repositioning the vertexes
certainly not going to match the impressive style of MGS there but i'm pleased with the result considering it's completely new to me, now i just need to figure out if i can limit the stretching when you slice quickly
// Project: bodychop
// Created: 2018-08-11
// show all errors
SetErrorMode(2)
// set window properties
SetWindowTitle( "bodychop" )
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
box as integer[99]
newobj as integer[99]
x_swipe as integer[2]
y_swipe as integer[2]
xpoint_of_impact as float[5]
ypoint_of_impact as float[5]
zpoint_of_impact as float[5]
global camX as float
global camY as float
global camZ as float
global pointX as float
global pointY as float
global pointZ as float
global dist as integer
global omx as integer
global omy as integer
cut_direction as string
SetCameraLookAt(1,0,10,0,0)
MoveCameraLocalZ(1,-150)
MoveCameraLocalY(1,150)
RotateCameraLocalX(1,25)
white = MakeColor(255,255,255)
grey = MakeColor(128,128,128)
_restart:
//create 2 objects to test cutting on
box[1] = CreateObjectSphere(100,10,10)
SetObjectColor(box[1],255,0,0,255)
MoveObjectLocalZ(box[1],180)
box[2] = CreateObjectCapsule(100,200,100)
SetObjectColor(box[2],0,255,0,255)
MoveObjectLocalX(box[2],-190)
MoveObjectLocalZ(box[2],180)
box[3] = CreateObjectBox(100,100,100)
SetObjectColor(box[3],0,0,255,255)
MoveObjectLocalZ(box[3],180)
MoveObjectLocalX(box[3],190)
//SaveMemblockToFile(box[1])
//SaveMemblockToFile(box[2])
boxes = 1
objs = 0
blade = CreateObjectBox(10,10,300)
SetObjectCollisionMode(blade,0)
MoveObjectLocalZ(blade,200)
MoveObjectLocalY(blade,80)
nearest = -1
//press space bar to reset the scene objects
//click and hold the knife to move it to slice the objects
do
if GetRawKeyReleased(27) then end
if GetRawKeyReleased(32)
DeleteAllObjects()
sync()
goto _restart
endif
Print(getobjecty(blade))
//use the right mouse to rotate the objects
if GetRawMouseRightPressed()
robj = 0
omx = GetPointerX()
omy = GetPointerY()
camX = GetCameraX(1)
camY = GetCameraY(1)
camZ = GetCameraZ(1)
pointX = Get3DVectorXFromScreen(omx, omy)
pointY = Get3DVectorYFromScreen(omx, omy)
pointZ = Get3DVectorZFromScreen(omx, omy)
dist = 5024
pointX = camX+(pointX*dist)
pointY = camY+(pointY*dist)
pointZ = camZ+(pointZ*dist)
SetObjectCollisionMode(blade,1)
robj = ObjectRayCast(0, camX, camY, camZ, pointX, pointY, pointZ)
SetObjectCollisionMode(blade,0)
endif
RotateAnObject(robj)
//use the left mouse to click the blade to move it to cut away at objects
if GetPointerPressed() //clicking the blade
//determine where was clicked on the object
px = GetPointerX()
py = GetPointerY()
oldpx = px
oldpy = py
camX = GetCameraX(1)
camY = GetCameraY(1)
camZ = GetCameraZ(1)
pointX = Get3DVectorXFromScreen(px, py)
pointY = Get3DVectorYFromScreen(px, py)
pointZ = Get3DVectorZFromScreen(px, py)
dist = 5024
pointX = camX+(pointX*dist)
pointY = camY+(pointY*dist)
pointZ = camZ+(pointZ*dist)
newpx = GetScreenXFrom3D(pointX,pointY,pointZ)
newpy = GetScreenYFrom3D(pointX,pointY,pointZ)
SetObjectCollisionMode(blade,1)
hitobj = ObjectRayCast(0, camX, camY, camZ, pointX, pointY, pointZ-10)
SetObjectCollisionMode(blade,0)
if hitobj > 0
if hitobj = blade
selected_blade = 1
endif
endif
elseif GetPointerState()
if selected_blade = 1
px = GetPointerX()
py = GetPointerY()
if px <> oldpx
nbx = px-oldpx
else
nbx = 0
endif
if py <> oldpy
nby = oldpy-py
else
nby = 0
endif
oldpx = px
oldpy = py
MoveObjectLocalX(blade,nbx)
MoveObjectLocalY(blade,nby)
nbx = GetObjectX(blade)
nby = GetObjectY(blade)
nbz = GetObjectZ(blade)-((GetObjectSizeMaxZ(blade)-GetObjectSizeMinZ(blade))/2)
nbz2 = nbz+((GetObjectSizeMaxZ(blade)-GetObjectSizeMinZ(blade))/2)
hitobj = ObjectRayCast(0,nbx,nby,nbz,nbx,nby,nbz2)
if hitobj > 0
Print("hitting object :"+str(hitobj))
rayindex = 0
hx# = GetObjectRayCastX(rayindex)
hy# = GetObjectRayCastY(rayindex)
hz# = GetObjectRayCastZ(rayindex)
if impacted_obj = 0
xpoint_of_impact[1] = hx#
ypoint_of_impact[1] = hy#
zpoint_of_impact[1] = hz#
x_swipe[1] = px
y_swipe[1] = py
impacted_obj = hitobj
else
xpoint_of_impact[5] = hx#
ypoint_of_impact[5] = hy#
zpoint_of_impact[5] = hz#
x_swipe[2] = px
y_swipe[2] = py
endif
else
if impacted_obj > 0
//do final cut now
oldobjx = getobjectx(impacted_obj)
oldobjy = getobjecty(impacted_obj)
oldobjz = getobjectz(impacted_obj)
SetObjectPosition(impacted_obj,0,0,0)
FixObjectPivot(impacted_obj)
SetObjectPosition(impacted_obj,oldobjx,oldobjy,oldobjz)
boxmem = CreateMemblockFromObjectMesh(impacted_obj,1)
indexes = GetMemblockInt(boxmem,0)
indexes = indexes-1
nearest = -1
tdist# = 400
if abs(x_swipe[2] - x_swipe[1]) < abs(y_swipe[2]-y_swipe[1])
cut_direction = "vertical"
else
cut_direction = "horizontal"
endif
dummy = CreateObjectBox(1,1,1)
SetObjectPosition(dummy,xpoint_of_impact[1],ypoint_of_impact[1],zpoint_of_impact[1])
SetObjectLookAt(dummy,xpoint_of_impact[5],ypoint_of_impact[5],zpoint_of_impact[5],0)
for a = 1 to 3
MoveObjectLocalZ(dummy,GetDistanceToPoint(dummy,xpoint_of_impact[5],ypoint_of_impact[5],zpoint_of_impact[5])*0.25)
xpoint_of_impact[a+1] = getobjectx(dummy)
ypoint_of_impact[a+1] = getobjecty(dummy)
zpoint_of_impact[a+1] = getobjectz(dummy)
next a
DeleteObject(dummy)
if cut_direction = "vertical"
//create sliced object 1 (left side of object)
for a = indexes to 0 step -1
onlyvx = GetMeshMemblockVertexX(boxmem,a)
onlyvy = GetMeshMemblockVertexY(boxmem,a)
onlyvz = GetMeshMemblockVertexZ(boxmem,a)
mx# = getobjectx(impacted_obj)+onlyvx
my# = getobjecty(impacted_obj)+onlyvy
mz# = getobjectz(impacted_obj)+onlyvz
neardist# = 400
nearest_point = -1
for b = 1 to 5
if mx# >= xpoint_of_impact[b]
newdist# = GetPointDistanceToPoint(mx#,my#,mz#,xpoint_of_impact[b],ypoint_of_impact[b],zpoint_of_impact[b])
if newdist# < neardist#
nearest_point = b
neardist# = newdist#
endif
endif
next b
if nearest_point > -1
SetMeshMemblockVertexPosition(boxmem,a,xpoint_of_impact[nearest_point]-getobjectx(impacted_obj),ypoint_of_impact[nearest_point]-getobjecty(impacted_obj),onlyvz)
endif
next a
inc objs, 1
newobj[objs] = CreateObjectFromMeshMemblock(boxmem)
SetObjectPosition(newobj[objs],getobjectx(impacted_obj),getobjecty(impacted_obj),getobjectz(impacted_obj))
MoveObjectLocalX(newobj[objs],((GetObjectSizeMaxX(newobj[objs])-GetObjectSizeMinX(newobj[objs]))*-0.25))
SetObjectColor(newobj[objs],255,0,0,255)
DeleteMemblock(boxmem)
//create object 2 (right side of object)
boxmem2 = CreateMemblockFromObjectMesh(impacted_obj,1)
for a = indexes to 0 step -1
onlyvx = GetMeshMemblockVertexX(boxmem2,a)
onlyvy = GetMeshMemblockVertexY(boxmem2,a)
onlyvz = GetMeshMemblockVertexZ(boxmem2,a)
mx# = getobjectx(impacted_obj)+onlyvx
my# = getobjecty(impacted_obj)+onlyvy
mz# = getobjectz(impacted_obj)+onlyvz
neardist# = 400
nearest_point = -1
for b = 1 to 5
if mx# <= xpoint_of_impact[b]
newdist# = GetPointDistanceToPoint(mx#,my#,mz#,xpoint_of_impact[b],ypoint_of_impact[b],zpoint_of_impact[b])
if newdist# < neardist#
nearest_point = b
neardist# = newdist#
endif
endif
next b
if nearest_point > -1
SetMeshMemblockVertexPosition(boxmem2,a,xpoint_of_impact[nearest_point]-getobjectx(impacted_obj),ypoint_of_impact[nearest_point]-getobjecty(impacted_obj),onlyvz)
endif
next a
inc objs, 1
newobj[objs] = CreateObjectFromMeshMemblock(boxmem2)
SetObjectColor(newobj[objs],0,255,0,255)
SetObjectPosition(newobj[objs],getobjectx(impacted_obj),getobjecty(impacted_obj),getobjectz(impacted_obj))
MoveObjectLocalX(newobj[objs],((GetObjectSizeMaxX(newobj[objs])-GetObjectSizeMinX(newobj[objs]))*0.25))
DeleteMemblock(boxmem2)
DeleteObject(impacted_obj)
elseif cut_direction = "horizontal"
//create sliced object 1 (top of object)
for a = indexes to 0 step -1
onlyvx = GetMeshMemblockVertexX(boxmem,a)
onlyvy = GetMeshMemblockVertexY(boxmem,a)
onlyvz = GetMeshMemblockVertexZ(boxmem,a)
mx# = getobjectx(impacted_obj)+onlyvx
my# = getobjecty(impacted_obj)+onlyvy
mz# = getobjectz(impacted_obj)+onlyvz
neardist# = 400
nearest_point = -1
for b = 1 to 5
if my# >= ypoint_of_impact[b]
newdist# = GetPointDistanceToPoint(mx#,my#,mz#,xpoint_of_impact[b],ypoint_of_impact[b],zpoint_of_impact[b])
if newdist# < neardist#
nearest_point = b
neardist# = newdist#
endif
endif
next b
if nearest_point > -1
SetMeshMemblockVertexPosition(boxmem,a,xpoint_of_impact[nearest_point]-getobjectx(impacted_obj),ypoint_of_impact[nearest_point]-getobjecty(impacted_obj),onlyvz)
endif
next a
inc objs, 1
newobj[objs] = CreateObjectFromMeshMemblock(boxmem)
SetObjectPosition(newobj[objs],getobjectx(impacted_obj),getobjecty(impacted_obj),getobjectz(impacted_obj))
MoveObjectLocalY(newobj[objs],((GetObjectSizeMaxY(newobj[objs])-GetObjectSizeMinY(newobj[objs]))*-0.25))
SetObjectColor(newobj[objs],255,0,0,255)
DeleteMemblock(boxmem)
//create object 2 (bottom of object)
boxmem2 = CreateMemblockFromObjectMesh(impacted_obj,1)
for a = indexes to 0 step -1
onlyvx = GetMeshMemblockVertexX(boxmem2,a)
onlyvy = GetMeshMemblockVertexY(boxmem2,a)
onlyvz = GetMeshMemblockVertexZ(boxmem2,a)
mx# = getobjectx(impacted_obj)+onlyvx
my# = getobjecty(impacted_obj)+onlyvy
mz# = getobjectz(impacted_obj)+onlyvz
neardist# = 400
nearest_point = -1
for b = 1 to 5
if my# <= ypoint_of_impact[b]
newdist# = GetPointDistanceToPoint(mx#,my#,mz#,xpoint_of_impact[b],ypoint_of_impact[b],zpoint_of_impact[b])
if newdist# < neardist#
nearest_point = b
neardist# = newdist#
endif
endif
next b
if nearest_point > -1
SetMeshMemblockVertexPosition(boxmem2,a,xpoint_of_impact[nearest_point]-getobjectx(impacted_obj),ypoint_of_impact[nearest_point]-getobjecty(impacted_obj),onlyvz)
endif
next a
inc objs, 1
newobj[objs] = CreateObjectFromMeshMemblock(boxmem2)
SetObjectColor(newobj[objs],0,255,0,255)
SetObjectPosition(newobj[objs],getobjectx(impacted_obj),getobjecty(impacted_obj),getobjectz(impacted_obj))
MoveObjectLocalY(newobj[objs],((GetObjectSizeMaxY(newobj[objs])-GetObjectSizeMinY(newobj[objs]))*0.25))
DeleteMemblock(boxmem2)
DeleteObject(impacted_obj)
endif
endif
impacted_obj = 0
endif
remstart
rayindex = 0 //GetObjectRayCastNumHits()
//get the point of impact
hx# = GetObjectRayCastX(rayindex)
hy# = GetObjectRayCastY(rayindex)
hz# = GetObjectRayCastZ(rayindex)
dummy = CreateObjectBox(2,2,2)
SetObjectPosition(dummy,hx#,hy#,hz#)
//SetObjectLookAt(dummy,camX,camY,camZ,0)
SetObjectLookAt(dummy,nbx,nby,nbz,0)
MoveObjectLocalZ(dummy,-0.005)
hx# = getobjectX(dummy)
hy# = getobjectY(dummy)
hz# = getobjectZ(dummy)
DeleteObject(dummy)
//loop through the memblock and find the closest vertex to the point clicked
for a = indexes to 0 step -1
onlyvx = GetMeshMemblockVertexX(boxmem,a)
onlyvy = GetMeshMemblockVertexY(boxmem,a)
onlyvz = GetMeshMemblockVertexZ(boxmem,a)
mx# = getobjectx(hitobj)+onlyvx
my# = getobjecty(hitobj)+onlyvy
mz# = getobjectz(hitobj)+onlyvz
//ignore any previously moved vertex
if onlyvx <> 0 or onlyvy <> 0 or onlyvz <> 0
mdist# = GetPointDistanceToPoint(mx#,my#,mz#,hx#,hy#,hz#)
temp = 0
//used to display the distance in real time
debug = 0
if debug = 1
repeat
if GetRawKeyReleased(27) then end
inc temp, 1
Print("nearest so far = "+str(nearest))
Print("distance = "+str(tdist#))
Print(a)
Print(mdist#)
sync()
until temp > 2
endif
//update the nearest vertex if it is closer than the previous closest
if mdist# <= tdist#
if nearest >= 0
//try to find only those points that are closer to camera
if GetPointDistanceToPoint(mx#,my#,mz#,camX,camY,camZ) <= GetPointDistanceToPoint(oldnx#,oldny#,oldnz#,camX,camY,camZ)
tdist# = mdist#
oldnx# = mx#
oldny# = my#
oldnz# = mz#
nearest = a
endif
else
tdist# = mdist#
oldnx# = mx#
oldny# = my#
oldnz# = mz#
nearest = a
endif
endif
endif
next a
if nearest >= 0
for a = indexes to 0 step -1
onlyvx = GetMeshMemblockVertexX(boxmem,a)
onlyvy = GetMeshMemblockVertexY(boxmem,a)
onlyvz = GetMeshMemblockVertexZ(boxmem,a)
mx# = getobjectx(hitobj)+onlyvx
my# = getobjecty(hitobj)+onlyvy
mz# = getobjectz(hitobj)+onlyvz
if mx# = oldnx# and my# = oldny# and mz# = oldnz#
SetMeshMemblockVertexPosition(boxmem,a,0,0,0)
endif
next a
SetObjectMeshFromMemblock(hitobj,1,boxmem)
DeleteMemblock(boxmem)
endif
remend
endif
elseif GetPointerReleased()
selected_blade = 0
endif
Print( str(ScreenFPS(),0)+"FPS" )
Sync()
loop
function RotateAnObject(robj)
if GetRawMouseRightState() and robj > 0
mx = GetPointerX()
my = GetPointerY()
if mx > omx
RotateObjectGlobalY(robj,-5)
elseif mx < omx
RotateObjectGlobalY(robj,5)
endif
if my > omy
RotateObjectGlobalX(robj,-5)
elseif my < omy
RotateObjectGlobalX(robj,5)
endif
omx = GetPointerX()
omy = GetPointerY()
endif
endfunction
function SaveMemblockToFile(id)
SetRawWritePath(GetReadPath())
boxmem = CreateMemblockFromObjectMesh(id,1)
CreateFileFromMemblock("mem"+str(id)+".txt",boxmem)
file = OpenToRead("mem"+str(id)+".txt")
file2 = OpenToWrite("mem"+str(id)+"_eng.txt")
repeat
WriteLine(file2,Str(ReadByte(file)))
until FileEOF(file)
endfunction
function GetDistanceToPoint(e,x,y,z)
disx = GetObjectX(e) - x
disy = GetObjectY(e) - y
disz = GetObjectZ(e) - z
dist# = sqrt(disx^2 + disy^2 + disz^2)
endfunction(dist#)
function GetPointDistanceToPoint(x1,y1,z1,x2,y2,z2)
disx = x2-x1
disy = y2-y1
disz = z2-z1
dist# = sqrt((disx^2) + (disy^2) + (disz^2))
endfunction(dist#)
remstart
//create our memblock from the object
oldobjx = getobjectx(hitobj)
oldobjy = getobjecty(hitobj)
oldobjz = getobjectz(hitobj)
SetObjectPosition(hitobj,0,0,0)
FixObjectPivot(hitobj)
SetObjectPosition(hitobj,oldobjx,oldobjy,oldobjz)
boxmem = CreateMemblockFromObjectMesh(hitobj,1)
indexes = GetMemblockInt(boxmem,0)
indexes = indexes-1
nearest = -1
tdist# = 400
rayindex = 0 //GetObjectRayCastNumHits()
//get the point of impact
hx# = GetObjectRayCastX(rayindex)
hy# = GetObjectRayCastY(rayindex)
hz# = GetObjectRayCastZ(rayindex)
dummy = CreateObjectBox(2,2,2)
SetObjectPosition(dummy,hx#,hy#,hz#)
//SetObjectLookAt(dummy,camX,camY,camZ,0)
SetObjectLookAt(dummy,oldobjx,oldobjy,oldobjz,0)
MoveObjectLocalZ(dummy,-0.005)
hx# = getobjectX(dummy)
hy# = getobjectY(dummy)
hz# = getobjectZ(dummy)
DeleteObject(dummy)
//loop through the memblock and find the closest vertex to the point clicked
for a = indexes to 0 step -1
onlyvx = GetMeshMemblockVertexX(boxmem,a)
onlyvy = GetMeshMemblockVertexY(boxmem,a)
onlyvz = GetMeshMemblockVertexZ(boxmem,a)
mx# = getobjectx(hitobj)+onlyvx
my# = getobjecty(hitobj)+onlyvy
mz# = getobjectz(hitobj)+onlyvz
//ignore any previously moved vertex
if onlyvx <> 0 or onlyvy <> 0 or onlyvz <> 0
mdist# = GetPointDistanceToPoint(mx#,my#,mz#,hx#,hy#,hz#)
temp = 0
//used to display the distance in real time
debug = 0
if debug = 1
repeat
if GetRawKeyReleased(27) then end
inc temp, 1
Print("nearest so far = "+str(nearest))
Print("distance = "+str(tdist#))
Print(a)
Print(mdist#)
sync()
until temp > 2
endif
//update the nearest vertex if it is closer than the previous closest
if mdist# <= tdist#
if nearest >= 0
//try to find only those points that are closer to camera
if GetPointDistanceToPoint(mx#,my#,mz#,camX,camY,camZ) <= GetPointDistanceToPoint(oldnx#,oldny#,oldnz#,camX,camY,camZ)
tdist# = mdist#
oldnx# = mx#
oldny# = my#
oldnz# = mz#
nearest = a
endif
else
tdist# = mdist#
oldnx# = mx#
oldny# = my#
oldnz# = mz#
nearest = a
endif
endif
endif
next a
if nearest >= 0
for a = indexes to 0 step -1
onlyvx = GetMeshMemblockVertexX(boxmem,a)
onlyvy = GetMeshMemblockVertexY(boxmem,a)
onlyvz = GetMeshMemblockVertexZ(boxmem,a)
mx# = getobjectx(hitobj)+onlyvx
my# = getobjecty(hitobj)+onlyvy
mz# = getobjectz(hitobj)+onlyvz
if mx# = oldnx# and my# = oldny# and mz# = oldnz#
SetMeshMemblockVertexPosition(boxmem,a,0,0,0)
endif
next a
SetObjectMeshFromMemblock(hitobj,1,boxmem)
DeleteMemblock(boxmem)
endif
remend
life's one big game
spec= 4ghz, 16gb ram, AMD R9 2700 gpu