AlignToVector Dosn't translate angles right.

Blitz3D Forums/Blitz3D Beginners Area/AlignToVector Dosn't translate angles right.

Figment(Posted 2003) [#1]
Okay Using AlignToVector to place a ball in this breakoutgame I'm coding doesn't seem to give the right angles when a collision occurs with the playingfield boundries, most notabaly on the upper part of the playfield.
the ball will come in at an angle of 50-70 degrees colide and after collision instead of coming out at the same angle reversed, it comes out on a totally different less steep angle like 30 or 40 degrees. Heres the code.
Graphics3D 800,600

Const CUBE_COL=1
Const SPHERE_COL=2
Global sy#=2
Global sx#=0

SetBuffer BackBuffer()
Global plength=25
Global camera=CreateCamera()
CameraViewport camera,0,0,800,600
PositionEntity camera,0,0,-5
Global vtimer=0
Global light=CreateLight()

;create arena
Global leftwall=CreateCube()
EntityColor leftwall,100,100,100
ScaleEntity leftwall,4,200,4
PositionEntity leftwall,-200,20,0
Global rightwall=CreateCube()
EntityColor rightwall,100,100,100
ScaleEntity rightwall,4,200,4
PositionEntity rightwall,200,20,0
Global topwall=CreateCube()
EntityColor topwall,100,100,100
ScaleEntity topwall,200,4,4
PositionEntity topwall,0,-150,0
EntityType leftwall,CUBE_COL
EntityType rightwall,CUBE_COL
EntityType topwall,CUBE_COL
Global x1# = 0

;Players Paddle
Global cube=CreateCube()
EntityColor cube,70,80,190
ScaleEntity cube,plength,2,2
EntityType cube,CUBE_COL

;Players Ball
Global sphere=CreateSphere(12)
EntityColor sphere,170,80,90
ScaleEntity sphere,2,2,2
EntityType sphere,SPHERE_COL
PositionEntity sphere,0,110,0

Collisions SPHERE_COL,CUBE_COL,2,1
PositionEntity camera,0,100,1
	RotateEntity camera,80,0,0
	PositionEntity camera,0,0,200
	RotateEntity camera,180,0,0

While Not KeyHit(1)
Update_Player()
Update_Ball()

UpdateWorld
RenderWorld


Text 30,30, "x:"+EntityX(sphere)
Text 30,50, "y:"+EntityY(sphere)
Text 30,70, "cx:"
Text 30,90, "cy:"
Text 335,500,"Collision Detection"
Flip
x1#=x1#+MouseXSpeed()
Wend

Function Update_Ball()
MoveEntity sphere,sx#,sy#,0

; Calculate bounce: 
If EntityCollided(sphere,CUBE_COL) Then
	If EntityCollided(cube,SPHERE_COL)
		If EntityX(sphere) => EntityX(cube) And EntityX(sphere) < EntityX(cube) +5
			sx#=0
			sx#=CollisionNX(cube,1)+2
		ElseIf EntityX(sphere) => EntityX(cube)+5 And EntityX(sphere) <= EntityX(cube)+10 
			sx#=0
			sx#=CollisionNX(cube,1)+1
		ElseIf EntityX(sphere) => EntityX(cube)+11 And EntityX(sphere) =< EntityX(cube)+15
			sx#=0
			sx#=CollisionNX(cube,1)+.5
		ElseIf EntityX(sphere) => EntityX(cube)+16 And EntityX(sphere) =< EntityX(cube)+20
			sx#=0
			sx#=CollisionNX(cube,1)-1
		ElseIf EntityX(sphere) => EntityX(cube)+21 And EntityX(sphere) =< EntityX(cube)+25
			sx#=0
			sx#=CollisionNX(cube,1)-2
		;ElseIf EntityX(sphere) > EntityX(cube)+26
		;	sx#=0
		;	sx#=CollisionNX(cube,1)-3


		EndIf
	EndIf
	    AlignToVector sphere,CollisionNX(sphere,1) ,CollisionNY(sphere,1),0,1


EndIf


End Function

Function Update_Player()
	MoveMouse GraphicsHeight()/2,GraphicsWidth()/2
	If x1#<-180 Then x1#=-180
	If x1#>180 Then x1#=180
PositionEntity cube,x1#,122,0
End Function

End


So can anyone tell me ifI'm doing something wrong or is this just the way that B3D's alignvectors function behaves?
*scratchin my head*


Ross C(Posted 2003) [#2]
i dunno much about aligntovector. but you should try doing this, as an alternative to aligntovector.

use an angle variable.
angle=45

use sin and cos to move the ball in the x and y direction
x=x+sin(angle)*ballspeed
Translateentity ball,x,y,0


then check for a collision. if a collision occurs, the reverse the angle on the x axis

move the ball in the y direction
y=y+cos(angle)*ballspeed)
TranslateEntity ball,x,y,0


again check for collisions, if there is one then reverse the angle along the y axis