this code work some time some time no

some time the cursor lead the circle so the circle stop draging iam using glfw c++ no ortho setting
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
void Circle::update()
{
int state = glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_RIGHT);
if (state == GLFW_PRESS)
{
	dragAndDrop = true;
	if(dragAndDrop)
	   drag();
	}else
	   dragAndDrop = false;
}

void Circle::drag()
{
  glm::vec2 scr = {Screen::getWidth(),Screen::getHeight() };
  auto t = Mouse::worldPosition(scr.x,scr.y, 
  Mouse::getMouseX(),Mouse::getMouseY());

  if ((t.x> circlePos.x - radius * scale.x && t.x < circlePos.x + radius * 
   scale.x)&&(t.y > circlePos.y - radius * scale.y && t.x < circlePos.y + radius 
   * scale.y)){
	circlePos.x =t.x;
	circlePos.y = t.y;
  }
}
glm::vec2 Mouse::worldPosition(float sWidth, float sHeight , float x, float y)
{
     float cx = sWidth / 2.0f;
	float cy = sHeight / 2,of;
	float wx = 2.0f * (x - cx) / sWidth;
	float wy = -2.of * (y - cy) / sHeight;
	return glm::vec2(wx,wy);
}
Last edited on
1
2
3
4
5
dragAndDrop = true;
	if(dragAndDrop)
	   drag();
	}else
	   dragAndDrop = false;

How does dragAndDrop ever become false?

> if (state == GLFW_PRESS)
Maybe put some debug / debugger breakpoint on an else condition for this?

Also, neatness in code helps.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
void Circle::drag()
{
  glm::vec2 scr = {
  Screen::getWidth(), Screen::getHeight()};
  auto t = Mouse::worldPosition(
    scr.x, scr.y,
    Mouse::getMouseX(), Mouse::getMouseY());

  if ( (t.x > circlePos.x - radius * scale.x && 
        t.x < circlePos.x + radius * scale.x) && 
       (t.y > circlePos.y - radius * scale.y && 
        t.x < circlePos.y + radius * scale.y)) {
    circlePos.x = t.x;
    circlePos.y = t.y;
  }
}

glm::vec2 Mouse::worldPosition(float sWidth, float sHeight, float x, float y)
{
  float cx = sWidth / 2.0f;
  float cy = sHeight / 2, of;
  float wx = 2.0f * (x - cx) / sWidth;
  float wy = -2. of * (y - cy) / sHeight;
  return glm::vec2(wx, wy);
}

Notice anything odd about line 12 now?




thank you salem i had a small error coping and cleaning my code so the orignal was
1
2
3
4
5
6
7
8
9
10
11
12
13
	int state = glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_RIGHT);
	if (state == GLFW_PRESS)
	{
		dragAndDrop == true ? dragAndDrop = false: dragAndDrop = true;

		t = Mouse::worldPosition(scr.x, scr.y, Mouse::getMouseX(), Mouse::getMouseY());
	}
	else if (state == GLFW_RELEASE) {
		dragAndDrop = false;
	}
	if (dragAndDrop)
		drag();
}

and I added this code that fix this problem
this is the declaration of my circle cl
1
2
3
4
5
6
7
8
9
10
11
12
if ((t.x > circlePos.x - radius * scale.x && t.x < circlePos.x + radius * scale.x) &&
		(t.y > circlePos.y - radius * scale.y && t.x < circlePos.y + radius * scale.y)) {
		circlePos.x = t.x;
		circlePos.y = t.y;
		Wasdraging = true;
		}
	/* code add to solve the drag problem*/
	if (circlePos.x != t.x || circlePos.y != t.y) {
			circlePos.x = t.x;
			circlePos.y = t.y;
	}
}

this way the draging problem is fixed but anew one appears and this one is too strange
i declared 2 circles this way
1
2
3
4
5
6
Circle circle(window, shader4, {-0.91f,0.89f,0 }, { .35f * aspect,.35f,0.0f  },1.0f,0.0f,0.0f);
	glClearColor(0.2f, 0.3f, 0.3f,0.3f);
	circles.push_back(circle);
	Circle circle1(window, shader4, { 0.91f,0.89f,0 }, { .35f * aspect,.35f,0.0f }, 1.0f, 0.0f, 1.0f);
	circles.push_back(circle1);
	glClearColor(0.2f, 0.3f, 0.3f, 0.3f);


now when i run it the two circle are drawn properly in the screen by thiscode
1
2
3
4
for (Circle& c : circles) {
			c.update();
			c.render(shader4);
		}

that call the render function at the circle class
1
2
3
4
5
6
glm::mat4 trans = glm::mat4(1.0f);
	trans = glm::translate(trans, glm::vec3(circlePos.x, circlePos.y, 0));0.0f, 1.0f));
	trans = glm::scale(trans, glm::vec3(scale.x, scale.y, 0));

	shader.setMat4("transform", trans);
	glDrawArrays(GL_TRIANGLE_FAN, 0, numberOfVertices);


once i press the mouse Rbutton
circle run under circle1 and the two circle become 1 that i can drag prefectly
what i have did wrong???
this is the declaration of my circle class
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Circle
{
public:
	unsigned int VBO, VAO, EBO;
	unsigned int texture1;
	int roll = 1;
	Circle(GLFWwindow* window, Shader& shader, glm::vec3  circlePos, glm::vec3  circleScale, float r, float g, float b);
	void init(Shader& shader);
	void update();
	void drag();
	void render(Shader shader);
	void cleanup();
private:
	GLFWwindow* window;
	glm::vec3  scale;
	glm::vec3  circlePos;
	glm::vec2 t,scr;
	const static int numberOfVertices = 26;
	int numberOfSides = numberOfVertices -2;
	float radius = 0.20f;
	float r, g, b;
	bool dragAndDrop = false;
	bool Wasdraging = false ;
};

did this is a normal behaviour?
so i ll let the player select which circle to move and then pass it to the update method
Last edited on
It appears your latest code still has the issue salem c pointed out. Look at line 2 of your second code excerpt. (I don't know if this is the main source of your issue, but it still is something that could be fixed.)

When you click, I assume you iterate through each Circle object you have.
If this part of the code:
1
2
3
4
5
6
if ((t.x > circlePos.x - radius * scale.x && t.x < circlePos.x + radius * scale.x) &&
		(t.y > circlePos.y - radius * scale.y && t.x < circlePos.y + radius * scale.y)) {
		circlePos.x = t.x;
		circlePos.y = t.y;
		Wasdraging = true;
		}
is run for both circles, then both circles will be set to the same position.
> t.x < circlePos.y + radius * scale.y
Comparing an X with a Y
See it?
yes salem thanks alot it was the error now it work perfectly
Ganado thanks alot and yes this whats happen and i solved it by passing the circle id to to drag() function it work very good
again thank you guys for help
Last edited on
Topic archived. No new replies allowed.