It's something different
.
When I test a collision between my "character" and an obstacle (a wall, a tree..), if the collision =1, my character can't move after that.
I'm looking for a technic to only block the character but not stop it forever (like on my first code).
I have found a little technic, but it doesn't work properly in some situation and I'm sur there is a better solution for sprite movement and block (obstacle) :
SetVirtualResolution(800,600)
Type sSprite
x as float
y as float
Sprite as integer
EndType
Global Caz, NbBloc as integer
caz = 32
NbBloc = 15
Global Dim sprite[NbBloc] as sSprite // the block - obstacle
Global dir, new, x, y as integer
loadimage(2, "image2.jpg")
loadimage(1, "image1.jpg")
// creation of the block (obstacle)
For i = 0 To NbBloc
if i = 0
sprite[i].Sprite = CreateSprite(2)
else
sprite[i].Sprite = CreateSprite(1)
endif
SetSpriteSize(sprite[i].Sprite,caz,caz)
x1 = Random(0,750)
x1 = x1/caz
x1 = x1*caz
sprite[i].x = x1
y1 = Random(0,550)
y1 = y1/Caz
y1 = y1*Caz
sprite[i].y = y1
SetSpritePosition(sprite[i].Sprite, sprite[i].x, sprite[i].y)
Next i
// create a wall to test
For i = 5 to NbBloc
sprite[i].y = 128
sprite[i].x = 128 + (i-5)*caz
SetSpritePosition(sprite[i].Sprite, sprite[i].x, sprite[i].y)
next i
// the "cible" for the character
x = Sprite[0].x
y = Sprite[0].y
do
if GetRawKeyPressed(27) = 1 / /escape - back on android
End
endif
If GetPointerPressed() = 1 // to move the character
Clic = 1
x = GetPointerX()
y = GetPointerY()
// snap at 32*32
x = x/Caz
x = x*Caz
y = y/Caz
y = y*Caz
dir = ATan2(sprite[0].x - x, sprite[0].y - y)
New = 1
elseif GetPointerReleased() =1
Clic = 0
EndIf
If clic = 1
x = GetPointerX()
x = x/Caz
x = x*Caz
y = GetPointerY()
y = y/Caz
y = y*Caz
dir = ATan2(sprite[0].x - x, sprite[0].y - y)
New = 1
endif
Print(str(x)+"/"+str(y)+" - Dir : "+str(dir))
if new >= 1
MovePlayer(x,y,dir)
endif
sync()
Loop
function GetAngle(x1#, y1#, x2#, y2#)
result# = ATanFull(x1# - x2#, y1# - y2#)
endfunction result#
Function MovePlayer(x,y,dir) '{
Collide = 0
// test the collision with block (obstacle, wall, tree...)
For i = 1 To NbBloc
n = 1
If GetSpriteCollision(sprite[i].Sprite, Sprite[0].sprite)
x1 = Sprite[0].x
y1 = Sprite[0].y
if Dir >= -22 and dir <=22 // direction up
y1 = Sprite[0].y +n
elseif dir >=23 and dir <=77 // up left
y1 = Sprite[0].y +n
x1 = Sprite[0].x +n
elseif dir >=78 and dir <=122 // left
x1 = Sprite[0].x +n
elseif dir >=123 and dir <=165 // down left
x1 = Sprite[0].x +n
y1 = Sprite[0].y -n
elseif dir >=166 or dir <=-157 // down (bottom)
y1 = Sprite[0].y -n
elseif dir >=-156 or dir <=-112 // down right
y1 = Sprite[0].y -n
x1 = Sprite[0].x -n
elseif dir >=-111 or dir <=-77 // right
x1 = Sprite[0].x -n
elseif dir >=-76 or dir <=-21 // up right
y1 = Sprite[0].y +n
x1 = Sprite[0].x -n
Endif
Sprite[0].x = x1
Sprite[0].y = y1
x = x1
y = y1
SetSpritePosition(sprite[0].Sprite, sprite[0].x, sprite[0].y)
Collide = 1
new = 0
EndIf
Next i
If collide = 0 // no collision, we move the character
speed = 2
if sprite[0].x <> x or sprite[0].y <> y
if sprite[0].x <> x
sprite[0].x = sprite[0].x + cos(ATan2((y - sprite[0].y),(x - sprite[0].x)))* speed
if abs(sprite[0].x - x) <= speed
sprite[0].x = x
endif
endif
if sprite[0].y <> y
sprite[0].y = sprite[0].y + sin(ATan2((y - sprite[0].y),(x - sprite[0].x)))* speed
if abs(sprite[0].y - y) <= speed
sprite[0].y = y
endif
endif
SetSpritePosition(sprite[0].Sprite, sprite[0].x, sprite[0].y)
else
new = 0
endif
endif
EndFunction '}
http://www.dracaena-studio.com