π
<-

Collisions en Lua

Pour TI-Nspire OS 3.0 ou ultérieur.

Collisions en Lua

Unread postby pierrotdu18 » 05 Mar 2014, 05:06

Bonjour tout le monde, je suis en train de faire un jeu en Lua, j'ai déjà fait ça :

Code: Select all
-------------------
--Screen--Manager--
-------------------
screens = { menu = 1,
            playing = 2,
            gameover = 3 }
currentscreen = 2

paints = { }
timers = { }
enterKeys = { }
arrowUps = { }
arrowDowns = { }
arrowLefts = { }
arrowRights = { }

-------------------
------Globals------
-------------------
w, h = 318, 212
function on.resize(ww, hh)
   w, h = ww, hh
end

local fenetre=platform.window

----------------------
--TheGame--Variables--
----------------------

p, m = 1, -1

cubes = { ["1"] = { w / 2 - 40 , h / 2 - 40 , m , m},
          ["2"] = { w / 2 - 40 , h / 2 + 20 , m , p},
          ["3"] = { w / 2 + 20 , h / 2 - 40 , p , m},
          ["4"] = { w / 2 + 20 , h / 2 + 20 , p , p} }
boule = { w / 2 - 10 , h / 2 - 10 }

terrains = { {w / 10 , h / 12 , w - w / 10 , h - h / 12} , {w / 5 , h / 5 , w - w / 5 , h - h / 5} }

timer.start(0.01)

paints[screens.playing] = function(gc)
                              gc: setColorRGB(0, 0, 255)
                              gc: setPen("medium","dashed")
                              gc: drawRect(terrains[1][1], terrains[1][2] , -w + 2*terrains[1][3] , -h + 2*terrains[1][4])
                              gc: setPen("thin","smooth")
                              for k,v in pairs(cubes) do
                                  gc: drawRect(v[1],v[2],20,20)
                              end
                              gc: setColorRGB(255, 0, 0)
                              gc: setPen("medium","dashed")
                              gc: drawRect(terrains[2][1], terrains[2][2] , -w + 2*terrains[2][3] , -h + 2*terrains[2][4])
                              gc: setPen("thin","smooth")
                                  gc: drawArc(boule[1], boule[2], 20, 20, 0, 360)
                          end
timers[screens.playing] = function ()
                             for k,v in pairs(cubes) do
                                 v[1] = v[1] + v[3]
                                 v[2] = v[2] + v[4]
                             end
                             for k,v in pairs(cubes) do
                                 if v[1] <= terrains[1][1] then
                                     v[3] = p
                                 elseif v[1] >= terrains[1][3] - 20 then
                                     v[3] = m
                                 end
                                 if v[2] <= terrains[1][2] then
                                     v[4] = p
                                 elseif v[2] >= terrains[1][4] - 20 then
                                     v[4] = m
                                 end
                             end
                             fenetre:invalidate()
                         end


function on.paint(gc)
    paints[currentscreen](gc)
end

function on.timer()
    timers[currentscreen]()
end


Sauf que maintenant, j'aimerais que quand les carrés rentrent en collision, ils soient repoussées comme quand ils touchent une paroi :)

Je me suis méchamment creusé, la tête, mais je trouve pas comment faire, à part utiliser des class() mais je sais pas faire :'(

Merci à ceux qui me viendront en aide! :D
Bonjour
User avatar
pierrotdu18Premium
Niveau 15: CC (Chevalier des Calculatrices)
Niveau 15: CC (Chevalier des Calculatrices)
Level up: 40.5%
 
Posts: 975
Joined: 07 Nov 2013, 20:18
Location: Paris V
Gender: Male
Calculator(s):
MyCalcs profile
Class: MP* Lycée Henri IV

Re: Collisions en Lua

Unread postby AnToX98 » 05 Mar 2014, 10:03

J'ai essayé de faire quelque chose mais ça ne marche pas, mes collisions, et je ne comprend pas pourquoi :p :

