Touch Controls like a Pool Game

Previous topic - Next topic

XanthorXIII

Does anyone know of Touch Controls that are like a pool game where you Angle the pool stick around a ball, draw back the pool stick and then when you let go it fires?
I'm trying to work this out and having a little bit of a challenge.
Thanks.
Owlcat has wise

ampos

Code (glbasic) Select
Reoeat
Checkmouse()
If mouse.b1=1 and press=0
  X1=mouse.x;u2=mouse.y;press=1
endif

If mouse.b1=1 and press=1
  X2=mouse.x;y2=mouse.y;press=2
endif

If mouse.b1=0 and press=2
  Press=3
endif

Until press=3

Slydog

2D or 3D?

Are you asking how to handling the input states like Ampos suggested, or graphically how to move / angle the cue stick with the mouse?
My current project (WIP) :: TwistedMaze <<  [Updated: 2015-11-25]

ampos

Quote from: ampos on 2011-Apr-03
Code (glbasic) Select
Reoeat
Checkmouse()
If mouse.b1=1 and press=0
  X1=mouse.x;u2=mouse.y;press=1
endif

If mouse.b1=1 and press=1
  X2=mouse.x;y2=mouse.y;press=2
endif

If mouse.b1=0 and press=2
  Press=3
endif

Until press=3


This didnt work, It should be

Code (glbasic) Select
Reoeat
Checkmouse()
If mouse.b1=1 and press=0
  X1=mouse.x;u2=mouse.y;press=1
endif

If mouse.b1=1 and press=1
  X2=mouse.x;y2=mouse.y
endif

If mouse.b1=0 and press=1
  Press=2
endif

Until press=2

[/quote]

XanthorXIII

It was really more about angling the cue stick around the ball, pulling back and then releasing. The game is not exactly pool but the mechanics I described are similar.
Here is the code I am using.

Code (glbasic) Select
minAngle = 155
maxAngle = 25
angle = ATAN(my-ballCenterY,mx-ballCenterX) // I only care about between the degrees 25 and 155 which is a nice 130 Degree arc
length = SQR(POW(mx-ballCenterX,2)+(my-ballCenterY,2)) // Not doing anything just yet with this but I have it here in case I need it.

IF ANGLE < minAngle and > maxAngle
    self.fireObjectX = mx-22
    self.fireObjectY = my-22
ENDIF


So what I have found is that this works, the object can move between 130 Degrees from the center of the ball however what I am finding that it is sticking on the 155 and 25 degree angle. I just need to work some more on my logic and calculations. I'm open to suggestions as always.
Thanks Ampos for the code.



Owlcat has wise

XanthorXIII

How do you handle ensuring that the Touch is only applying to the object or within the bounds of the object? Do you check the location of the touch to see if it is within the bounds of the object.
For example

Code (glbasic) Select
IF mouse[mouseCount].x >= self.imagePosX and mouse[mouseCount].x <= self.imagePosX+imageSizeX and mouse[mouseCount].y >= self.imagePosY and mouse[mouseCount].y <= self.imagePosY+imageSizeY
// Code Here
ENDIF

Or is there a better way to do this?
Owlcat has wise

ampos

Check my "zone" code, and define a zone around the ball.

http://www.glbasic.com/forum/index.php?topic=5186.0

Code (glbasic) Select
z=zone(mouse.x,mouse.y)
if z=1 and mouse.b1
   //pressed inside zone 1
endif

Slydog

#7
You could do as you said, check to see if the mouse x,y are in the rectangular area of the object.
Your statement could be cleaned up some of course by making a function that returns true if the mouse is inside of the passed location / size.  (FUNCTION IsClick%: x%, y%, w%, h%   <- or whatever)

Glancing at ampos's zone thread, it seems that's what his does too, but lets you define zones ahead of time.
For your example, the cue would be moving, and any predefined 'cue' zones wouldn't work as the 'cue' zone would be changing each frame, unless your shot zone is locked to a certain screen location, or update the 'cue' zone each time it changes.

You have two more options:

If a circular touch area is desired, you could calculate the distance from the center of the object to the mouse x,y.  If the distance is too much, don't count the touch, or else it's in range so allow it.
Using the Pythagorean Theorem, this would be:
Code (glbasic) Select
FUNCTION Distance#: x1%, y1%, x2%, y2%
  LOCAL diff_x%, diff_y%
  LOCAL distance#
  diff_x = x2 - x1
  diff_y = y2 - y1
  distance = SQR((diff_x * diff_x) + (diff_y * diff_y))
  RETURN distance
ENDFUNCTION
// Usage . . .
IF Distance(object_center_x, object_center_y, mouse.x, mouse.y) > 20.0 . . . // Too far


Or if you want a pixel perfect touch, not rectangular, use sprite collisions. 
Create a one pixel sprite (eg. sp_mouse) to represent the mouse position, then use something like:
Code (glbasic) Select
touched = SPRCOLL(sp_object, object_x, object_y, sp_mouse, mouse.x, mouse.y)

Just some ideas.  (and none of this is tested, just made it up now)

[Edit] To clarify, mouse.x,mouse.y (of a custom TYPE) are populated via a command like: "MOUSESTATE mouse.x, mouse.y, mouse.b1, mouse.b2"
My current project (WIP) :: TwistedMaze <<  [Updated: 2015-11-25]