Help Collision
Iniziato da Nalcwap, 24 nov 2011 - 23:58
25 risposte a questa discussione
#1
OFFLINE
Inviato 24 novembre 2011 - 23:58
Sorry i want know how make 2 squares 10x10cm collide and collide in all 4 positions up,down,right,left
use graph
block='block.png'
block2='block2.png'
x=10
y=50
l=30
m=50
function objects()
graph.put(.x,.y,.block)
graph.put(.l,.m,.block2)
end
function collide_sq1_sq2()
if .x=.l and .y=.m then
.x=.x-10;
esif ...
end
end
ui.keys(false);
while true do
g.clear();
objects();
ke=ui.cmd(1);
if ke=ui.rightkey then
x=x+10;
elsif ke=ui.leftkey then
x=x-10;
elsif ke=ui.upkey then
y=y-10;
elsif ke=ui.downkey then
y=y+10;
end;
collide_sq1_sq2()
g.show();
end
ho collide the 2 squares in all faces up.down,left,right?
Thanks for any help
use graph
block='block.png'
block2='block2.png'
x=10
y=50
l=30
m=50
function objects()
graph.put(.x,.y,.block)
graph.put(.l,.m,.block2)
end
function collide_sq1_sq2()
if .x=.l and .y=.m then
.x=.x-10;
esif ...
end
end
ui.keys(false);
while true do
g.clear();
objects();
ke=ui.cmd(1);
if ke=ui.rightkey then
x=x+10;
elsif ke=ui.leftkey then
x=x-10;
elsif ke=ui.upkey then
y=y-10;
elsif ke=ui.downkey then
y=y+10;
end;
collide_sq1_sq2()
g.show();
end
ho collide the 2 squares in all faces up.down,left,right?
Thanks for any help
#4
OFFLINE
Inviato 26 novembre 2011 - 17:14
Questo è quello che so fare, vedi se ti può andar bene,(non sono un programmatore
1) Considero due quadrati di lato 10x10.
2) Considero che i movimenti dei quadrati portino sempre a sovrapporre il perimeto mai oltre.
(quindi devi spostarti sempre di 10 pixel per volta.)
3) diamo per scontato che il lato destro di block.png può sovrapporsi solo al lato sinistro di block2.png
cosi pure lato alto di block.png si sovrappone solo a lato basso di block2.png.
Per semplicità verifico se il lato di un quadrato si sovrappone all'altro. (.x+10=.l) e per
essere una collisione la distanza assoluta di (.y-.m) deve essere minore del lato del quadrato.
Per permetterti di verificare il tutto ho trasformato il tuo codice in un mini giochino dove ti puoi divertire a respingere il quadratino come fossero due magneti di stessa polarità.
// 26/11/2011 Ore 15.00
use graph as g, ui, math;
//--Variable global-----
block=g.icon('block.png');
block2=g.icon('block2.png');
// x x+10
// ! !
// y ---+-----+
// ! !
// ! !
// y+10--+-----+
x=10;
y=50;
l=30;
m=50;
// -----Function----------
function objects()
graph.put(.x,.y,.block);
graph.put(.l,.m,.block2);
end
function collide_sq1_sq2()
if (.x+10=.l) and (math.abs(.y-.m)<10) then
.l=.l+10;
elsif (.x=.l+10) and (math.abs(.y-.m)<10) then
.l=.l-10;
elsif (.y+10=.m) and (math.abs(.x-.l)<10) then
.m=.m+10;
elsif (.y=.m+10) and (math.abs(.x-.l)<10) then
.m=.m-10;
end;
end
//============MAIN=============
ui.keys(false);
while true do
g.clear();
objects();
ke=ui.cmd(1);
if ke=ui.rightkey then
x=x+10;
elsif ke=ui.leftkey then
x=x-10;
elsif ke=ui.upkey then
y=y-10;
elsif ke=ui.downkey then
y=y+10;
end;
collide_sq1_sq2();
g.show();
end
//==========END MAIN============
sqGame.txt 989bytes
2 downloads
block.png 221bytes
5 downloads
block2.png 212bytes
2 downloads
Messaggio modificato da lento, 26 novembre 2011 - 17:15
#6
OFFLINE
Inviato 27 novembre 2011 - 09:20
Questo programma utilizza il metodo di rilevazione contenuto in questo post: http://www.nokioteca...ost__p__17198031 question if square l,m is fixed not move what i have to modified the code to stop and collide fixed?
Ti ho lasciato anche la funzione collide_sq1_sq2() se oltre la collisione ti servisse sapere su che lato è
avvenuta.
// 27/11/2011 Ore 08.00
use graph as g, ui, math;
//--Variable global-----
block=g.icon('block.png');
block2=g.icon('block2.png');
// x x+10
// ! !
// y ---+-----+
// ! !
// ! !
// y+10--+-----+
x=10;
y=50;
l=30;
m=50;
// -----Function----------
function objects()
graph.put(.x,.y,.block);
graph.put(.l,.m,.block2);
end
function collisione(x1,y1)
if (math.abs((.l+5)-(.x+x1+5))< 10) and
(math.abs((.m+5)-(.y+y1+5))< 10) then
return true;
end;
return false;
end;
// alternativa rende il lato colpito--------------+
function collide_sq1_sq2() //
if (.x+10=.l) and (math.abs(.y-.m)<10) then //
return "right"; //.l=.l+10; //
elsif (.x=.l+10) and (math.abs(.y-.m)<10) then //
return "left"; //.l=.l-10; //
elsif (.y+10=.m) and (math.abs(.x-.l)<10) then //
return "down"; //.m=.m+10; //
elsif (.y=.m+10) and (math.abs(.x-.l)<10) then //
return "up"; //.m=.m-10; //
end; //
return ""; //
end //
//------------------------------------------------+
//============MAIN=============
ui.keys(false);
while true do
g.clear();
objects();
ke=ui.cmd(1);
if ke=ui.rightkey then
if collisione(10,0)=false then x=x+10 end;
elsif ke=ui.leftkey then
if collisione(-10,0)=false then x=x-10 end;
elsif ke=ui.upkey then
if collisione(0,-10)=false then y=y-10 end;
elsif ke=ui.downkey then
if collisione(0,10)=false then y=y+10 end;
end;
// collide_sq1_sq2();
g.show();
end
//==========END MAIN============
sqGame1.txt 1,64K
3 downloadsDato che i quadrati si spostano sempre di 10 pixel e in caso di collisione il quadrato1 si sovrappone esattamente sul quadrato2
potresti utilizzare questa soluzione più semplice e veloce:
// 27/11/2011 Ore 08.15
// very simple and fast
use graph as g, ui;
//--Variable global-----
block=g.icon('block.png');
block2=g.icon('block2.png');
x=10;
y=50;
l=30;
m=50;
// -----Function----------
function objects()
graph.put(.x,.y,.block);
graph.put(.l,.m,.block2);
end
function collisione(x1,y1)
if (.l=.x+x1) and (.m=.y+y1) then
return true;
end;
return false;
end;
//============MAIN=============
ui.keys(false);
while true do
g.clear();
objects();
ke=ui.cmd(1);
if ke=ui.rightkey then
if collisione(10,0)=false then x=x+10 end;
elsif ke=ui.leftkey then
if collisione(-10,0)=false then x=x-10 end;
elsif ke=ui.upkey then
if collisione(0,-10)=false then y=y-10 end;
elsif ke=ui.downkey then
if collisione(0,10)=false then y=y+10 end;
end;
g.show();
end
//==========END MAIN============
sqGame2.txt 952bytes
2 downloadsSpero di aver capito cosa volevi...
#8
OFFLINE
Inviato 27 novembre 2011 - 11:40
Sorry this question in the
use graph as g, ui;
block=g.icon('block.png');
block2=g.icon('block2.png');
x=10;
y=50;
l=30;
m=50;
function objects()
graph.put(.x,.y,.block);
graph.put(.l,.m,.block2);
end
function collisione(x1,y1)
if (.l=.x+x1) and (.m=.y+y1) then
return true;
end;
return false;
end;
ui.keys(false);
while true do
g.clear();
objects();
ke=ui.cmd(1);
if ke=ui.rightkey then
if collisione(10,0)=false then x=x+10 end;
elsif ke=ui.leftkey then
if collisione(-10,0)=false then x=x-10 end;
elsif ke=ui.upkey then
if collisione(0,-10)=false then y=y-10 end;
elsif ke=ui.downkey then
if collisione(0,10)=false then y=y+10 end;
end;
g.show();
end
i want implement the array(i try make the game step by step for easy understand) now the objects(squares) i try put into a array example:
blocks=[[1,0,0,2,2,0,0,0,0,0],
[0,0,0,0,0,0,0,0,2,0],
[0,0,0,0,0,0,0,0,2,0],
[0,0,0,0,0,0,0,0,0,0]]
0=null
1=block
2=block2
but now with the script sqgame2 i not have a clue how implement the collision you make for all squares
can you explain if the example sqgame2.txt what i have to do for the collision work in all squares if ease best im very bad understand complicated formulas and thanks for the trouble
use graph as g, ui;
block=g.icon('block.png');
block2=g.icon('block2.png');
x=10;
y=50;
l=30;
m=50;
function objects()
graph.put(.x,.y,.block);
graph.put(.l,.m,.block2);
end
function collisione(x1,y1)
if (.l=.x+x1) and (.m=.y+y1) then
return true;
end;
return false;
end;
ui.keys(false);
while true do
g.clear();
objects();
ke=ui.cmd(1);
if ke=ui.rightkey then
if collisione(10,0)=false then x=x+10 end;
elsif ke=ui.leftkey then
if collisione(-10,0)=false then x=x-10 end;
elsif ke=ui.upkey then
if collisione(0,-10)=false then y=y-10 end;
elsif ke=ui.downkey then
if collisione(0,10)=false then y=y+10 end;
end;
g.show();
end
i want implement the array(i try make the game step by step for easy understand) now the objects(squares) i try put into a array example:
blocks=[[1,0,0,2,2,0,0,0,0,0],
[0,0,0,0,0,0,0,0,2,0],
[0,0,0,0,0,0,0,0,2,0],
[0,0,0,0,0,0,0,0,0,0]]
0=null
1=block
2=block2
but now with the script sqgame2 i not have a clue how implement the collision you make for all squares
can you explain if the example sqgame2.txt what i have to do for the collision work in all squares if ease best im very bad understand complicated formulas and thanks for the trouble
#9
OFFLINE
Inviato 28 novembre 2011 - 14:57
Quanto spiegato per le collisioni, non serve se utilizzi il sistema "Tile" per disegnare lo scenario e far muovere i personaggi.
As explained for the collision, the system is not needed if you use "Tile" to paint the scenery and the characters to move.
Collisioni in "Tile" sono già state spiegate ed usate in:
Snake: http://www.nokioteca...ost__p__1308015
Pacman: http://www.nokioteca...ost__p__1447308
Comunque per riassumere se vuoi muovere il personaggio in un "Tile" prima leggi nella matrice dello scenario cosa contiene quel
"Tile" e poi decidi se vi può accedere oppure no.
#10
OFFLINE
Inviato 04 dicembre 2011 - 23:43
Sorry lento i read the sneake and the packman but not understand the basis for in my example sqgame2 put square1 walking and collide in array can you explain please easy the base for this level=[[2,0,0,2,2],
[0,0,0,0,0],
[0,2,0,2,0],
[0,0,2,0,0],
[0,0,0,0,0]]
0=null
1=square1
2=square2
if square1 is in level[3][3]
how i put move the square1 move and collide in all square2 in this array level?can you post a very simple example for i understand d basic for this please?very thanks lento and sorry for the trouble
[0,0,0,0,0],
[0,2,0,2,0],
[0,0,2,0,0],
[0,0,0,0,0]]
0=null
1=square1
2=square2
if square1 is in level[3][3]
how i put move the square1 move and collide in all square2 in this array level?can you post a very simple example for i understand d basic for this please?very thanks lento and sorry for the trouble
#14
OFFLINE
Inviato 05 dicembre 2011 - 22:55
Eccomi qua.Thanks lento for your reply
Per rendere comprensibile ho semplificato il tutto.
Usa i tasti cursore per spostarti ui.leftkey,ui.rightkey,ui.upkey,ui.downkey e il tasto ui.gokey per terminare.
es.txt 2,75K
3 downloadsQuesto quanto vedi a schermo usando la tua matrice leve[].
img.png 873bytes
1 downloadsIn seguito potrai usare maxRow maxCol per muoverti in parti di disegno non visibili a schermo.
#17
OFFLINE
Inviato 06 dicembre 2011 - 14:26
If i want make this game with squares moving and when squares are in correct base the level go to level 2 how put squares move and how activate level 2 with this 2 squares?
Square 1=moving others squares with faces
square []=square for activate level 2
square X=square for activate level 2
the 2 squares will have activate the next level if stand in correct base example image
how make this thanks?
Square 1=moving others squares with faces
square []=square for activate level 2
square X=square for activate level 2
the 2 squares will have activate the next level if stand in correct base example image
how make this thanks?