Code: Select all
-------------------
--Screen--Manager--
-------------------
screens = { menu = 1,
            playing = 2,
            gameover = 3 }
currentscreen = 2

paints = { }
timers = { }
enterKeys = { }
arrowUps = { }
arrowDowns = { }
arrowLefts = { }
arrowRights = { }

-------------------
------Globals------
-------------------
w, h = 318, 212
function on.resize(ww, hh)
   w, h = ww, hh
end

local fenetre=platform.window

----------------------
--TheGame--Variables--
----------------------

p, m = 1, -1

cubes = { { w / 2 - 40 , h / 2 - 40 , m , m},
          { w / 2 - 40 , h / 2 + 20 , m , p},
          { w / 2 + 20 , h / 2 - 40 , p , m},
          { w / 2 + 20 , h / 2 + 20 , p , p} }
cW = 20


boule = { w / 2 - 10 , h / 2 - 10 }

terrains = { {w / 10 , h / 12 , w - w / 10 , h - h / 12} , {w / 5 , h / 5 , w - w / 5 , h - h / 5} }

timer.start(0.01)

paints[screens.playing] = function(gc)
                              gc: setColorRGB(0, 0, 255)
                              gc: setPen("medium","dashed")
                              gc: drawRect(terrains[1][1], terrains[1][2] , -w + 2*terrains[1][3] , -h + 2*terrains[1][4])
                              gc: setPen("thin","smooth")
                              for k,v in pairs(cubes) do
                                  gc: drawRect(v[1],v[2],20,20)
                              end
                              gc: setColorRGB(255, 0, 0)
                              gc: setPen("medium","dashed")
                              gc: drawRect(terrains[2][1], terrains[2][2] , -w + 2*terrains[2][3] , -h + 2*terrains[2][4])
                              gc: setPen("thin","smooth")
                                  gc: drawArc(boule[1], boule[2], 20, 20, 0, 360)
                          end
timers[screens.playing] = function ()
                             for i = 1, #cubes do
                                 cubes[i][1] = cubes[i][1] + (cubes[i][3])                               
                                 cubes[i][2] = cubes[i][2] + (cubes[i][4])
                             end
                             for i = 1, #cubes do
                                 if cubes[i][1] <= terrains[1][1] then
                                     cubes[i][3] = p                                     
                                 elseif cubes[i][1] >= terrains[1][3] - 20 then
                                     cubes[i][3] = m
                                 end
                                 if cubes[i][2] <= terrains[1][2] then
                                     cubes[i][4] = p
                                 elseif cubes[i][2] >= terrains[1][4] - 20 then
                                     cubes[i][4] = m
                                 end
                             end
                             for k = 1, #cubes do
                                 for i = 1, #cubes do
                                     if i ~= k then
                                         cubes[k][3] = box_collision(cubes[k], cubes[i])[1]
                                         cubes[k][4] = box_collision(cubes[k], cubes[i])[2]
                                     end
                                 end
                             end
                             fenetre:invalidate()
                         end

function box_collision(cube1, cube2)
    if (cube1[1] <= cube2[1] + cW and cube1[2] + cW > cube2[2] and cube1[2] < cube2[2] + cW) or  (cube1[1] + cW >= cube2[1] and cube1[2] + cW > cube2[2] and cube1[2] < cube2[2] + cW) then -- a gauche
        return {-cube1[3], cube1[4]}
    elseif (cube1[2] + cW >= cube2[2] and cube1[1] + cW > cube2[1] and cube1[1] < cube2[1] + cW) or  (cube1[2] <= cube1[2] + cW and cube1[1] + cW > cube2[1] and cube1[1] < cube2[1] + cW)  then -- en bas
        return {cube1[3], -cube1[4]}
    else
        return {cube1[3], cube1[4]}   
    end
end


function on.paint(gc)
    paints[currentscreen](gc)
end

function on.timer()
    timers[currentscreen]()
end
User avatar
AnToX98Premium
Niveau 14: CI (Calculateur de l'Infini)
Niveau 14: CI (Calculateur de l'Infini)
Level up: 75.5%
 
Posts: 1022
Images: 15
Joined: 19 May 2013, 16:54
Location: Paris, France
Gender: Male
Calculator(s):
MyCalcs profile
Class: 1ere S

Re: Collisions en Lua

Unread postby pierrotdu18 » 05 Mar 2014, 10:27

J'ai pas compris ta fonction de collision...
Si le cube n°1 va vers le nord ouest, alors cubes.1[3]=m et cubes.1[4]=m, car je fonctionne avec p ou m (plus ou moins)...
Bonjour
User avatar
pierrotdu18Premium
Niveau 15: CC (Chevalier des Calculatrices)
Niveau 15: CC (Chevalier des Calculatrices)
Level up: 40.5%
 
Posts: 975
Joined: 07 Nov 2013, 20:18
Location: Paris V
Gender: Male
Calculator(s):
MyCalcs profile
Class: MP* Lycée Henri IV

Re: Collisions en Lua

Unread postby AnToX98 » 05 Mar 2014, 17:27

Si le cube touche le bas ou le haut de l'autre on inverse cubes[i][4] (y), si le cube touche la droite ou la gauche du cube on inverse cubes[i][3] (x)
User avatar
AnToX98Premium
Niveau 14: CI (Calculateur de l'Infini)
Niveau 14: CI (Calculateur de l'Infini)
Level up: 75.5%
 
Posts: 1022
Images: 15
Joined: 19 May 2013, 16:54
Location: Paris, France
Gender: Male
Calculator(s):
MyCalcs profile
Class: 1ere S

Re: Collisions en Lua

Unread postby technolapin » 06 Mar 2014, 09:44

C'est juste tellement galère les collision... :p
J'ai déjà créé un topic à ce sujet il n'y a pas si longtemps (mais j'ai pas la réponse).
Tu y trouvera peut-être des choses dites qui pourront t'aider: viewtopic.php?f=19&t=13661
Votez Kasane Teto 2017
User avatar
technolapin
Niveau 13: CU (Calculateur Universel)
Niveau 13: CU (Calculateur Universel)
Level up: 97.8%
 
Posts: 514
Images: 25
Joined: 31 Dec 2012, 10:48
Location: Chez moi
Gender: Male
Calculator(s):
MyCalcs profile
Class: Chui en vacance ducon

Re: Collisions en Lua

Unread postby pierrotdu18 » 06 Mar 2014, 11:50

Bon j'ai un peu amélioré, est-ce qu'une gente personne pourrait m'expliquer pourquoi ça marche toujours pas? :'(

EDIT : Il y a des fois où ça marche niquel, mais la plupart du temps les cubes bleus n'entrent pas en collisions...

Code: Select all
    -------------------
    --Screen--Manager--
    -------------------
    screens = { menu = 1,
                playing = 2,
                gameover = 3 }
    currentscreen = 2

    paints = { }
    timers = { }
    enterKeys = { }
    arrowUps = { }
    arrowDowns = { }
    arrowLefts = { }
    arrowRights = { }
    mouseMoves = { }

    -------------------
    ------Globals------
    -------------------
    w, h = 318, 212
    function on.resize ( ww , hh )
       w , h = ww , hh
    end

    local fenetre = platform.window

    ----------------------
    --TheGame--Variables--
    ----------------------
    vitglob = 1.5
    cW = 20
    p , m = 1 , -1

    bleu = { (w - ( h - h / 12 ) ) / 2 , w - ( w - ( h - h / 12 ) ) / 2 , (h - ( h - h / 12 ) ) / 2 , h - (h - ( h - h / 12 ) ) / 2 }

    rouge = { (w - ( h - h / 4 ) ) / 2 , w - ( w - ( h - h / 4 ) ) / 2 , (h - ( h - h / 4 ) ) / 2 , h - (h - ( h - h / 4 ) ) / 2 }


    cubes = { { rouge[1] + 10 ,      rouge[3] + 10 ,       m , m  , 1 ,   3/2 },
              { rouge[1] + 10 ,      rouge[4] - cW - 10 ,  m , m  , 1/2 , 2/3 },
              { rouge[2] - cW - 10 , rouge[3] + 10 ,       m , m  , 3/2 , 1/2 },
              { rouge[2] - cW - 10 , rouge[4] - cW - 10,   m , m  , 1/3 , 1   } }

    cube = { w / 2 - 10 , h / 2 - 10 }

    function drawCenterSquare ( gc , larg )
        gc: drawRect ( ( w - larg ) / 2 , ( h - larg ) / 2 , larg , larg )
    end

    function degrade ( gc , larg , thickness , r , g , b)
        local k
        gc: setPen("thin" , "smooth")
        for k=0,thickness do
            gc: setColorRGB( test(r + k * 10,k) , test(g + k * 10,k) , test(b + k * 10,k))
            drawCenterSquare(gc , larg + k)
        end
    end

    function test(a,k)
        if a + k * 10 <= 255 then
            return a + k * 10
        else
            return 255
        end
    end

    function on.enterKey()
    timer.start(0.02)
    end



    function on.paint(gc)
        paints[currentscreen](gc)
    end

    function on.timer()
        timers[currentscreen]()
    end
    function on.mouseMove(x,y)
        mouseMoves[currentscreen](x,y)
    end
    function on.arrowUp()
        arrowUps[currentscreen]()
    end
    function on.arrowDown()
        arrowDowns[currentscreen]()
    end
    function on.arrowLeft()
        arrowLefts[currentscreen]()
    end
    function on.arrowRight()
        arrowRights[currentscreen]()
    end

    paints[screens.playing] = function(gc)
                                  gc: setColorRGB(0 , 0 , 0)
                                  gc: fillRect(0 , 0 , w , h)

                                  degrade(gc , h - h / 12 , 10 , 0 , 0 , 255)
                                 
                                  if collision_rouge() then
                                      gc: setFont("serif", "b", 30)
                                      gc: setColorRGB(255 , 255 , 255)
                                      gc: drawString("PERDU" , w / 2 - gc:getStringWidth("PERDU") / 2 , h / 2)
                                  end
                                 
                                  degrade(gc , h - h / 4 , 10 , 255 , 0 , 0)
                                 
                                  gc: setColorRGB(255,0,0)
                                  gc: setPen("thin" , "smooth")
                                  gc: drawRect(cube[1] , cube[2] , cW , cW)
                                  gc: setColorRGB(255 , 193 , 179)
                                  gc: fillRect(cube[1] + 1 , cube[2] + 1 ,cW - 1 , cW - 1)
                                 
                                  gc: setPen("thin" , "smooth")
                                  for k,v in pairs(cubes) do
                                      gc: setColorRGB(0 , 0 , 255)
                                      gc: drawRect(v[1] , v[2] , cW , cW)
                                      gc: setColorRGB(159 , 196 , 255)
                                      gc: fillRect(v[1] + 1 , v[2] + 1 , cW - 1 , cW - 1)
                                  end
                              end
    timers[screens.playing] = function ()
                                 for i = 1, #cubes do
                                     cubes[i][1] = cubes[i][1] + (cubes[i][3]) * vitglob                         
                                     cubes[i][2] = cubes[i][2] + (cubes[i][4]) * vitglob
                                 end
                                 for i = 1, #cubes do
                                     if cubes[i][1] <= bleu[1] then
                                         cubes[i][3] = p * cubes[i][5]                                     
                                     elseif cubes[i][1] >= bleu[2] - cW then
                                         cubes[i][3] = m * cubes[i][5]
                                     end
                                     if cubes[i][2] <= bleu[3] then
                                         cubes[i][4] = p * cubes[i][6]
                                     elseif cubes[i][2] >= bleu[4] - cW then
                                         cubes[i][4] = m * cubes[i][6]
                                     end
                                 end
                                 for k=1,#cubes do
                                     if collision_bleu(cubes[k])~=false then
                                         if collisionType_bleu(cubes[k],cubes[collision_bleu(cubes[k])]) == "width" then
                                             cubes[k][3] = - cubes[k][3]
                                         else
                                             cubes[k][4] = - cubes[k][4]
                                         end
                                     end
                                 end
                                 fenetre:invalidate()
                              end
    mouseMoves[screens.playing] = function(x , y)
                                     cube[1] = x - cW / 2
                                     cube[2] = y - cW / 2
                                  end
    arrowUps[screens.playing] = function () cube[2] = cube[2] - 10 end
    arrowDowns[screens.playing] = function () cube[2] = cube[2] + 10 end
    arrowLefts[screens.playing] = function () cube[1] = cube[1] - 10 end
    arrowRights[screens.playing] = function () cube[1] = cube[1] + 10 end

    function collision_rouge()
        local x = cube[1]
        local y = cube[2]
        --Carré dépasse le cadre rouge
        if x <= rouge[1] or x + cW >= rouge[2] or y <= rouge[3] or y + cW >= rouge[4] then
            return true
        end
        --Touche un autre carré
        for k , v in pairs(cubes) do
            local cx = v[1]
            local cy = v[2]
            if x >= cx - cW and x <= cx + cW and y >= cy - cW and y <= cy + cW then
                    return true
            end
        end
        return false
    end
    function collision_bleu(obj)
        local x = obj[1]
        local y = obj[2]
        for k , v in pairs(cubes) do
            local cx = v[1]
            local cy = v[2]
            if not (x==cx and y==cy) then
                if x >= cx - cW and x <= cx + cW and y >= cy - cW and y <= cy + cW then
                    return k
                else
                    return false
                end
            end
        end
    end
    function collisionType_bleu(obj1,obj2)
        local x1 = obj1[1]
        local y1 = obj1[2]
        local x2 = obj2[1]
        local y2 = obj2[2]
        if not (y2 + cW<=y1+2 or y2+2>=y1 + cW) then
            return "width"
        else
            return "height"
        end
    end
Bonjour
User avatar
pierrotdu18Premium
Niveau 15: CC (Chevalier des Calculatrices)
Niveau 15: CC (Chevalier des Calculatrices)
Level up: 40.5%
 
Posts: 975
Joined: 07 Nov 2013, 20:18
Location: Paris V
Gender: Male
Calculator(s):
MyCalcs profile
Class: MP* Lycée Henri IV


Return to Nspire-Lua

Who is online

Users browsing this forum: ClaudeBot [spider] and 1 guest

-
Search
-
Social TI-Planet
-
Featured topics
Comparaisons des meilleurs prix pour acheter sa calculatrice !
"1 calculatrice pour tous", le programme solidaire de Texas Instruments. Reçois gratuitement et sans aucune obligation d'achat, 5 calculatrices couleur programmables en Python à donner aux élèves les plus nécessiteux de ton lycée. Tu peux recevoir au choix 5 TI-82 Advanced Edition Python ou bien 5 TI-83 Premium CE Edition Python.
Enseignant(e), reçois gratuitement 1 exemplaire de test de la TI-82 Advanced Edition Python. À demander d'ici le 31 décembre 2024.
Aidez la communauté à documenter les révisions matérielles en listant vos calculatrices graphiques !
1234
-
Donations / Premium
For more contests, prizes, reviews, helping us pay the server and domains...
Donate
Discover the the advantages of a donor account !
JoinRejoignez the donors and/or premium!les donateurs et/ou premium !


Partner and ad
Notre partenaire Jarrety Calculatrices à acheter chez Calcuso
-
Stats.
2304 utilisateurs:
>2257 invités
>41 membres
>6 robots
Record simultané (sur 6 mois):
6892 utilisateurs (le 07/06/2017)
-
Other interesting websites
Texas Instruments Education
Global | France
 (English / Français)
Banque de programmes TI
ticalc.org
 (English)
La communauté TI-82
tout82.free.fr
 (Français)