
Thanks!
Not helping me much, I have never done any kind of graphic programming. I know I have to use ATan() and that's about it, I can code any desktop software. I'm just new to 2D/3D game programming. It would be ten times more beneficial to me (for me) if someone could post an example of what I am trying to do (would help if using the variables and constants I posted above). Some people like reading hours of material to learn how to do a single thing, I am not like that. I have a photographic memory when it comes to learning. Meaning once I see how its done, I will know how its done for future use (also leads to learning how to improve/modify it). Below is an example of what I am trying to do, update the angle on mouse movement so the sprite can be rotated every frame (smooth and precise aim). I recon I would need to be using this same angle for drawing my bullets as well (one step at a time, I can most likely figure that out on my own). Tho it may look like I got it down pat, my game doesn't no where function as I intend. The picture is to only give you reference of the type of angle calculation that I need (from player x\y to crosshair x\y).luis wrote:The difference between the two points gives you a vector, this is the distance between the two points, if you need it.
Then you calc the slope of the line passing through the two points.
The arcotangent of the slope is the angle (direction) of the vector.
Look on internet for a course on 2d vectors (it's easy stuff, don' be alarmed), coding the answer by yourself would be better and you'll learn something you can use again in the future
This is very nice (skip the matrices and read only the vectors parts) -> http://programmedlessons.org/VectorLessons/
Code: Select all
;PB 4.02 6 mai 2007
;updated for 5.11
EnableExplicit ; Force la déclaration de toutes les variables
Structure Vecteur
x.f
y.f
EndStructure
;Declare les procédures
Declare Norme(*V.Vecteur)
;Initialise l'environnement nécessaire au fonctionnement des sprites et pour ouvrir un écran.
InitSprite()
;Initialise l'environnement propre à la gestion du clavier.
InitKeyboard()
;Initialise l'environnement propre à la gestion de la souris.
InitMouse()
;Initialise l'environnement propre à la gestion des sprites 3D.
InitSprite3D()
;Ouvre un nouvel écran avec les caractéristiques Largeur, Hauteur et Profondeur.
ExamineDesktops()
OpenScreen(DesktopWidth(0),DesktopHeight(0),DesktopDepth(0),"Tut Sprite3D et Souris")
;-Déclaration des variables
Define Angle, dist, Vitesse.f = 3
Define.Vecteur Direction, Sprite, Destination
Macro DISTANCE(p1, p2)
Sqr((p1\x - p2\x) * (p1\x - p2\x) + (p1\y - p2\y) * (p1\y - p2\y))
EndMacro
;Creation d'une texture
CreateSprite(0,64,64,#PB_Sprite_Texture)
;On dessine un triangle plein dans la texture
StartDrawing(SpriteOutput(0))
LineXY(1, 1, 1, SpriteHeight(0)-2,RGB(255,255,0))
LineXY(1, 1, SpriteWidth(0)-2, SpriteHeight(0)/2,RGB(255,255,0))
LineXY(1, SpriteHeight(0)-2, SpriteWidth(0)-2, SpriteHeight(0)/2,RGB(255,255,0))
FillArea(SpriteWidth(0)/2,SpriteHeight(0)/2,RGB(255,255,0),RGB(255,155,0))
StopDrawing()
;Creation d'un sprite3D utilisant la texture définie précédemment
CreateSprite3D(0,0)
;Sprite pour la souris
CreateSprite(1,5,5)
StartDrawing(SpriteOutput(1))
Box(0,0,5,5,RGB(0,255,0))
StopDrawing()
Repeat
;Inverse le buffer d'arrière plan avec le buffer visible à l'écran.
;La partie invisible du buffer remplace alors complètement La partie visible.
FlipBuffers()
;Efface l'écran courant avec la couleur specifiée.
ClearScreen(RGB(0,0,100))
;Met à jour l'état du clavier.Cette fonction doit être appelée avant d'utiliser les commandes
;KeyboardInkey(), KeyboardPushed() et KeyboardReleased().
ExamineKeyboard()
;Destination
If ExamineMouse()
Destination\x = MouseX()
Destination\y = MouseY()
EndIf
;Direction
Direction\x = Destination\x - Sprite\x
Direction\y = Destination\y - Sprite\y
Angle=Degree(ATan2(Direction\x, Direction\y))
Norme(@Direction)
;Deplacement sprite
dist = DISTANCE(Destination, Sprite)
If dist < Vitesse
Sprite\x + Direction\x * dist
Sprite\y + Direction\y * dist
Else
Sprite\x + Direction\x * Vitesse
Sprite\y + Direction\y * Vitesse
EndIf
;Affiche le sprite 3D
If Start3D()
DisplaySprite3D(0, Sprite\x, Sprite\y)
RotateSprite3D(0,Angle,0)
Stop3D()
EndIf
;Affiche la souris
DisplaySprite(1, Destination\x, Destination\y)
Until KeyboardPushed(#PB_Key_Escape)
End
Procedure Norme(*V.Vecteur)
Define.f Norme
Norme.f = Sqr(*V\x * *V\x + *V\y * *V\y)
If Norme
*V\x / Norme
*V\y / Norme
EndIf
EndProcedure
Thanks this will help a lot.Comtois wrote:may be this can help ?
Code: Select all
;PB 4.02 6 mai 2007 ;updated for 5.11 EnableExplicit ; Force la déclaration de toutes les variables Structure Vecteur x.f y.f EndStructure ;Declare les procédures Declare Norme(*V.Vecteur) ;Initialise l'environnement nécessaire au fonctionnement des sprites et pour ouvrir un écran. InitSprite() ;Initialise l'environnement propre à la gestion du clavier. InitKeyboard() ;Initialise l'environnement propre à la gestion de la souris. InitMouse() ;Initialise l'environnement propre à la gestion des sprites 3D. InitSprite3D() ;Ouvre un nouvel écran avec les caractéristiques Largeur, Hauteur et Profondeur. ExamineDesktops() OpenScreen(DesktopWidth(0),DesktopHeight(0),DesktopDepth(0),"Tut Sprite3D et Souris") ;-Déclaration des variables Define Angle, dist, Vitesse.f = 3 Define.Vecteur Direction, Sprite, Destination Macro DISTANCE(p1, p2) Sqr((p1\x - p2\x) * (p1\x - p2\x) + (p1\y - p2\y) * (p1\y - p2\y)) EndMacro ;Creation d'une texture CreateSprite(0,64,64,#PB_Sprite_Texture) ;On dessine un triangle plein dans la texture StartDrawing(SpriteOutput(0)) LineXY(1, 1, 1, SpriteHeight(0)-2,RGB(255,255,0)) LineXY(1, 1, SpriteWidth(0)-2, SpriteHeight(0)/2,RGB(255,255,0)) LineXY(1, SpriteHeight(0)-2, SpriteWidth(0)-2, SpriteHeight(0)/2,RGB(255,255,0)) FillArea(SpriteWidth(0)/2,SpriteHeight(0)/2,RGB(255,255,0),RGB(255,155,0)) StopDrawing() ;Creation d'un sprite3D utilisant la texture définie précédemment CreateSprite3D(0,0) ;Sprite pour la souris CreateSprite(1,5,5) StartDrawing(SpriteOutput(1)) Box(0,0,5,5,RGB(0,255,0)) StopDrawing() Repeat ;Inverse le buffer d'arrière plan avec le buffer visible à l'écran. ;La partie invisible du buffer remplace alors complètement La partie visible. FlipBuffers() ;Efface l'écran courant avec la couleur specifiée. ClearScreen(RGB(0,0,100)) ;Met à jour l'état du clavier.Cette fonction doit être appelée avant d'utiliser les commandes ;KeyboardInkey(), KeyboardPushed() et KeyboardReleased(). ExamineKeyboard() ;Destination If ExamineMouse() Destination\x = MouseX() Destination\y = MouseY() EndIf ;Direction Direction\x = Destination\x - Sprite\x Direction\y = Destination\y - Sprite\y Angle=Degree(ATan2(Direction\x, Direction\y)) Norme(@Direction) ;Deplacement sprite dist = DISTANCE(Destination, Sprite) If dist < Vitesse Sprite\x + Direction\x * dist Sprite\y + Direction\y * dist Else Sprite\x + Direction\x * Vitesse Sprite\y + Direction\y * Vitesse EndIf ;Affiche le sprite 3D If Start3D() DisplaySprite3D(0, Sprite\x, Sprite\y) RotateSprite3D(0,Angle,0) Stop3D() EndIf ;Affiche la souris DisplaySprite(1, Destination\x, Destination\y) Until KeyboardPushed(#PB_Key_Escape) End Procedure Norme(*V.Vecteur) Define.f Norme Norme.f = Sqr(*V\x * *V\x + *V\y * *V\y) If Norme *V\x / Norme *V\y / Norme EndIf EndProcedure
You would learn to do a lot more than a single thing, how all fits together, and how to build upon that.Blankname wrote:Some people like reading hours of material to learn how to do a single thing, I am not like that.
Code: Select all
Structure XY
X.f
Y.f
EndStructure
Procedure V2_VectorFromPoints (*p1.XY, *p2.XY, *vout.XY)
*vout\X = *p2\X - *p1\X
*vout\Y = *p2\Y - *p1\Y
EndProcedure
Procedure.f V2_GetLength (*v.XY)
ProcedureReturn Sqr((*v\X)*(*v\X) + (*v\Y)*(*v\Y))
EndProcedure
Procedure.f V2_GetAngle (*v.XY)
Protected fAngle.f = Degree(ATan(*v\Y / *v\X))
If *V\X < 0.0 And *V\Y > 0.0
; Quadrant II
fAngle = 180.0 + fAngle
ElseIf *V\X < 0.0 And *V\Y < 0.0
; Quadrant III
fAngle = 180.0 + fAngle
ElseIf *V\X > 0.0 And *V\Y < 0.0
; Quadrant IV
fAngle = 360.0 + fAngle
EndIf
ProcedureReturn fAngle
EndProcedure
; EXAMPLE
Define you.XY ; your position
Define target.XY ; the target position
Define V.XY ; the vector from the two points
you\x = 100
you\y = 100
target\x = 150
target\y = 150
V2_VectorFromPoints (@you, @target, @V)
Debug V2_GetLength (@V)
Debug V2_GetAngle (@V)
It's one the best sensations I knowem_uk wrote: I would never have thought of using code like this before, but the more I read and study the more I can feel my brain catching on.