Xbox Controller Thumb Input - c++

I am working on moving a small rectangle around a screen using the thumbstick on the xbox controller. I have it perfect for the mouse Input but I seem to have run into a problem with the controller. The square appears on the screen at (0,0) the deadzone for the controller. When I move the thumbstick the square move a specific amount and no further, when I lift my finger off the stick it goes back to (0,0). The code is below, its pretty simple code but cant get it to work. Thanks for any help.
Here is the first part.
void Graphics::rectangle(int x, int y)
{
{
r1.x1 = x;
r1.y1 = y;
r1.x2 = r1.x1 + 50;
r1.y2 = r1.y1 + 50;
}
}
Here is the second part.
LONG x = input->getGamepadThumbRX(0);
LONG y = input->getGamepadThumbRY(0);
float x1 = x/32767;
float y1 = y/32767;
if(x1 > 0 && x1 < 0.2f || x1 < 0 && x1 > -0.2f){
x1 = 0;
}
if(y1 > 0 && y1 < 0.2f || y1 < 0 && y1 > -0.2f){
y1 = 0;
}
float factor = 10;
int dx = int(factor * x1);
int dy = int(factor * y1);
graphics->rectangle(dx,dy);

I finally got a solution to this problem. I added the variables xNew and yNew that take the values of x and y and add them on to the existing values. This allowed me to move the square around the screen. xNew and yNew are initialize at the top of the class below is the code for the input from controller and the result in rectangle.
`
void Graphics::rectangle(int x, int y)
{
xNew += x;
yNew += -y;
{
r1.x1 = xNew;
r1.y1 = yNew;
r1.x2 = r1.x1 + 50;
r1.y2 = r1.y1 + 50;
}
}
void Game::update()
{
LONG x = input->getGamepadThumbRX(0);
LONG y = input->getGamepadThumbRY(0);
float x1 = x/32767;
float y1 = y/32767;
float factor = 10;
int dx = int(factor * x1);
int dy = int(factor * y1);
graphics->rectangle(dx,dy);
}`

The problem is the mouse is a "Relative Movement" Input and the thumb-stick is a "Absolute Movement" input. What you need to do is when you are working with the thumb-sticks your final line needs to be something like
graphics->rectangle(xOld + dx, yOld + dy);
where xOld and yOld is the location of the square before you applied the function.

Might be a little late, but I do have a very easy-to-use C++ XInput wrapper. Have a look here
It literally takes a few lines of code and you have full support for XInput controllers in your app :)

Related

How can I draw filled triangle line by line in C++ console?

I am creating an utility to display graphics in the console. I currently have a method that takes 2 coordinates and draws a line between those two points. Is it possible to create a method based on this that will take 3 coordinates and draw a filled triangle?
I was thinking of drawing 2 lines, and then drawing lines from each point of one line to each point of the other. However, I think that the time complexity of this method will be really bad.
Here is a method that I used to draw a line:
void drawLine(Line line)
{
const bool steep = (fabs(line.end.y - line.begin.y) > fabs(line.end.x - line.begin.x));
if (steep)
{
std::swap(line.begin.x, line.begin.y);
std::swap(line.end.x, line.end.y);
}
if (line.begin.x > line.end.x)
{
std::swap(line.begin, line.end);
}
const double dx = line.end.x - line.begin.x;
const double dy = fabs(line.end.y - line.begin.y);
const double zStepLength = fabs(fabs(line.end.z) - fabs(line.begin.z)) / dx;
double error = dx / 2.0;
const int ystep = (line.begin.y < line.end.y) ? 1 : -1;
const double zstep = (line.begin.z < line.end.z) ? zStepLength : -zStepLength;
for (double x = line.begin.x, z = line.begin.z; x <= line.end.x; x++, z += zstep)
{
if (steep)
{
setPixel({ line.begin.y, x, z }, line.color);
}
else
{
setPixel({ x, line.begin.y, z }, line.color);
}
error -= dy;
if (error < 0)
{
line.begin.y += ystep;
error += dx;
}
}
}

Antialiased line with C++ Builder VCL

Question: I need to upgrade an old Embarcadero VCL graphic math application by introducing antialiased lines. So, I wrote in C++ the algorithm indicated in the page: https://en.wikipedia.org/wiki/Xiaolin_Wu%27s_line_algorithm.
How to write correctly the function 'plot' to draw the pixel at (x,y) with a brightness 'c', especially on the Embarcadero VCL.
Solution:
This solution has been possible by the contribution of #Spektre (use of a union to mix colors according to some brightness). pC is a canvas pointer, funcColor is the line intended color, and are properties of the Observer class:
//Antialiased line:
void Observer::aaLine(int x0, int y0, int x1, int y1)
{
union {
uint32_t dd;//The color value
uint8_t db[4];//To work on channels: {00.RR.GG.BB}
} c, c0;//Line color, and background color
//Color mixer, with calculations on each channel, because there is no
//Alpha channel with VCL:
auto plot = [&](int X, int Y, float brightness){
c.dd = funcColor;//Line color
c0.dd = pC->Pixels[X][Y];//Background color
//Find coefficients to simulate transparency, where there is not:
//Front color is augmented when background is decreased:
for(int i = 0; i < 3; ++i)
c.db[i] = int(c.db[i] * brightness + c0.db[i] * (1 - brightness));
//Output obtained by conversion:
pC->Pixels[X][Y] = static_cast<TColor>(c.dd);
};
//Wu's algorithm:
//Fractional part of x:
auto fpart = [](double x) { return x - floor(x); };
auto rfpart = [&](double x) { return 1 - fpart(x); };
bool steep = abs(y1 - y0) > abs(x1 - x0);//Means slope > 45 deg.
if(steep) {
std::swap(x0, y0);
std::swap(x1, y1);
}
if( x0 > x1 ) {
std::swap(x0, x1);
std::swap(y0, y1);
}
double dx = x1 - x0, dy = y1 - y0, gradient = (dx == 0. ? 1. : dy/dx) ;
//Handle first endpoint
double xend = x0,
yend = y0 + gradient * (xend - x0),
xgap = rfpart(x0 + 0.5),
xpxl1 = xend, // this will be used in the main loop
ypxl1 = floor(yend);
if( steep ) {
plot(ypxl1, xpxl1, rfpart(yend) * xgap);
plot(ypxl1+1, xpxl1, fpart(yend) * xgap);
}
else {
plot(xpxl1, ypxl1 , rfpart(yend) * xgap);
plot(xpxl1, ypxl1+1, fpart(yend) * xgap);
}
auto intery = yend + gradient; // first y-intersection for the main loop
//Handle second endpoint
xend = round(x1);
yend = y1 + gradient * (xend - x1);
xgap = fpart(x1 + 0.5);
auto xpxl2 = xend, //this will be used in the main loop
ypxl2 = floor(yend);
if( steep ){
plot(ypxl2 , xpxl2, rfpart(yend) * xgap);
plot(ypxl2+1, xpxl2, fpart(yend) * xgap);
//Main loop:
for(double x = xpxl1 + 1 ; x <= xpxl2 - 1 ; x += 1) {
plot(int(intery) , x, rfpart(intery));
plot(int(intery+1), x, fpart(intery));
intery += gradient;
}
}
else {
plot(xpxl2, ypxl2, rfpart(yend) * xgap);
plot(xpxl2, ypxl2+1, fpart(yend) * xgap);
//Main loop:
for(double x = xpxl1 + 1 ; x <= xpxl2 - 1 ; x += 1) {
plot(x, int(intery), rfpart(intery));
plot(x, int(intery+1), fpart(intery));
intery += gradient;
}
}
}//Observer::aaLine.
The source code above is updated, and works for me as a solution.
The image below comes from tests: Blue's are NOT antialiased, and Red's ones are the results from the solution above. I am satisfied with what I want to do.
I think your problem lies in this:
auto plot = [&](double X, double Y, double brighness){
pC->Pixels[X][Y] = brightness; };
If I understand it correctly pC is some target TCanvas ... this has 2 major problems:
pC->Pixels[X][Y] = brightness; will handle brightness as color according to selected mode (so copy,xor,... or whatever) and not as brightness.
I would use form of alpha blending where you take originaly render color (or background) and wanted color of rendered line and mix it with brightness as parameter:
TColor c0=pC->Pixels[X][Y],c0=color of your line;
// here mix colors c = (c0*(1.0-brightness)) + (c1*brightness)
// however you need to do this according to selected pixelformat of you graphic object and color channel wise...
pC->Pixels[X][Y]=c;
Beware VCL transparency does not use alpha parameter its just opaque or not ... For more info about the mixing see similar:
Digital Differential Analyzer with Wu's Algorithm in OpenGL
especially pay attention to the:
union
{
DWORD dd;
BYTE db[4];
} c,c0;
as TColor is 32bit int anyway ...
speed of pC->Pixels[X][Y] in VCL (or any GDI based api) is pitiful at best
in case you handle many pixels you should consider to use ScanLine[Y] from Graphics::TBitmap ... and render to bitmap as backbufer. This usually improve speed from ~1000 to ~10000 times. for more info see:
Graphics rendering in C++

C++ Raylib how to detect the side of a rectangle that a circle has collided with

I can use the function CheckCollisionCircleRec(Vector2{ x, y }, radius, paddleRect) to find out simply if my circle has collided with my rectangle, but I want to be able to find out what side of the rectangle my circle has collided with. How would I go about doing this? None of the algorithms I've made are working. Example of my most recent blunder:
if (x - radius <= 0 || x + radius >= screenWidth) {
speedX *= -1;
}
else if (y - radius <= 0 || y + radius >= screenHeight) {
speedY *= -1;
}
else if (CheckCollisionCircleRec(Vector2{ x, y }, radius, paddleRect)) {
float paddleBottom = paddleRect.y + paddleRect.height;
float paddleRight = paddleRect.x + paddleRect.width;
if (range(paddleRect.x, paddleRect.x + speedX / 100, x + radius)) {
x = paddleRect.x - radius;
speedX *= -1;
}
if (range(paddleRight - speedX / 100, paddleRight, x - radius)) {
x = paddleRight + radius;
speedX *= -1;
};
if (range(paddleRect.y, paddleRect.y + speedY / 100, y + radius)) {
y = paddleRect.y - radius;
speedY *= -1;
}
if (range(paddleBottom - speedY / 100, paddleBottom, y - radius)) {
y = paddleBottom + radius;
speedY *= -1;
};
EDIT:
Here's the function I used to get the working end result:
// px and py are the ball's previous locations
// x and y are the ball's current locations
void checkCollision(Rectangle rectangle) {
int left = rectangle.x;
int right = rectangle.x + rectangle.width;
int top = rectangle.y;
int bottom = rectangle.y + rectangle.height;
if (CheckCollisionCircleRec(Vector2{ x, y }, radius, rectangle)) {
if (px < left) {
speedX = negative(speedX);
x = left - radius;
}
else if (px > right) {
speedX = positive(speedX);
x = right + radius;
}
else if (py < top) {
speedY = negative(speedY);
y = top - radius;
}
else if (py > bottom) {
speedY = positive(speedY);
y = bottom + radius;
};
};
};
A simply way is to use the PREVIOUS location of your circle. Not sure if you can in your program, but since you have an x and y handy, I'll assume you can have a prevX and prevY. I'll also assume these values represent the CENTER of the circle.
Now if (prevX < paddleRect.x), then you likely collided with the left side (not guaranteed, but resolving ambiguities with complete accuracy requires recursively simulating your physics at smaller and smaller timesteps, which is likely unnecessary here). You can also constrain this more tightly with something like if (prevX < paddleRect.x && prevY > paddleRect.y && prevY < paddleRect.y + paddRect.height). There are various constraints you can add depending on how cleanly you want the side to be hit before detecting it. You can add corner hits, etc.
The reason for using the previous location is that, if your circle is moving fast enough, then in a single frame it can jump straight into the middle of the rectangle. It's usually necessary to use the previous position to give more specific collision information in the current-location collision

Code about a particle bouncing on a curved surface

I'm supposed to write a code that simulates an inelastic particle bouncing within a wall and a semicircle (radius 30, center # (50,0). The particle loses 3% of its original velocity every time it hits a wall and/or the semicircle.
I'm also using graphics.h library
I have made most of the program already, but the particle does not properly bounce on the semicircle.
I used the reflection matrix:
\begin{bmatrix}
cos 2\theta & sin2\theta \
sin2 \theta & -cos2\theta
\end{bmatrix}
here is the this snippet of the code containing the conditions when the particle hits the semicircle:
//initial conditions
x = 0;
y = 100;
Vx = 15.0;
Vy = 0.0;
double dt = 0.0001; //initial time step and increment
for(double t = 0; t < 50; t += dt)
{
Vy = Vy + grav*dt;
//Conditions when the particle hits a wall or the floor and retaining only 97% of its speed
if (y<0.0)
{
Vy = -Vy + grav*dt;
}
if(x >100.0 || x < 0.0)
{
Vx = -Vx;
}
//condition when the particle hits the semicircle
//semicircle equation: y = sqrt(30^2 - (x-50)^2)
if(y < sqrt((30*30)-((x-50)*(x-50))) )
{
Vxnew = ((Vx)*(cos(2.0*theta)) + (Vy*(sin(2.0*theta))));
Vy = (Vy*(cos(2.0*theta)) - ((Vx)*(sin(2.0*theta))));
Vx = Vxnew;
}
x = x + Vx*dt;
y = y + Vy*dt;
putpixel(conx(x), cony(y), 15);
}
I'm not sure if this reflection matrix applies on curved surfaces and I should use something else or my implementation is wrong.
For the losing the velocity part, I can probably just multiply the new x and y velocities by 0.97.
For context, this function that I made converts the calculated value in the loop to pixel values
//convert to pixel value (scale of 6)
double conx(double x)
{
return x * (600/100) + 50;
}
double cony(double y)
{
return -y * (600/100) + 650;
}

C/C++ declared and defined variable turns invisible

The entirety of my code is a bit too much to post on to here so I'll try to show the essentials.
I am coding a simple graphically represented analogue clock (12-hour with three hands).
Currently my code works if I let the clock run from default i.e. all hands start at 12.
However I have added a feature that allows editing of the time shown and inherent to this, regardless of starting position of the hand, when it hits 12, the larger respective hand should then tick once. My code is below.
for (psi = 0; psi<6.28318530718-0.5236; psi+=0.5235987756) {
float xply = sin(psi);
float yply = cos(psi);
int hhx = x0 + (circleRad-100)*xply;
int hhy = y0 - (circleRad-100)*yply;
float phi;
for (phi = 0; phi<6.28318530718-0.10472; phi+=0.1047197551) {
float Multx = sin(phi);
float Multy = cos(phi);
int mhx = x0 + (circleRad-50)*Multx;
int mhy = y0 - (circleRad-50)*Multy;
float theta;
for (theta= 0; theta<6.28318530718-0.104720; theta+=0.1047197551) {
// If seconds are given then the if condition is tested
if (secPhase > 0) {
float angle = theta+secPhase;
// If second hand reach top, for loop breaks and enters a new loop for next minute, secphase is erased as new minute start from 0 secs.
if (angle > 6.28318530718-0.104720) {
plotHands(angle, x0, y0, circleRad, a, mhx, mhy, hhx, hhy, bytes);
capture.replaceOverlay(true, (const unsigned char*)a);
sleep(1);
secPhase = 0;
break;
}
// if second hand has not reached top yet, then plotting continues
plotHands(angle, x0, y0, circleRad, a, mhx, mhy, hhx, hhy, bytes);
capture.replaceOverlay(true, (const unsigned char*)a);
sleep(1);
}
// if there were no seconds given, plotting begins at 12.
else {
plotHands(theta, x0, y0, circleRad, a, mhx, mhy, hhx, hhy, bytes);
capture.replaceOverlay(true, (const unsigned char*)a);
sleep(1);
}
}
}
}
Currently my code works for seconds. There are declared and defined values, that I have not included here, that I can alter that will change the starting position of each hand and wherever the second hand is, when it hits 12 the minute hand will tick once.
This is the problem. Logically, I could just apply the same concept that I used for the second hand but migrate it to the minute hand and change the respective variable names involved so that when the minute hand does strike 12, the hour hand will move. This is the code that breaks:
for (phi = 0; phi<6.28318530718-0.10472; phi+=0.1047197551) {
if (minPhase > 0) {
float minAngle = phi + minPhase;
if (minAngle > 6.28318530718-0.10472) {
minPhase = 0;
break;
}
float Multx = sin(minAngle);
float Multy = cos(minAngle);
int mhx = x0 + (circleRad-50)*Multx;
int mhy = y0 - (circleRad-50)*Multy;
}
else {
float Multx = sin(phi);
float Multy = cos(phi);
int mhx = x0 + (circleRad-50)*Multx;
int mhy = y0 - (circleRad-50)*Multy;
}
}
I have taken only the middle for loop involving the minute hand. These loops and statements ensure that if there is no given starting point of the minute hand, the else statement will run, but if there is a starting point, the starting point will tick until it strikes twelve and which point it breaks to the hour for loop, ticks once, whilst clearing the starting point of the minute hand to start afresh in the new hour.
However once I attempt to compile the code, the compiler tells me:
error: 'mhx' was not declared in this scope
error: 'mhy' was not declared in this scope
it shows this everytime this variable is called in the function to draw the minute hands and is as if these variables have simply disappeared. They have clearly been declared and defined in my code by when attempted to be called in the for loop below it, it claims that these variables are missing.
I found also that if I removed the 'else' statement, the code compiled and run, but was broken, i.e. the minute hand was not in its supposed position.
Can anyone enlighten me please? I am still very new to C and C++.
Thank you in advance.
The variables go out of scope when they hit the closing brace of either the if or the else. Declare them outside of the scope and assign their values inside the if/else blocks.
for (phi = 0; phi<6.28318530718-0.10472; phi+=0.1047197551) {
if (minPhase > 0) {
float minAngle = phi + minPhase;
if (minAngle > 6.28318530718-0.10472) {
minPhase = 0;
break;
}
float Multx = sin(minAngle);
float Multy = cos(minAngle);
int mhx = x0 + (circleRad-50)*Multx;
int mhy = y0 - (circleRad-50)*Multy;
// Multx, Multy, mhx, mhy will go out of scope when the following brace is reached
}
else {
float Multx = sin(phi);
float Multy = cos(phi);
int mhx = x0 + (circleRad-50)*Multx;
int mhy = y0 - (circleRad-50)*Multy;
// Multx, Multy, mhx, mhy will go out of scope when the following brace is reached
}
}
You should instead do this:
for (phi = 0; phi<6.28318530718-0.10472; phi+=0.1047197551) {
float Multyx, Multy;
int mhx, mhy;
// These variables will now be visible in the entire for loop's scope not just the if or else statement they were declared into.
if (minPhase > 0) {
float minAngle = phi + minPhase;
if (minAngle > 6.28318530718-0.10472) {
minPhase = 0;
break;
}
Multx = sin(minAngle);
Multy = cos(minAngle);
mhx = x0 + (circleRad-50)*Multx;
mhy = y0 - (circleRad-50)*Multy;
}
else {
Multx = sin(phi);
Multy = cos(phi);
mhx = x0 + (circleRad-50)*Multx;
mhy = y0 - (circleRad-50)*Multy;
}
}
You need to move mhx and mhy to the scope above the if statement to be visible outside the if/else.
for (phi = 0; phi<6.28318530718-0.10472; phi+=0.1047197551) {
int mhx, mhy; // move declaration here
if (minPhase > 0) {
float minAngle = phi + minPhase;
if (minAngle > 6.28318530718-0.10472) {
minPhase = 0;
break;
}
float Multx = sin(minAngle);
float Multy = cos(minAngle);
mhx = x0 + (circleRad-50)*Multx; // no longer a declaration, just assignment
mhy = y0 - (circleRad-50)*Multy;
}
else {
float Multx = sin(phi);
float Multy = cos(phi);
mhx = x0 + (circleRad-50)*Multx; // no longer a declaration, just assignment
mhy = y0 - (circleRad-50)*Multy;
}
}
I assume you have other code in the body of your for loop after this if statement that you haven't shown.