# How to Find a Specific Coordinate for Small Basic? • ### Question

• Hi and thanks for reading my request.

Currently, I am running into problems trying to figure out what the X and Y coordinates are for moving object named Ball. I have published the small basic game so the code can be seen by importing it into your small basic program by importing FGZ797.

To shoot the bullet, you must press the key 'A.'

Is there any coordinate or command to find out the coordinates of the moving object and apply it? I am using Shape.Animation to make the Ball move, however, I would like the ball to stop when the bullet enters anywhere in the Ball's terrain. Is there any possible way to do this with the current code I have? I am very new to small basic and would greatly appreciate any intended help.

My main concern really is what should I put into the If command here highlighted in bold. I am unsure on what to do to get the desired effect of the ball stopping when the bullet "hits."

If (Bulletx = x) Then
Hit = "true"
If Hit = "true" Then
Shapes.Animate(Ball,x,y,0)
Sound.PlayClickAndWait()
Program.Delay(200)
GraphicsWindow.Clear()
Ship()
Endif
Endif

Once again, thanks for all the help. Any support would be appreciated.
Thursday, March 18, 2010 5:55 PM

• You need to know the position of the target and bullet, X and Y coordinates of both.

You can then check for a hit by checking if they overlap, or perhaps easier is to check the distance between the objects.

If the bullet is at (bX,bY) and the target is at (tX,tY), then the distance between them is

dist = Math.SquareRoot((bX-tX)*(bX-tX) + (bY-tY)*(bY-tY))

Then, in your if you need:

If (dist < distMin) Then
Hit = "True"
...

where distMin is some variable holding a minimum distance before they collide.

Also, note that shapes are drawn from the top left corner, so if you want to position an object at (X,Y) and its width is W and height is H, then you need:

Shapes.Move(shape,X-W/2,Y-H/2)

This is using the Shapes.Move , which will be much easier than Shapes.Animate, because you always know where the shape is since had to give it the coordinates - this is how all of the action type games are made in SmallBasic, the Animate isn't really very interactive as you discovered, since we don't know where it is while it is moving.

Also, looking at your code, you should consider having a main game loop, where all the movement update is done and just calls subroutines to do specific tasks and act on the variable changes triggered by the event calls.

You are well on the way though.

This is some of the ideas in your code - by no means finished in any way FGZ797-0.
Thursday, March 18, 2010 7:54 PM

### All replies

• You need to know the position of the target and bullet, X and Y coordinates of both.

You can then check for a hit by checking if they overlap, or perhaps easier is to check the distance between the objects.

If the bullet is at (bX,bY) and the target is at (tX,tY), then the distance between them is

dist = Math.SquareRoot((bX-tX)*(bX-tX) + (bY-tY)*(bY-tY))

Then, in your if you need:

If (dist < distMin) Then
Hit = "True"
...

where distMin is some variable holding a minimum distance before they collide.

Also, note that shapes are drawn from the top left corner, so if you want to position an object at (X,Y) and its width is W and height is H, then you need:

Shapes.Move(shape,X-W/2,Y-H/2)

This is using the Shapes.Move , which will be much easier than Shapes.Animate, because you always know where the shape is since had to give it the coordinates - this is how all of the action type games are made in SmallBasic, the Animate isn't really very interactive as you discovered, since we don't know where it is while it is moving.

Also, looking at your code, you should consider having a main game loop, where all the movement update is done and just calls subroutines to do specific tasks and act on the variable changes triggered by the event calls.

You are well on the way though.

This is some of the ideas in your code - by no means finished in any way FGZ797-0.
Thursday, March 18, 2010 7:54 PM
• I love litdev's solutions because so often they go beyond programing basic and include some branch of science.

I think another approuch or slight variation of his(?) solution is to split one move or animation into a series of moves.  If you where to split it into 2 moves/animations you could check locations (Moves -1) or 1 times.  If you were to split the move into 100 moves using a simple for loop, for example you would have 99 checks and a much better chance of trapping a collision.  At some point the number of splits would become unporductive and an interesting question would be to determine that point.

If you want to crank it up a level or two, what if you where to take the faster object, in this case the bullet, and plot it's location by a fraction of a second.  Again the smaller the time interval the more accurate the collision trap.  Applying some physics, you should also be able to plot the direction and speed of the ball after the collision.  How you would do this we would need Litdev's science brain!
Thursday, March 18, 2010 8:36 PM