Sprite dragging and point collision

Blitz3D Forums/Blitz3D Beginners Area/Sprite dragging and point collision

Drekinn(Posted 2004) [#1]
Greetings all. Just a few questions:

1) Does SeedRnd Millisecs() need only to be executed once in a program for complete randomness to take effect? If so I assume that every time a Rand/Rnd event occurs the seeded number would be set as the current Millisecs() value at the time of that particular event.

2) For a sprite moving from one point to another specific point, what is the best method for detecting when the sprite meets the specific point?

3) What is the best method for dragging a sprite around the screen with the mouse? (ie. click on the sprite, hold button down and drag).

Any assistance would be appreciated.


Genexi2(Posted 2004) [#2]
1) One call is enough.

2) I would use EntityDistance() for Blitz3d, and that distance formula for 2d, and have Blitz do something when the distance between both objects is small.

3) Not too sure.


WolRon(Posted 2004) [#3]
3) Do you mean 2D or 3D sprite?


Drekinn(Posted 2004) [#4]
Genexi2, yeah tried the distance formula method but it wasn't accurate enough for my needs. I've used a method that works without fail but is quite clumsy in its structure; that is to detect whether the sprite has passed the x and y values of the 2nd point. This method also requires the direction from which the sprite is approaching. Just wondering if there was a more efficient way.

WolRon, oops sorry, I'm talking 2D.


Ross C(Posted 2004) [#5]
I'd only use the word sprite if you are referring to 3d ;) Gets me confused anyway. Just say image :o)

For detecting when the sprite meets a specific point, you could use rectsoverlap command.

If RectsOverlap(x,y,width,height, pointX,pointY,1,1) then
   ;point collided
End If


x and y are your images co-ords and width and height are the width and height of your image :o)


Ross C(Posted 2004) [#6]
As for the dragging of a sprite, i'd do a rectsoverlap again. When you click, you should keep a diffence value, between the location of the mouse cursor, and the location of the image. Just means that when you move the mouse, you can move the image and position it from where the cursor is :o)

Graphics 800,600
SetBuffer BackBuffer()


Global x = 300
Global y = 300

Global image = CreateImage(64,64) ; create a test image to use

SetBuffer ImageBuffer(image) ; draw random stuff to the image to ensure we see it
For loop = 0 To 299
	Color Rand(40,255),Rand(40,255),Rand(40,255)
	Rect Rand(0,62),Rand(0,62),Rand(1,2),Rand(1,2)
Next

Global drag_mode = 0
Global dx = 0
Global dy = 0

SetBuffer BackBuffer()

While Not KeyHit(1)

	Cls
	
	If MouseDown(1) Then
		If drag_mode = 0 Then
			If RectsOverlap(MouseX(),MouseY(),1,1, x,y,ImageWidth(image),ImageHeight(image)) Then
				drag_mode = 1
				dx = x - MouseX()
				dy = y - MouseY()
			End If
		End If
	Else
		drag_mode = 0
	End If
	
	If drag_mode = 1 Then
		x = MouseX() + dx ; make the image drag along with the mouse
		y = MouseY() + dy ; make the image drag along with the mouse
	End If
	
	DrawImage image, x , y
	
	Rect MouseX(),MouseY(),2,2

	Flip
Wend
End



Drekinn(Posted 2004) [#7]
Thanks for the drag code. Thought that might be the case. :)

As for your point to point example, is there a way of just detecting when two specific x,y coords intersect as opposed to the whole sprite (oops, I mean.. image) intersecting? ;)


Ross C(Posted 2004) [#8]
>RossC calls on sswift or floyd!

You'd probably need an equation for intersecting lines. If you search the internet for "intersecting lines equation", you should find some info. :o)


Drekinn(Posted 2004) [#9]
Ok, thanks Ross C. I'll check it out.