Vai al contenuto



Foto

Help Collision


  • Per favore accedi per rispondere
25 risposte a questa discussione

#1 OFFLINE   Nalcwap

Nalcwap

    Advanced Member

  • Utente
  • StellettaStellettaStelletta
  • 58 messaggi

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

#2 OFFLINE   lento

lento

    Senior Member

  • Utente
  • StellettaStellettaStellettaStellettaStellettaStelletta
  • 638 messaggi
  • Cellulare:E72

Inviato 25 novembre 2011 - 19:59

Sorry i want know how make 2 squares 10x10cm collide and collide in all 4 positions up,down,right,left..

the squares overlap?

#3 OFFLINE   Nalcwap

Nalcwap

    Advanced Member

  • Utente
  • StellettaStellettaStelletta
  • 58 messaggi

Inviato 26 novembre 2011 - 02:59

No i do not want the squares overlap ,i want the squares hit in all sides and collide example [x][l] collide

but not overlap or [y]
[m]

#4 OFFLINE   lento

lento

    Senior Member

  • Utente
  • StellettaStellettaStellettaStellettaStellettaStelletta
  • 638 messaggi
  • Cellulare:E72

Inviato 26 novembre 2011 - 17:14

Questo è quello che so fare, vedi se ti può andar bene,(non sono un programmatore :whistle: )

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============

Allega file  sqGame.txt   989bytes   2 downloads
Allega file  block.png   221bytes   5 downloads
Allega file  block2.png   212bytes   2 downloads

Messaggio modificato da lento, 26 novembre 2011 - 17:15


#5 OFFLINE   Nalcwap

Nalcwap

    Advanced Member

  • Utente
  • StellettaStellettaStelletta
  • 58 messaggi

Inviato 26 novembre 2011 - 22:17

Thanks lento you are genius in making games without you this board is more dead ,1 question if square l,m is fixed not move what i have to modified the code to stop and collide fixed?

#6 OFFLINE   lento

lento

    Senior Member

  • Utente
  • StellettaStellettaStellettaStellettaStellettaStelletta
  • 638 messaggi
  • Cellulare:E72

Inviato 27 novembre 2011 - 09:20

1 question if square l,m is fixed not move what i have to modified the code to stop and collide fixed?

Questo programma utilizza il metodo di rilevazione contenuto in questo post: http://www.nokioteca...ost__p__1719803

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============
Allega file  sqGame1.txt   1,64K   3 downloads
Dato 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============
Allega file  sqGame2.txt   952bytes   2 downloads
Spero di aver capito cosa volevi...

#7 OFFLINE   Nalcwap

Nalcwap

    Advanced Member

  • Utente
  • StellettaStellettaStelletta
  • 58 messaggi

Inviato 27 novembre 2011 - 10:36

Thanks buddy for your help the sqgame2 is more ease and small and fast if i have doubts i call you ,very thanks

#8 OFFLINE   Nalcwap

Nalcwap

    Advanced Member

  • Utente
  • StellettaStellettaStelletta
  • 58 messaggi

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

#9 OFFLINE   lento

lento

    Senior Member

  • Utente
  • StellettaStellettaStellettaStellettaStellettaStelletta
  • 638 messaggi
  • Cellulare:E72

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   Nalcwap

Nalcwap

    Advanced Member

  • Utente
  • StellettaStellettaStelletta
  • 58 messaggi

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

#11 OFFLINE   lento

lento

    Senior Member

  • Utente
  • StellettaStellettaStellettaStellettaStellettaStelletta
  • 638 messaggi
  • Cellulare:E72

Inviato 05 dicembre 2011 - 08:29

Can you post a very simple example for i understand d basic for this...

Ok. :thumbs:

Messaggio modificato da lento, 05 dicembre 2011 - 08:30


#12 OFFLINE   Nalcwap

Nalcwap

    Advanced Member

  • Utente
  • StellettaStellettaStelletta
  • 58 messaggi

Inviato 05 dicembre 2011 - 16:42

Thanks lento for for your reply

#13 OFFLINE   Nalcwap

Nalcwap

    Advanced Member

  • Utente
  • StellettaStellettaStelletta
  • 58 messaggi

Inviato 05 dicembre 2011 - 16:43

Thanks lento for your reply

#14 OFFLINE   lento

lento

    Senior Member

  • Utente
  • StellettaStellettaStellettaStellettaStellettaStelletta
  • 638 messaggi
  • Cellulare:E72

Inviato 05 dicembre 2011 - 22:55

Thanks lento for your reply

Eccomi qua.
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.

Allega file  es.txt   2,75K   3 downloads

Questo quanto vedi a schermo usando la tua matrice leve[].
Allega file  img.png   873bytes   1 downloads

In seguito potrai usare maxRow maxCol per muoverti in parti di disegno non visibili a schermo.

#15 OFFLINE   Nalcwap

Nalcwap

    Advanced Member

  • Utente
  • StellettaStellettaStelletta
  • 58 messaggi

Inviato 05 dicembre 2011 - 23:11

Very thanks lento i will try

#16 OFFLINE   Nalcwap

Nalcwap

    Advanced Member

  • Utente
  • StellettaStellettaStelletta
  • 58 messaggi

Inviato 05 dicembre 2011 - 23:11

Very thanks lento i will try

#17 OFFLINE   Nalcwap

Nalcwap

    Advanced Member

  • Utente
  • StellettaStellettaStelletta
  • 58 messaggi

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?

#18 OFFLINE   Nalcwap

Nalcwap

    Advanced Member

  • Utente
  • StellettaStellettaStelletta
  • 58 messaggi

Inviato 06 dicembre 2011 - 14:30

Example

#19 OFFLINE   Nalcwap

Nalcwap

    Advanced Member

  • Utente
  • StellettaStellettaStelletta
  • 58 messaggi

Inviato 06 dicembre 2011 - 14:48

image

#20 OFFLINE   Nalcwap

Nalcwap

    Advanced Member

  • Utente
  • StellettaStellettaStelletta
  • 58 messaggi

Inviato 06 dicembre 2011 - 15:09

Here

Download file

  • Allega file  1.png   1,63K   0 downloads