Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


[VB] Functions

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
sven3107
Expert Cheater
Reputation: 0

Joined: 04 Feb 2009
Posts: 118
Location: Belgium

PostPosted: Tue May 05, 2009 1:09 pm    Post subject: [VB] Functions Reply with quote

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
View user's profile Send private message Send e-mail MSN Messenger
92Garfield
I'm a spammer
Reputation: 57

Joined: 20 Dec 2007
Posts: 5871
Location: Banana Republic Germany

PostPosted: Tue May 05, 2009 2:47 pm    Post subject: Reply with quote

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
View user's profile Send private message
pkedpker
Master Cheater
Reputation: 1

Joined: 11 Oct 2006
Posts: 412

PostPosted: Tue May 05, 2009 11:12 pm    Post subject: Reply with quote

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

_________________
Hacks I made for kongregate.
Kongregate Universal Badge Hack: http://forum.cheatengine.org/viewtopic.php?p=4129411
Kongreate Auto Rating/Voter hack: http://forum.cheatengine.org/viewtopic.php?t=263576
Took a test lol
Back to top
View user's profile Send private message
sven3107
Expert Cheater
Reputation: 0

Joined: 04 Feb 2009
Posts: 118
Location: Belgium

PostPosted: Wed May 06, 2009 9:33 am    Post subject: Reply with quote

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
View user's profile Send private message Send e-mail MSN Messenger
tombana
Master Cheater
Reputation: 2

Joined: 14 Jun 2007
Posts: 456
Location: The Netherlands

PostPosted: Wed May 06, 2009 10:04 am    Post subject: Reply with quote

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
View user's profile Send private message
shhac
Expert Cheater
Reputation: 0

Joined: 30 Oct 2007
Posts: 108

PostPosted: Thu May 07, 2009 6:32 am    Post subject: Reply with quote

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
View user's profile Send private message
sven3107
Expert Cheater
Reputation: 0

Joined: 04 Feb 2009
Posts: 118
Location: Belgium

PostPosted: Sun May 10, 2009 5:53 am    Post subject: Reply with quote

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. Laughing
Back to top
View user's profile Send private message Send e-mail MSN Messenger
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites