How to get reference to clicked sf::CircleShape? - c++

In my SFML program I am storing drawn CircleShapes in a Vector. How to get reference to one on clicking mouse button?

There's no shape.makeClickable() function in SFML, all you have to do is:
sf::CircleShape* onClick(float mouseX, float mouseY) {//Called each time the players clicks
for (sf::CircleShape& circle : vec) {
float distance = hypot((mouseX - circle.getPosition().x), (mouseY - circle.getPosition().y)); //Checks the distance between the mouse and each circle's center
if (distance <= circle.getRadius())
return &circle;
}
return nullptr;
}
With this vector in your class:
std::vector<sf::CircleShape> vec;
EDIT
To get ALL circles that you have clicked-on, and not only the first one that it finds :
std::vector<sf::CircleShape*> onClick(float mouseX, float mouseY) {//Called each time the players clicks
std::vector<sf::CircleShape*> clicked;
for (sf::CircleShape& circle : vec) {
float distance = hypot((mouseX - circle.getPosition().x), (mouseY - circle.getPosition().y)); //Checks the distance between the mouse and each circle's center
if (distance <= circle.getRadius())
clicked.push_back(&circle);
}
return clicked;
}

Related

The collision in SFML is not that good, how to improve it?

I've been lately working on a simple game using C++ and SFML latest version, but I had a problem which is that the collision detection is not that good, for example the player dies even if the enemy didn't touch him yet, but just near him. Here is the code of the player class with the move function and collision detection code AND the moves of the enemy class:
`class PlayerA : public CircleShape
{
public:
//Constructor:
PlayerA(float xposition, float yposition, float radius, float s)
{
setRadius(radius);
setFillColor(Color::Yellow);
setOutlineColor(Color(00,80,00));
setOutlineThickness(-2);
setPointCount(3);
setSpeed(s);
setPosition(xposition,yposition);
}
//Movements of the player:
void up()
{
move(0,-10*speed);
}
void down()
{
move(0,10*speed);
}
void right()
{
move(10*speed,0);
}
void left()
{
move(-10*speed,0);
}
void checkA(ObsA *obs1=NULL,ObsA *obs2=NULL, ObsA *obs3=NULL, ObsA *obs4=NULL, ObsA *obs5=NULL)
{
if(obs2==NULL)
{
if(getGlobalBounds().intersects(obs1->getGlobalBounds()))
{
relevel();
}
}
private:
float speed=0.00;
void obs()
{
if(speed > 0)
{
rotate(0.5*speed);
}
else
{
rotate(0.5*speed);
}
}
private:
float speed = 0.00;
void obs()
{
if(speed > 0)
{
rotate(0.5*speed);
}
else
{
rotate(0.5*speed);
}
}
private:
float speed = 0.00;
Is there something wrong with the code, how to fix the problem, thank you!
The intersects function just check if two rectangles intersect. If you want pixel perfect collision detection in SFML you have to write that yourself.
Basically, start with intersects, if it is true, then get the intersecting rectangle and check if any pixels therein from both original rectangles contains overlaping relevant pixels.
You can use this function to perform better collision detection.Its a basic one but works well
bool circleTest(const sf::Sprite &first, const sf::Sprite &second)
{
sf::Vector2f firstRect(first.getTextureRect().width, first.getTextureRect().height);
firstRect.x *= first.getScale().x;
firstRect.y *= first.getScale().y;
sf::Vector2f secondRect(second.getTextureRect().width, second.getTextureRect().height);
secondRect.x *= second.getScale().x;
secondRect.y *= second.getScale().y;
float r1 = (firstRect.x + firstRect.y) / 4;
float r2 = (secondRect.x + secondRect.y) / 4;
float xd = first.getPosition().x - second.getPosition().x;
float yd = first.getPosition().y - second.getPosition().y;
return std::sqrt(xd * xd + yd * yd) <= r1 + r2;
}
Are you using a circle? If I remember correctly, the circle will have a rectangle hitbox. If that is the case, then you may have collision between the invisible rectangle corners.
If you're using a circle, Perhaps change class to a square rectangle and see if collision works correctly. Or try testing collision directly on an x or y axis with your circles; i.e. having them moving in a straight line towards each other only changing 1 axis. (the edge of the circle will be the same as the edge of the rectangle at the left, right, top, and bottom sections).
If you're needing a better collision for circles, there may be one already built in SFML. But I don't think it would be too much to write your own logic using the radius of your two circles, the center of your two objects, and the angle hypotenuse between the centers.
edit based on Merlyn Morgan-Graham's comment.

Top-down perspective aim tank turret with mouse

I want to make tank's turret be aimed with mouse in a top-down perspective. I have written some code to animate rotation to given angle:
void Tank::rotateTurret(float angle) {
turretRotation += angle;
}
sf::Sprite turret;
void Tank::update(unsigned int time) {
if (turretRotation != 0.0f) {
float rotate;
if (turretRotation > 0.0f) {
rotate = turretRotationSpeed * time;
if (rotate > turretRotation) {
rotate = turretRotation;
turretRotation = 0;
}
else
turretRotation -= rotate;
}
else {
rotate = -turretRotationSpeed * time;
if (rotate < turretRotation) {
rotate = turretRotation;
turretRotation = 0;
}
else
turretRotation -= rotate;
}
turret.rotate(rotate);
}
}
And I can calculate mouse pointer angle relative to top left corner:
void TankPlayerController::update() {
sf::Vector2i mousePosition = sf::Mouse::getPosition(*relativeWindow);
sf::Vector2i mouseMovement = mousePosition - lastMousePosition;
if (mouseMovement.x != 0 || mouseMovement.y != 0) {
float mouseAngle = VectorAngleDeg(mousePosition.x, mousePosition.y);
tank->rotateTurret(???);
lastMousePosition = mousePosition;
}
}
But I have no idea how to combine it together. How should it be done?
You need to calculate the angle to the center of the turret (CoT) from the upper left corner (ULHC) and the angle to the mouse location from the ULHC. Next consider the triangle formed from the lines connecting the ULHC to the CoT, the line connecting the ULHC to the mouse pointer location and the line connecting the CoT to the mouse pointer location. Since you know the distance from the ULHC to the CoT and the distance from the ULHC to the mouse pointer location all you need to do is determine the difference between the angle to the CoT and the mouse pointer position you can use the Law of Cosines to get the angel between the ULHC and the mouse position at the turret and from there the angle to any arbitrary axis you choose.
It would be easier with a picture :|

Calculating ball deflection angle when colliding with paddle in brick slayer game

Here is my code:
void Draw()
{
int x = 59;
int y = 500;
int temp = x;
int colour;
for (int i = 0; i < 9; ++i)
{
for (int j = 0; j < 10; ++j)
{
if (i % 2 == 0)
colour = 2;
else
colour = 3;
DrawRectangle(x, y, 65, 25, colors[colour]);
x += 67;
}
x = temp;
y -= 39;
}
DrawRectangle(tempx, 0, 85, 12, colors[5]);
DrawCircle(templx, temply, 10, colors[7]);
}
// This function will be called automatically by this frequency: 1000.0 / FPS
void Animate()
{
templx +=5;
temply +=5;
/*if(templx>350)
templx-=300;
if(temply>350)
temply-=300;*/
glutPostRedisplay(); // Once again call the Draw member function
}
// This function is called whenever the arrow keys on the keyboard are pressed...
//
I am using OpenGL for this project. The function Draw() is used to print the bricks, slider, and the ball. The Animate() function is called automatically by the frequency given in the code. As it can be seen, I have incremented the values of templx and temply, but the ball goes out of screen as it crosses its limit. I have to deflect the ball if it collides with the paddle or the wall. What can I do to achieve this? All the conditions that I have used by now do not work properly.
So basically you would like to have a ball that is bouncing from the edges of your window. (For this answer I will ignore the slider, finding collision with the slider is very similar to finding collision with the walls).
templx and temply pair is position of your ball. I don't know what is the 3rd argument of DrawCircle function so I will assume that it is the radius. Let wwidth and wheight be width and height of a game window. Note that this magic constant 5 is, in fact, a velocity of the ball. Now ball is moving from upper left corner to lower right corner of your window. If you change 5 to -5 it will move from lower right corner to upper left corner.
Let's introduce two more variables vx and vy - velocity on x axis and velocity on y axis. The initial values will be 5 and 5. Now notice that when ball hits the right edge of the window it doesn't change its vertical velocity, it is still moving up/down but it changes its horizontal velocity from left->right to right->left. So if the vx was 5, after hitting the right edge of the window we should change it to -5.
The next problem is how to find out if we hit the edge of the window or not.
Note that the right-most point on the ball has the position templx + radius and the left-most point on the ball has the position templx - radius etc. Now to find out if we hit the wall or not we should just compare this values with window dimensions.
// check if we hit right or left edge
if (templx + radius >= wwidth || templx - radius <= 0) {
vx = -vx;
}
// check if we hit top or bottom edge
if (temply + radius >= wheight || temply - radius <= 0) {
vy = -vy;
}
// update position according to velocity
templx += vx;
temply += vy;

Make a circle push others away?

My problem is probably quite simple for you guys, I'm just new with programming so yeah.
I want to have a circle on the center of the screen, then I want that when a player gets close to it, he'll be pushed away in relation to the direction he came from,
Here's a little drawing I made to be abit more clear
The red circles are the player coming from different directions,
the green circle is the obstacle.
The arrows show what direction the player should be pushed to
the player moves only in the x,y axis
Thank you very much for your time
Transform player coordinates (red circle centres) from Descartes-like screen system to polar with origo of centre obstacle (green circle).
Choosing complex numbers for that purpose could be a solution: complex(x,y) == polar(r,th). Radii of objects are given and a tolerancy radius ('close') also.
Iterate through player coordinates and compare their magnitude (distance of red circle centres from origo). When distance is larger than sum of their radius + tolerancy then they are not considered close. Otherwise magnitudes are adjusted. Back transforming output coordinates into screen gives the result.
void PushAway( std::vector< std::complex< double > > & player_coords )
{
const double r_o = 3.0; // obstacle radius
const double r_p = 1.0; // player radius
const double r_t = 0.1; // 'close'
const double push = 10.0; // pushing amount
for( uint idx = 0; idx < player_coords.size(); ++idx )
{
std::complex< double > & pc = player_coords[idx];
double magn = std::abs( pc );
// too close ?
if( magn < r_o + r_t + r_p )
{
// push away in appropriate direction
pc = std::polar( push + magn, std::arg( pc ) );
}
}
}

algorithm for drawing a sort of nozzle(line),whose is direction determined by the mouse?

i am using The old Turbo C++ and am a beginner.
This is the code of a ongoing project that i am planning.
the circle moves withe WSAD keys
But the problem is that i want the nozzle(a line from the center) of that circle to follow the movement of the mouse,but i cant figure out the mathematical part to restrict the length of that nozzle to, say 30 pixels. the line goes on touching the pointer's location.
I tried to use the Distance formula and the line equation to get with an expression which has both the slope and the length of the line. But the problem here is that there is an square root in the denominator, and i think that is causing the problem
Most of the code here is unnecessary for the following problem, so please ignore
here is the relevant code
originx=getmaxx()/2;
originy=getmaxy()/2;
while(doga==0) //main game loop
{ if(kbhit())
op=getch();
if(limiter>10) //limiter is used to restrict the motion of the circle for a limited // time
{ op=0;limiter=0;} // otherwise the cirlce moves in that direction unless another //key is pressed
//movement of the circle
if(op==72)
{ originy--; limiter++;}
if(op==80)
{originy++; limiter++;}
if(op==75)
{ originx--; limiter++ ;}
if(op==77)
{ originx++; limiter++; }
circle(originx,originy,5);
mouseposi(x,y,cl);
printf(" %d %d",x,y);
printf("\b\b\b\b\b\b\b\b");
m=sloper(originx,originy,x,y);
line(originx,originy,80/sqrt(1+m*m),m*80/sqrt(1+m*m)); //THIS LINE IS WHERE THE PROBLEM IS
delay(30);
cleardevice();
if(op==49) //for exiting
doga=2;
}
}
Let (x,y) be the point you're after, (ox, oy) be your origin, and (mx, my) be the mouse location.
The vector from the origin to the mouse is (dx, dy) = (mx - ox, my - oy).
The distance between the mouse and the origin is the same as the norm of that vector:
distance = sqrt(dx * dx + dy * dy);
Normalizing (scaling) the vector to get a new vector of length 1 ("unit length") we get
nx = dx / distance;
ny = dy / distance;
And finally we can scale those coordinates by the desired length (remembering to add back the origin)
x = ox + length * nx;
y = oy + length * ny;