mouse movement

  • (6 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »

76 Replies - 4877 Views - Last Post: 05 June 2018 - 08:07 PM Rate Topic: -----

#1 pbivens85   User is offline

  • D.I.C Addict

Reputation: -13
  • View blog
  • Posts: 507
  • Joined: 22-October 13

mouse movement

Posted 15 May 2018 - 07:03 PM

I am trying to move a checker piece from one position to another. I am drag and dropping a piece using drawScene_move() to drawScene_move_one(). I also have the drawScene_two() to drop a piece in a another position. I want to select board[5][1] and then drag to board[4][0] but I don't want to drag from board[5][1] to board[4][2] which it does also. here is the code I have been working on.
void black_piece()
{
	if (board[5][1] == 1)
	{
		drawScene_move();
	}
	if (board[4][0] == 1)
	{
		drawScene_move_one();
	}
	if (board[4][2] == 1)
	{
		drawScene_move_two();
	}
}

void mouseMotion(int x, int y)
{
	cout << x << " " << y << endl;

	if (x >= 280 && x <= 320 && y <= 360 && y >= 330)
	{
		board[5][1] = 1;
	}
	if (x >= 240 && x <= 280 && y <= 330 && y >= 300)
	{
		board[4][0] = 1;
	}
	if (x >= 320 && x <= 360 && y <= 330 && y >= 300)
	{
		board[4][2] = 1;
	}
	black_piece();
	glFlush();
}


let me know if you need further explanation.

Is This A Good Question/Topic? 0
  • +

Replies To: mouse movement

#2 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6565
  • View blog
  • Posts: 22,532
  • Joined: 05-May 12

Re: mouse movement

Posted 15 May 2018 - 08:23 PM

What is the significance of the coordinates (5,1), (4,0) and (4,2) ? Why are you allowed to move from (5,1) to (4,0), but not allowed to move from (5,1) to (4,2) ? From what I can see, both are movements along a a diagonal, and both are a movement of one square so therefore both should be legal moves.

Anyway, to prevent the movement, simply put additional constraints to your movement code to prevent the drag/drop from happening.

As a quick aside, why all the magic numbers in your code? I'm quite sure that we've already told you in the past that using magic numbers is really a poor programming practive. Imagine what happens if your code suddenly runs on a high-DPI screen where the pixels are smaller. You'll have to go through all your code and change your current magic numbers to other magic numbers.

As a design note: you could relatively easily compute the row and column positions of by simple arithmetic. You don't need all those if statements.

As another design note: with modern games, particularly event driven games, the usual approach is to get an input event (like your mouse movement) and then to update your program state. Eventually the game loop will come around to needing to draw the scene, and the code to draw the scene will simply read the current program state and show that on screen. Your current approach of forcing a call to draw a scene is the old school pre-event driven programming way of doing things.
Was This Post Helpful? 2
  • +
  • -

#3 pbivens85   User is offline

  • D.I.C Addict

Reputation: -13
  • View blog
  • Posts: 507
  • Joined: 22-October 13

Re: mouse movement

Posted 16 May 2018 - 01:51 PM

actually I can move from [5][1] to [4][0] but you can also move from [5][1] to [4][2] which I don't want to happen if I have already moved a piece. also what do you mean by additional constraints.
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101   User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12410
  • View blog
  • Posts: 45,548
  • Joined: 27-December 08

Re: mouse movement

Posted 16 May 2018 - 02:10 PM

Quote

also what do you mean by additional constraints.


Where can one move a given piece? Where can one not move a given piece? What rules govern moving a piece? This is what Skydiver means by constraints.
Was This Post Helpful? 1
  • +
  • -

#5 pbivens85   User is offline

  • D.I.C Addict

Reputation: -13
  • View blog
  • Posts: 507
  • Joined: 22-October 13

Re: mouse movement

Posted 16 May 2018 - 02:18 PM

well I can move from the lower right to the upper left and from the lower right to upper right, however both moves are allowed and I only want the piece to move once not twice.

I hope I explained this problem well.
Was This Post Helpful? 0
  • +
  • -

#6 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6565
  • View blog
  • Posts: 22,532
  • Joined: 05-May 12

Re: mouse movement

Posted 16 May 2018 - 06:56 PM

So mark your game state appropriately. When the piece moves to another square, mark the origin as empty, and the destination as occupied. Furthermore, as part of the drag-drop operation, remember the coordinates of the source and destination. Right now, it is not clear how you are figuring out which is there source and which is the destination when you are using the board itself, and just using the value of 1 everywhere. What will happen latter when you implement multiple jumps for a king? How will you know which sequence of squares the player used for the jumps?
Was This Post Helpful? 0
  • +
  • -

#7 pbivens85   User is offline

  • D.I.C Addict

Reputation: -13
  • View blog
  • Posts: 507
  • Joined: 22-October 13

Re: mouse movement

Posted 16 May 2018 - 07:10 PM

should I use an array to store the origin and destination sites?
Was This Post Helpful? 0
  • +
  • -

#8 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6565
  • View blog
  • Posts: 22,532
  • Joined: 05-May 12

Re: mouse movement

Posted 16 May 2018 - 07:26 PM

What was your plan to deal with jumps when you were designing your implementation of the the game? Or did you not have a plan? Instead you decided to do the UI first, and then figure out how to do the game logic as you went along?
Was This Post Helpful? 0
  • +
  • -

#9 pbivens85   User is offline

  • D.I.C Addict

Reputation: -13
  • View blog
  • Posts: 507
  • Joined: 22-October 13

Re: mouse movement

Posted 16 May 2018 - 07:31 PM

well I have to work on the game logic by stubbing out some code. this seems to help me understand game design. also I might use vectors.
Was This Post Helpful? 0
  • +
  • -

#10 macosxnerd101   User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12410
  • View blog
  • Posts: 45,548
  • Joined: 27-December 08

Re: mouse movement

Posted 16 May 2018 - 07:47 PM

What point do vectors serve?

Be thoughtful about the logic before throwing code at it. How do you handle legal moves without jumps? Think this through and implement it.

Then think about how to handle moves with jumps. After thinking through the logic, implement it!

When we ask you pointed questions, we aren’t looking for you to simply “report back,” as you did in Post #5. We are trying to prod you in the right direction, and you should take the initiative rather than waiting for folks to hold your hand.
Was This Post Helpful? 2
  • +
  • -

#11 pbivens85   User is offline

  • D.I.C Addict

Reputation: -13
  • View blog
  • Posts: 507
  • Joined: 22-October 13

Re: mouse movement

Posted 17 May 2018 - 04:11 PM

skydiver what do you mean by simple arithmetic?
Was This Post Helpful? 0
  • +
  • -

#12 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6565
  • View blog
  • Posts: 22,532
  • Joined: 05-May 12

Re: mouse movement

Posted 17 May 2018 - 04:26 PM

Here's an example. Let's assume that each square is 100 pixels wide and that the board is flush against the top left corner. So if the mouse coordinates are (450,740), then the board coordinates would be (4,7). 450/100 == 4 and 740/100 == 7.

Now let's say the board is offset by (60,30). Then subtract the offset from the mouse coordinates, and then do the division to get (450-60)/100 == 3; (740-30)/100 == 7 ==> (3,7).
Was This Post Helpful? 1
  • +
  • -

#13 pbivens85   User is offline

  • D.I.C Addict

Reputation: -13
  • View blog
  • Posts: 507
  • Joined: 22-October 13

Re: mouse movement

Posted 17 May 2018 - 04:57 PM

well my squares are 40 width and 30 height, and my board is offset 240 width and 180 height in mouse coordinates. my board coordinates are -4.0f to 4.0f
Was This Post Helpful? 0
  • +
  • -

#14 pbivens85   User is offline

  • D.I.C Addict

Reputation: -13
  • View blog
  • Posts: 507
  • Joined: 22-October 13

Re: mouse movement

Posted 17 May 2018 - 05:40 PM

well I have studied the math and I am able to convert mouse coordinates to board array coordinates.
Was This Post Helpful? 0
  • +
  • -

#15 pbivens85   User is offline

  • D.I.C Addict

Reputation: -13
  • View blog
  • Posts: 507
  • Joined: 22-October 13

Re: mouse movement

Posted 17 May 2018 - 06:26 PM

I have replaced the if statements with array math, here is the code.
void mouseMotion(int x, int y)
{
	cout << x << " " << y << endl;
	board[(y - 180) / 30][(x - 240) / 40] = 1;

	black_piece();
	glFlush();
}


thanks skydiver
Was This Post Helpful? 0
  • +
  • -

  • (6 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »