| View previous topic :: View next topic |
| Author |
Message |
sven3107 Expert Cheater
Reputation: 0
Joined: 04 Feb 2009 Posts: 118 Location: Belgium
|
Posted: Tue May 05, 2009 1:09 pm Post subject: [VB] Functions |
|
|
Warning: Bad coding ahead!
I just felt like putting the warning there because I'm having trouble on some functions that allow simple motion. (inspired on GML)
This one works fine, it makes a specified object move in a direction at a certain speed.
| Code: | Sub Step_Direction(ByVal ObjectName, ByVal StepSize, ByVal StepDirection)
With ObjectName
ObjectName.Left = (ObjectName.Left + (Math.Cos(StepDirection * Math.PI / 180) * StepSize))
ObjectName.Top = (ObjectName.Top + (Math.Sin(StepDirection * Math.PI / 180) * StepSize))
End With
End Sub |
But the code below is meant to specify the angle formed by two points and the standard direction system.
Counter-clockwise, 0/360 points to the right, 270 points down.
| Code: | Function Point_Direction(ByVal XStart, ByVal YStart, ByVal XEnd, ByVal YEnd)
Return Math.Asin((YEnd - YStart) / (XEnd - XStart))
End Function |
I'm pretty sure it's in the math function that causes the error, mostly because the code tried to divide by zero.
If someone can help me out with this function I'd be very grateful.
|
|
| Back to top |
|
 |
92Garfield I'm a spammer
Reputation: 57
Joined: 20 Dec 2007 Posts: 5871 Location: Banana Republic Germany
|
Posted: Tue May 05, 2009 2:47 pm Post subject: |
|
|
it divides by zero when
XEnd = XStart
I think the mistake isnt in that function
maybe its where it receives XEnd and Xstart
Also this isnt VB maybe it's VB.Net
_________________
|
|
| Back to top |
|
 |
pkedpker Master Cheater
Reputation: 1
Joined: 11 Oct 2006 Posts: 412
|
Posted: Tue May 05, 2009 11:12 pm Post subject: |
|
|
| Code: |
Function Point_Direction(ByVal XStart, ByVal YStart, ByVal XEnd, ByVal YEnd)
If(XStart <> 0 Or Ystart <> 0 Or XEnd <> 0 or YEnd <> 0) Then
Return Math.Asin((YEnd - YStart) / (XEnd - XStart))
Else
Return 0
End If
End Function
|
or so it would work when XEnd - XStart is 0.. maybe this should be alright not sure..
| Code: |
Function Point_Direction(ByVal XStart, ByVal YStart, ByVal XEnd, ByVal YEnd)
If(XEnd - YEnd) <> 0) Then
Return Math.Asin((YEnd - YStart) / (XEnd - XStart))
Else
Return Math.Asin((YEnd - YStart) / 1)
End If
End Function
|
P.S.> no idea if you want to get distance between 2 points (x,y) but the math formula is
| Code: |
distance = Sqrt( ((x2 - x1) ^2) + ((y2 - y1) ^ 2))
|
| Code: |
Function getDistance(x1 as integer, y1 as integer, x2 as integer, y2 as integer) as long
return Convert.ToInt32(Math.Sqrt(Math.Pow((x1 - x2), 2)+ Math.Pow((y1 - y2), 2)))
end function
|
_________________
|
|
| Back to top |
|
 |
sven3107 Expert Cheater
Reputation: 0
Joined: 04 Feb 2009 Posts: 118 Location: Belgium
|
Posted: Wed May 06, 2009 9:33 am Post subject: |
|
|
Wow, direction code is perfect!
Distance code was already there, though.
| Code: | Public Function Point_Distance(ByVal XS, ByVal YS, ByVal XE, ByVal YE)
Return Abs(XS - XE) + Abs(YS - YE)
End Function |
Much simpler (made shorter Abs function)
But thanks a lot for that direction code!
|
|
| Back to top |
|
 |
tombana Master Cheater
Reputation: 2
Joined: 14 Jun 2007 Posts: 456 Location: The Netherlands
|
Posted: Wed May 06, 2009 10:04 am Post subject: |
|
|
| sven3107 wrote: | Wow, direction code is perfect!
Distance code was already there, though.
| Code: | Public Function Point_Distance(ByVal XS, ByVal YS, ByVal XE, ByVal YE)
Return Abs(XS - XE) + Abs(YS - YE)
End Function |
Much simpler (made shorter Abs function)
But thanks a lot for that direction code! |
Where did you study math?
Distance^2 = (xdistance)^2 + (ydistance)^2
|
|
| Back to top |
|
 |
shhac Expert Cheater
Reputation: 0
Joined: 30 Oct 2007 Posts: 108
|
Posted: Thu May 07, 2009 6:32 am Post subject: |
|
|
| Code: | Public Function Point_Distance(ByVal XS, ByVal YS, ByVal XE, ByVal YE)
Return Math.Sqrt((XS - XE) ^ 2 + (YS - YE) ^ 2)
End Function |
Also, Personally I don't like <> notation, so I'd do | Code: | Function Point_Direction(ByVal XStart, ByVal YStart, ByVal XEnd, ByVal YEnd)
If (XEnd - YEnd) = 0 Then
Return Math.Asin(YEnd - YStart)
Else
Return Math.Asin((YEnd - YStart) / (XEnd - XStart))
End If
End Function |
And you haven't specified types anywhere, which makes it harder to find where errors are coming from.
|
|
| Back to top |
|
 |
sven3107 Expert Cheater
Reputation: 0
Joined: 04 Feb 2009 Posts: 118 Location: Belgium
|
Posted: Sun May 10, 2009 5:53 am Post subject: |
|
|
| tombana wrote: |
Where did you study math?
Distance^2 = (xdistance)^2 + (ydistance)^2 |
I never learnt that stuff, i just fiddle around with math functions.
|
|
| Back to top |
|
 |
|