Synopsis d'un casse brique en 3d

Soyez le premier à donner votre avis sur cette source.

Snippet vu 5 071 fois - Téléchargée 37 fois

Contenu du snippet

ce code est le début d'un casse brique en 3D.
il gère l'affichage linéaire en 3D,
la déplacement de la balle(un cube pour l'instant),
le déplacement de la raquette(un pavé)
il emmet un bip lorsque l'on devrait perdre la balle.
les brique ne sont pas encore gérées.

Le code suivant est à metre dans une form.

Source / Exemple :


'Option Explicit

Private Const pi = 3.14159265358979
Private Type point3d
    X As Double
    Y As Double
    z As Double
End Type
Private Type fline
    p1 As point3d
    p2 As point3d
End Type
Private Type cube
    p1 As point3d
    p2 As point3d
    p3 As point3d
    p4 As point3d
    p5 As point3d
    p6 As point3d
    p7 As point3d
    p8 As point3d
End Type
Private Type carre
    p1 As point3d
    p2 As point3d
    p3 As point3d
    p4 As point3d
End Type

Dim grand_cube As cube
Dim centre As point3d
Dim angle As point3d
Dim balle As cube
Dim affballe As cube
Dim dirballe As point3d
Dim hballe As carre
Dim hballe2 As carre
Dim hballe3 As carre
Dim hballe4 As carre
Dim hballe5 As carre
Dim hballe6 As carre
Dim raq As cube
Dim dirraq As point3d
Dim xx As Double
Dim yy As Double
Dim xxx As Double
Dim yyy As Double

Private Sub deplace(pointdep As point3d, pointrot As point3d, angle As point3d, pointarr As point3d)
    Dim tmp As point3d

    pointarr.X = pointdep.X - pointrot.X
    pointarr.Y = pointdep.Y - pointrot.Y
    pointarr.z = pointdep.z - pointrot.z

    tmp = pointarr

    pointarr.z = Sqr(tmp.z ^ 2 + tmp.Y ^ 2) * Cos(angle.X + angled(tmp.z, tmp.Y))
    pointarr.Y = Sqr(tmp.z ^ 2 + tmp.Y ^ 2) * Sin(angle.X + angled(tmp.z, tmp.Y))

    tmp = pointarr

    pointarr.X = Sqr(tmp.X ^ 2 + tmp.z ^ 2) * Cos(angle.Y + angled(tmp.X, tmp.z))
    pointarr.z = Sqr(tmp.X ^ 2 + tmp.z ^ 2) * Sin(angle.Y + angled(tmp.X, tmp.z))

    tmp = pointarr

    pointarr.X = Sqr(tmp.X ^ 2 + tmp.Y ^ 2) * Cos(angle.z + angled(tmp.X, tmp.Y))
    pointarr.Y = Sqr(tmp.X ^ 2 + tmp.Y ^ 2) * Sin(angle.z + angled(tmp.X, tmp.Y))

    pointarr.X = pointarr.X + pointrot.X
    pointarr.Y = pointarr.Y + pointrot.Y
    pointarr.z = pointarr.z + pointrot.z
End Sub

'x et y sont 2 point d'une droite passant par l'origine.
'le résultat est l'angle de la droite par rapport à l'orizontal.
Private Function angled(ByVal X As Double, ByVal Y As Double) As Double
    Select Case X
        Case Is > 0
            angled = Atn(Y / X)
        Case Is < 0
            angled = Atn(Y / X) + pi '=-Atn(y / x)
        Case Else
            If Y > 0 Then
                angled = pi / 2
            Else
                angled = 3 * pi / 2
            End If
    End Select
End Function

Private Sub affcube(obj As Form, cube1 As cube, couleur As Long, pointrot As point3d, angle As point3d)
    Dim cube As cube
    Call movecube(cube1, pointrot, angle, cube)
    obj.Line (cube.p1.X, cube.p1.Y)-(cube.p2.X, cube.p2.Y), couleur
    obj.Line (cube.p2.X, cube.p2.Y)-(cube.p3.X, cube.p3.Y), couleur
    obj.Line (cube.p3.X, cube.p3.Y)-(cube.p4.X, cube.p4.Y), couleur
    obj.Line (cube.p4.X, cube.p4.Y)-(cube.p1.X, cube.p1.Y), couleur
    obj.Line (cube.p5.X, cube.p5.Y)-(cube.p6.X, cube.p6.Y), couleur
    obj.Line (cube.p6.X, cube.p6.Y)-(cube.p7.X, cube.p7.Y), couleur
    obj.Line (cube.p7.X, cube.p7.Y)-(cube.p8.X, cube.p8.Y), couleur
    obj.Line (cube.p8.X, cube.p8.Y)-(cube.p5.X, cube.p5.Y), couleur
    obj.Line (cube.p1.X, cube.p1.Y)-(cube.p5.X, cube.p5.Y), couleur
    obj.Line (cube.p2.X, cube.p2.Y)-(cube.p6.X, cube.p6.Y), couleur
    obj.Line (cube.p3.X, cube.p3.Y)-(cube.p7.X, cube.p7.Y), couleur
    obj.Line (cube.p4.X, cube.p4.Y)-(cube.p8.X, cube.p8.Y), couleur
End Sub

Private Sub movecube(cubedep As cube, pointrot As point3d, angle As point3d, cubearr As cube)
    Call deplace(cubedep.p1, pointrot, angle, cubearr.p1)
    Call deplace(cubedep.p2, pointrot, angle, cubearr.p2)
    Call deplace(cubedep.p3, pointrot, angle, cubearr.p3)
    Call deplace(cubedep.p4, pointrot, angle, cubearr.p4)
    Call deplace(cubedep.p5, pointrot, angle, cubearr.p5)
    Call deplace(cubedep.p6, pointrot, angle, cubearr.p6)
    Call deplace(cubedep.p7, pointrot, angle, cubearr.p7)
    Call deplace(cubedep.p8, pointrot, angle, cubearr.p8)
End Sub

Private Sub affcarre(obj As Form, carre1 As carre, couleur As Long, pointrot As point3d, angle As point3d)
    Dim carre As carre
    Call movecarre(carre1, pointrot, angle, carre)
    obj.Line (carre.p1.X, carre.p1.Y)-(carre.p2.X, carre.p2.Y), couleur
    obj.Line (carre.p2.X, carre.p2.Y)-(carre.p3.X, carre.p3.Y), couleur
    obj.Line (carre.p3.X, carre.p3.Y)-(carre.p4.X, carre.p4.Y), couleur
    obj.Line (carre.p4.X, carre.p4.Y)-(carre.p1.X, carre.p1.Y), couleur

End Sub

Private Sub movecarre(carredep As carre, pointrot As point3d, angle As point3d, carrearr As carre)
    Call deplace(carredep.p1, pointrot, angle, carrearr.p1)
    Call deplace(carredep.p2, pointrot, angle, carrearr.p2)
    Call deplace(carredep.p3, pointrot, angle, carrearr.p3)
    Call deplace(carredep.p4, pointrot, angle, carrearr.p4)
End Sub

Private Sub Form_Load()
centre.X = 0
centre.Y = 0
centre.z = 0
angle.X = -0.2
angle.Y = -0.3

balle.p1.X = 2000
balle.p1.Y = 2000
balle.p1.z = 300
balle.p2.X = 2600
balle.p2.Y = 2000
balle.p2.z = 300
balle.p3.X = 2600
balle.p3.Y = 2600
balle.p3.z = 300
balle.p4.X = 2000
balle.p4.Y = 2600
balle.p4.z = 300
balle.p5.X = 2000
balle.p5.Y = 2000
balle.p5.z = -300
balle.p6.X = 2600
balle.p6.Y = 2000
balle.p6.z = -300
balle.p7.X = 2600
balle.p7.Y = 2600
balle.p7.z = -300
balle.p8.X = 2000
balle.p8.Y = 2600
balle.p8.z = -300
dirballe.X = 40
dirballe.Y = 30
dirballe.z = 20

grand_cube.p1.X = 1000
grand_cube.p1.Y = 1000
grand_cube.p1.z = 2000
grand_cube.p2.X = 5000
grand_cube.p2.Y = 1000
grand_cube.p2.z = 2000
grand_cube.p3.X = 5000
grand_cube.p3.Y = 5000
grand_cube.p3.z = 2000
grand_cube.p4.X = 1000
grand_cube.p4.Y = 5000
grand_cube.p4.z = 2000
grand_cube.p5.X = 1000
grand_cube.p5.Y = 1000
grand_cube.p5.z = -2000
grand_cube.p6.X = 5000
grand_cube.p6.Y = 1000
grand_cube.p6.z = -2000
grand_cube.p7.X = 5000
grand_cube.p7.Y = 5000
grand_cube.p7.z = -2000
grand_cube.p8.X = 1000
grand_cube.p8.Y = 5000
grand_cube.p8.z = -2000

raq.p1.X = 2000
raq.p1.Y = grand_cube.p4.Y
raq.p1.z = 500
raq.p2.X = 3000
raq.p2.Y = grand_cube.p4.Y
raq.p2.z = 500
raq.p3.X = 3000
raq.p3.Y = grand_cube.p4.Y - 100
raq.p3.z = 500
raq.p4.X = 2000
raq.p4.Y = grand_cube.p4.Y - 100
raq.p4.z = 500
raq.p5.X = 2000
raq.p5.Y = grand_cube.p4.Y
raq.p5.z = -500
raq.p6.X = 3000
raq.p6.Y = grand_cube.p4.Y
raq.p6.z = -500
raq.p7.X = 3000
raq.p7.Y = grand_cube.p4.Y - 100
raq.p7.z = -500
raq.p8.X = 2000
raq.p8.Y = grand_cube.p4.Y - 100
raq.p8.z = -500

hballe.p1.Y = grand_cube.p4.Y
hballe.p2.Y = grand_cube.p4.Y
hballe.p3.Y = grand_cube.p4.Y
hballe.p4.Y = grand_cube.p4.Y

hballe2.p1.Y = grand_cube.p1.Y
hballe2.p2.Y = grand_cube.p1.Y
hballe2.p3.Y = grand_cube.p1.Y
hballe2.p4.Y = grand_cube.p1.Y

hballe3.p1.z = grand_cube.p1.z
hballe3.p2.z = grand_cube.p1.z
hballe3.p3.z = grand_cube.p1.z
hballe3.p4.z = grand_cube.p1.z

hballe4.p1.z = grand_cube.p5.z
hballe4.p2.z = grand_cube.p5.z
hballe4.p3.z = grand_cube.p5.z
hballe4.p4.z = grand_cube.p5.z

hballe5.p1.X = grand_cube.p1.X
hballe5.p2.X = grand_cube.p1.X
hballe5.p3.X = grand_cube.p1.X
hballe5.p4.X = grand_cube.p1.X

hballe6.p1.X = grand_cube.p2.X
hballe6.p2.X = grand_cube.p2.X
hballe6.p3.X = grand_cube.p2.X
hballe6.p4.X = grand_cube.p2.X

xxx = raq.p1.X
yyy = raq.p5.Y
xx = Me.Width / 2
yy = Me.Height / 2
Show
DoEvents
Dim i As Integer
1
Cls
'PSet (centre.x, centre.y), RGB(0, 0, 255)
Call affcube(Me, grand_cube, RGB(255, 0, 0), centre, angle)

If balle.p2.X + dirballe.X >= grand_cube.p2.X And dirballe.X > 0 Then dirballe.X = -dirballe.X
If balle.p1.X - dirballe.X <= grand_cube.p1.X And dirballe.X < 0 Then dirballe.X = -dirballe.X

If balle.p4.Y + dirballe.Y >= grand_cube.p4.Y - 100 And dirballe.Y > 0 Then
 'si un des point da la balle est dans la raquette
    If ((balle.p1.X >= raq.p1.X And balle.p1.X <= raq.p2.X) And (balle.p1.z >= raq.p5.z And balle.p1.z <= raq.p1.z)) Then dirballe.Y = -dirballe.Y: GoTo 2
    If ((balle.p2.X >= raq.p1.X And balle.p2.X <= raq.p2.X) And (balle.p2.z >= raq.p5.z And balle.p2.z <= raq.p1.z)) Then dirballe.Y = -dirballe.Y: GoTo 2
    If ((balle.p5.X >= raq.p1.X And balle.p5.X <= raq.p2.X) And (balle.p5.z >= raq.p5.z And balle.p5.z <= raq.p1.z)) Then dirballe.Y = -dirballe.Y: GoTo 2
    If ((balle.p6.X >= raq.p1.X And balle.p6.X <= raq.p2.X) And (balle.p6.z >= raq.p5.z And balle.p6.z <= raq.p1.z)) Then dirballe.Y = -dirballe.Y: GoTo 2
    If balle.p4.Y + dirballe.Y >= grand_cube.p4.Y Then Beep: dirballe.Y = -dirballe.Y ' End 'dirballe.y = -dirballe.y
    
End If
2
If balle.p1.Y - dirballe.Y <= grand_cube.p1.Y And dirballe.Y < 0 Then dirballe.Y = -dirballe.Y

If balle.p1.z + dirballe.z >= grand_cube.p1.z And dirballe.z > 0 Then dirballe.z = -dirballe.z
If balle.p5.z - dirballe.z <= grand_cube.p5.z And dirballe.z < 0 Then dirballe.z = -dirballe.z

balle.p1.X = balle.p1.X + dirballe.X
balle.p2.X = balle.p2.X + dirballe.X
balle.p3.X = balle.p3.X + dirballe.X
balle.p4.X = balle.p4.X + dirballe.X
balle.p5.X = balle.p5.X + dirballe.X
balle.p6.X = balle.p6.X + dirballe.X
balle.p7.X = balle.p7.X + dirballe.X
balle.p8.X = balle.p8.X + dirballe.X
balle.p1.Y = balle.p1.Y + dirballe.Y
balle.p2.Y = balle.p2.Y + dirballe.Y
balle.p3.Y = balle.p3.Y + dirballe.Y
balle.p4.Y = balle.p4.Y + dirballe.Y
balle.p5.Y = balle.p5.Y + dirballe.Y
balle.p6.Y = balle.p6.Y + dirballe.Y
balle.p7.Y = balle.p7.Y + dirballe.Y
balle.p8.Y = balle.p8.Y + dirballe.Y
balle.p1.z = balle.p1.z + dirballe.z
balle.p2.z = balle.p2.z + dirballe.z
balle.p3.z = balle.p3.z + dirballe.z
balle.p4.z = balle.p4.z + dirballe.z
balle.p5.z = balle.p5.z + dirballe.z
balle.p6.z = balle.p6.z + dirballe.z
balle.p7.z = balle.p7.z + dirballe.z
balle.p8.z = balle.p8.z + dirballe.z

raq.p1.X = raq.p1.X + dirraq.X
raq.p2.X = raq.p2.X + dirraq.X
raq.p3.X = raq.p3.X + dirraq.X
raq.p4.X = raq.p4.X + dirraq.X
raq.p5.X = raq.p5.X + dirraq.X
raq.p6.X = raq.p6.X + dirraq.X
raq.p7.X = raq.p7.X + dirraq.X
raq.p8.X = raq.p8.X + dirraq.X
raq.p1.z = raq.p1.z + dirraq.z
raq.p2.z = raq.p2.z + dirraq.z
raq.p3.z = raq.p3.z + dirraq.z
raq.p4.z = raq.p4.z + dirraq.z
raq.p5.z = raq.p5.z + dirraq.z
raq.p6.z = raq.p6.z + dirraq.z
raq.p7.z = raq.p7.z + dirraq.z
raq.p8.z = raq.p8.z + dirraq.z

dirballe.X = dirballe.X * (1 + Rnd / 5000)
dirballe.Y = dirballe.Y * (1 + Rnd / 5000)
dirballe.z = dirballe.z * (1 + Rnd / 5000)

hballe.p1.X = balle.p1.X
hballe.p1.z = balle.p1.z
hballe.p2.X = balle.p2.X
hballe.p2.z = balle.p2.z
hballe.p3.X = balle.p6.X
hballe.p3.z = balle.p6.z
hballe.p4.X = balle.p5.X
hballe.p4.z = balle.p5.z

hballe2.p1.X = balle.p1.X
hballe2.p1.z = balle.p1.z
hballe2.p2.X = balle.p2.X
hballe2.p2.z = balle.p2.z
hballe2.p3.X = balle.p6.X
hballe2.p3.z = balle.p6.z
hballe2.p4.X = balle.p5.X
hballe2.p4.z = balle.p5.z

hballe3.p1.X = balle.p1.X
hballe3.p1.Y = balle.p1.Y
hballe3.p2.X = balle.p2.X
hballe3.p2.Y = balle.p2.Y
hballe3.p3.X = balle.p3.X
hballe3.p3.Y = balle.p3.Y
hballe3.p4.X = balle.p4.X
hballe3.p4.Y = balle.p4.Y

hballe4.p1.X = balle.p1.X
hballe4.p1.Y = balle.p1.Y
hballe4.p2.X = balle.p2.X
hballe4.p2.Y = balle.p2.Y
hballe4.p3.X = balle.p3.X
hballe4.p3.Y = balle.p3.Y
hballe4.p4.X = balle.p4.X
hballe4.p4.Y = balle.p4.Y

hballe5.p1.Y = balle.p1.Y
hballe5.p1.z = balle.p1.z
hballe5.p2.Y = balle.p5.Y
hballe5.p2.z = balle.p5.z
hballe5.p3.Y = balle.p8.Y
hballe5.p3.z = balle.p8.z
hballe5.p4.Y = balle.p4.Y
hballe5.p4.z = balle.p4.z

hballe6.p1.Y = balle.p1.Y
hballe6.p1.z = balle.p1.z
hballe6.p2.Y = balle.p5.Y
hballe6.p2.z = balle.p5.z
hballe6.p3.Y = balle.p8.Y
hballe6.p3.z = balle.p8.z
hballe6.p4.Y = balle.p4.Y
hballe6.p4.z = balle.p4.z

Call affcube(Me, balle, RGB(0, 255, 0), centre, angle)
Call affcube(Me, raq, RGB(127, 127, 255), centre, angle)
Call affcarre(Me, hballe, RGB(127, 127, 127), centre, angle)
Call affcarre(Me, hballe2, RGB(127, 127, 127), centre, angle)
Call affcarre(Me, hballe3, RGB(127, 127, 127), centre, angle)
Call affcarre(Me, hballe4, RGB(127, 127, 127), centre, angle)
Call affcarre(Me, hballe5, RGB(127, 127, 127), centre, angle)
Call affcarre(Me, hballe6, RGB(127, 127, 127), centre, angle)

'Circle (affballe.x, affballe.y), balle.rayon
For i = 0 To 500: DoEvents: Next
GoTo 1
End Sub

Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    If X > xx + 200 Then
        dirraq.X = 50
    ElseIf X < xx - 200 Then
        dirraq.X = -50
    Else
        dirraq.X = 0
    End If
    If Y > yy + 200 Then
        dirraq.z = -50
    ElseIf Y < yy - 200 Then
        dirraq.z = 50
    Else
        dirraq.z = 0
    End If
End Sub

Conclusion :


qu'en pensez-vous ?
comment trouver-vous la fonction 'deplace' ?
(j'ai mis du temps avant de la faire fonctionner, au moin les trois quart du temp total du projet!)

A voir également

Ajouter un commentaire Commentaires
beeline Messages postés 83 Date d'inscription jeudi 20 décembre 2001 Statut Membre Dernière intervention 9 juin 2009
18 juil. 2003 à 10:55
honnetement c'est bien ... bon début
( a par le cube pas droit et la balle qui vas "legerement" trop vite

mais c'est pas comme sa que je le ferait :
regarde les sources d'opengl ou de directx pour de la "vrais" 3d

a la limite trouve toi une verssion de 3d game créator : pour faire ce que tu fait c'est le mieu, en plus c'est du basic
cs_dtom Messages postés 30 Date d'inscription mercredi 22 janvier 2003 Statut Membre Dernière intervention 16 novembre 2003
18 juil. 2003 à 11:01
ces 2 ligne détermine l'angle de vue(en radian) :
angle.X = -0.2
angle.Y = -0.3
ces trois ligne déterminent la vitesse de la balle (la valeur peut etre négative):
dirballe.X = 40
dirballe.Y = 30
dirballe.z = 20
voila pourqui la vitesse de la balle augmente :
dirballe.X = dirballe.X * (1 + Rnd / 5000)
dirballe.Y = dirballe.Y * (1 + Rnd / 5000)
dirballe.z = dirballe.z * (1 + Rnd / 5000)

mais je pense que je fais me tourner vers dx.
PS : si on supprime l'accélération de la balle, ca peut faire un bon écran de veille!
YS1 Messages postés 40 Date d'inscription samedi 12 avril 2003 Statut Membre Dernière intervention 13 mai 2006
18 juil. 2003 à 11:16
Félicitations ! Seulement 3 fautes dans la présentation du code ! (mais il y en 5 dans ton commentaire). Je n'ai pas testé le code. Je ne comprends pas bien pourquoi tu t'acharnes à écrire ce genre de code sans DirectX ou autre...
beeline Messages postés 83 Date d'inscription jeudi 20 décembre 2001 Statut Membre Dernière intervention 9 juin 2009
18 juil. 2003 à 18:06
ch'feré ptet bien dfér 1 éfor moi ossi
mathieumg Messages postés 558 Date d'inscription mardi 4 février 2003 Statut Membre Dernière intervention 18 février 2006
19 juil. 2003 à 04:17
C koi laffaire bleu au debut ? Et on peu po faire close avec le X et moyen.

Merci

Mathieu M-G
http://www.maxicom.ca.tc

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.