iNET Interactive - Online Advertising Agency
          
Go Back  Xtreme Visual Basic Talk > Legacy Visual Basic (VB 4/5/6) > Knowledge Base > Code Library > Module to move object from A to B


Reply
 
Thread Tools Display Modes
  #1  
Old 07-02-2003, 10:36 PM
blindwig's Avatar
blindwig blindwig is offline
Ultimate Contributor
* Expert *
 
Join Date: Jun 2003
Location: California, USA
Posts: 1,927
Default Module to move object from A to B

OK, I keep seeing posts here from people asking how to move an object (like a picture or image) around on a form. I've made a little module to use for moving an object toward a destination, not directly to that destination, so that you can loop and move the object as fast or slow as you want.

Here's the module source:
Code:
Option Explicit 'gotta have some pi! Global Const pi = 3.14159265 'Picks a spot that allows object to be fully visable in it's parent, 'assuming that the object is smaller than it's parent Public Sub PickASpot(ByRef objTarget As Object, _ ByRef TargetX, ByRef TargetY) objTarget.Parent.ScaleMode = 1 TargetX = Int(Rnd * (objTarget.Parent.ScaleWidth - objTarget.Width)) TargetY = Int(Rnd * (objTarget.Parent.ScaleHeight - objTarget.Height)) End Sub 'Returns the distance between 2 points Private Function Distance(ByVal x1 As Long, ByVal y1 As Long, _ ByVal x2 As Long, ByVal y2 As Long) As Double Distance = Sqr((x2 - x1) ^ 2 + (y2 - y1) ^ 2) End Function 'Will move an object toward a target location, only as far as MaxMovement Public Function SlideObjTo(ByRef objTarget As Object, _ ByVal TargetX As Long, ByVal TargetY As Long, _ Optional ByVal MaxMovement As Long = -1) As Boolean Dim CurX, CurY As Long CurX = objTarget.Left CurY = objTarget.Top 'Default MaxMovement is half the diagonal size of the object If MaxMovement <= 0 Then MaxMovement = Sqr((objTarget.Height) _ ^ 2 + (objTarget.Width) ^ 2) / 2 If Distance(CurX, CurY, TargetX, TargetY) > MaxMovement Then MoveObjTowards objTarget, TargetX, TargetY, MaxMovement Else MoveObjTo objTarget, TargetX, TargetY End If 'Return tells you if the object has reached it's destination SlideObjTo = (Abs(objTarget.Left - TargetX) <= Screen.TwipsPerPixelX) _ And (Abs(objTarget.Top - TargetY) <= Screen.TwipsPerPixelY) End Function 'Will move an object directly to the target location Private Sub MoveObjTo(ByRef objTarget As Object, _ ByVal TargetX As Long, ByVal TargetY As Long) objTarget.Left = TargetX objTarget.Top = TargetY End Sub 'Will move an object toward a target location as far as DISTANCE Private Sub MoveObjTowards(ByRef objTarget As Object, _ ByVal TargetX As Long, ByVal TargetY As Long, ByVal Distance As Long) Dim Angle As Double Dim X, Y As Long X = objTarget.Left - TargetX Y = objTarget.Top - TargetY If X > 0 Then Angle = Atn(Y / X) + pi ElseIf X = 0 Then If Y < 0 Then Angle = pi / 2 Else Angle = -pi / 2 ElseIf X < 0 Then Angle = Atn(Y / X) End If X = Int(Cos(Angle) * Distance) Y = Int(Sin(Angle) * Distance) objTarget.Left = objTarget.Left + X objTarget.Top = objTarget.Top + Y End Sub
and here is a form you can use to demo the module. It has an image and a timer:
Code:
Dim DestX As Long, DestY As Long Private Sub Form_Load() PickASpot Image1, DestX, DestY End Sub Private Sub Timer1_Timer() If SlideObjTo(Image1, DestX, DestY) Then PickASpot Image1, DestX, DestY Me.Caption = "Moving from (" & Image1.Left & "," & Image1.Top & _ ") to (" & DestX & "," & DestY & ")" End Sub
As always, comments are welcome!
__________________
"Fortunately, I live in the United States of America, where we are gradually coming to understand that nothing we do is ever our fault, especially if it is really stupid." -Dave Barry
Reply With Quote
  #2  
Old 07-06-2003, 12:17 PM
blindwig's Avatar
blindwig blindwig is offline
Ultimate Contributor
* Expert *
 
Join Date: Jun 2003
Location: California, USA
Posts: 1,927
Default

OK, I just added another function to my module, to be used when you don't know the object's exact destination, and would rather move it by trajectory and velocity. Here's the code (goes in the module):
Code:
'Moves an object in the direction DEGREE by DISTANCE units 'Note that DEGREE is actual degrees (0-360), not radians (0-2pi) 'Note that 0 DEGREE is not straight up, but to the right, and goes clockwise from there Public Sub MoveObjByTrajectory(ByRef objTarget As Object, ByVal Degree As Double, Optional ByVal Distance As Double = -1) Dim X, Y As Long 'Default Distance is half the diagonal size of the object If Distance <= 0 Then Distance = Sqr((objTarget.Height) ^ 2 + (objTarget.Width) ^ 2) / 2 X = Int(Cos(Degree * pi / 180) * Distance) Y = Int(Sin(Degree * pi / 180) * Distance) objTarget.Left = objTarget.Left + X objTarget.Top = objTarget.Top + Y End Sub
And to use it, for example:
Code:
MoveObjByTrajectory Image1, rnd*360
Enjoiy!
__________________
"Fortunately, I live in the United States of America, where we are gradually coming to understand that nothing we do is ever our fault, especially if it is really stupid." -Dave Barry
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Writing entire objects to a file? MoNoXBooGIE General 14 04-20-2004 07:21 PM
file move via code in MS Access module staciek Word, PowerPoint, Outlook, and Other Office Products 2 01-09-2003 08:27 AM
Move Object Within Form ???? HELP !!!! Tosh10 General 19 12-11-2002 05:34 PM
public object module tigz General 2 09-19-2002 04:32 AM
Dynamicly load object from module Ales Zigon General 10 12-16-2001 09:09 AM

Advertisement: