Beziers, following them..
BlitzMax Forums/BlitzMax Beginners Area/Beziers, following them..
| ||
So I have my aliens following beziers but since the bezier's points are not evenly distributed the aliens cant seem to maintain the right speed. A friend of mine who knows visual basic told me to find the angle that points towards the next point in the bezier and have the alien fly towards the next point at one pixel per update. I asked him "how do I find the angle?" he gives me this VB Function (which I cant use in BMAX) Public Function getAngle(ByVal x1 As Single, ByVal y1 As Single, ByVal x2 As Single, ByVal y2 As Single) As Single On Error Resume Next Dim xLength As Single Dim yLength As Single xLength = x1 - x2: yLength = y1 - y2 'look for stupid stuff If xLength = 0 Then If yLength > 0 Then getAngle = 0: Exit Function If yLength < 0 Then getAngle = 180: Exit Function If yLength = 0 Then Exit Function End If If yLength = 0 Then If xLength < 0 Then getAngle = 90: Exit Function If xLength > 0 Then getAngle = 270: Exit Function End If getAngle = Atn(yLength / xLength) getAngle = getAngle * DEG If xLength < 0 Then getAngle = getAngle + 90 ElseIf xLength > 0 Then getAngle = getAngle + 270 End If End Function Then he says: x = x + cos(theta) * x - sin(theta) * distance y = y + cos(theta) * y + sin(theta) * distance could anyone please explain this to me? First, how can I find the angle in blitzmax? How can I make the alien find the angle that directs it towards the next point? Then how do I get it to follow that point and make sure it goes the same speed? |
| ||
Small codesnippet I found in these forums earlier todayFunction calcAngle2D:Float( x1:float, y1:float, x2:float, y2:float ) Local angle:Float=ATan2( ( y2 - y1 ), ( x2 - x1 ) ) '+ 180 Return angle End Function |
| ||
whats the x2 and the y2? the next spot.. hmm I wonder how I would determine what that is exactly. |
| ||
x1,y1 could be the last point or the alien position. x2,y2 is the next point. Just to give a quick example: angle = calcAngle2D( enemy.x,enemy.y, nextpoint.x,nextpoint.y) enemy.x :+ cos( angle ) enemy.y :+ sin( angle ) |
| ||
hmm they're starting out in the middle of the screen now instead of the corner? |
| ||
Ok I fixed the problem.. but to make them all go the same speed enemy.x:+ Cos( enemy.Angle ) * enemy.x- Sin (enemy.Angle) * (1 - enemy.t) enemy.Y:+ Cos( enemy.Angle ) * enemy.Y+ Sin (enemy.Angle) * (1 - enemy.t) Right? |
| ||
no that didnt work... How can I make the speed right? |
| ||
this may help.. sorry if its a little complex. This is the test app i used to write my animation controller. If you read through and strip it right back it should be clear what is going on. |
| ||
Here's something that (more or less) travels at a constant rate along a bezier: |
| ||
Warpy strikes again. This would be the third time you've helped me considerable ;) |