🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Mouse Input Raylib

Started by
3 comments, last by LeftyGuitar 4 years, 10 months ago

Hello,

I am trying to get the point counter to increment when the user clicks on a ball using the mouse. I'm using the Raylib library.  I have posted a snippet of my code. I can post if more is needed.

 


Vector2 mp = GetMousePosition();

	RedBall.X_Pos = static_cast <float> (rand() % Width / 2);
	RedBall.Y_Pos = static_cast <float> (rand() % Height / 2);
      
      RedBall.X_Vel = 8.0f;
	RedBall.Y_Vel = 8.0f;
      
      RedBall.Pos = { RedBall.X_Pos,RedBall.Y_Pos };
	RedBall.Vel = { RedBall.X_Vel,RedBall.Y_Vel };
      
      RedBall.Pos.x += RedBall.Vel.x;
		RedBall.Pos.y += RedBall.Vel.y;
      
      if ((RedBall.Pos.x >= (GetScreenWidth() - RedBall.Radius)) || (RedBall.Pos.x <= RedBall.Radius)) RedBall.Vel.x *= -1.0f;
		if ((RedBall.Pos.y >= (GetScreenHeight() - RedBall.Radius)) || (RedBall.Pos.y <= RedBall.Radius)) RedBall.Vel.y *= -1.0f;


    //problem with collision detecting?
	if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON) && CheckCollisionPointCircle(RedBall.Pos,mp,RedBall.Radius))
	{
		Player.Current_Game_Score++; //does not increment
	}
      
      DrawCircleV(RedBall.Pos, RedBall.Radius, RED);

 

Advertisement

So for a 2d game, you can subract the mouse position and the circles position to get a Delta vector. Then check the delta vectors maginude to see if it is less than the circles' radius. If it is, you got collision!

-Jawshttp://uploading.com/files/eff2c24d/TGEpre.zip/

Why do you post ball movement code if you have a question about click positions?

When the "if" doesn't trigger, then either that code is never executed (but then your ball wouldn't move), or the


IsMouseButtonPressed(MOUSE_LEFT_BUTTON) && CheckCollisionPointCircle(RedBall.Pos,mp,RedBall.Radius)

condition fails.

Did you check which yields false at the wrong moment?

My prime suspect would be the CheckCollisionPointCircle, did you try to debug that function? (Step through it while you know what the answer should be, then find the line where it makes the wrong decision.)

 

Ok I seem to make a little bit more progress. Now it sometimes increases the value and other times it doesen't.

 

EDIT: Ok I figured it out! I needed to put the GetMousePosition inside of the update loop to get the mouse's position in real-time.

 


Vector2 mp;
Vector2 pos;


	RedBall.X_Pos = static_cast <float> (rand() % Width / 2);
	RedBall.Y_Pos = static_cast <float> (rand() % Height / 2);
      
      RedBall.Radius = 20;

	mp = GetMousePosition();
	pos = { GetMousePosition().x,GetMousePosition().y };


		if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON) && CheckCollisionPointCircle(pos,RedBall.Pos,RedBall.Radius))
		{
			Player.Current_Game_Score++;
		}

 

This topic is closed to new replies.

Advertisement