Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I am trying to implement balls that change their direction when they hit an obstacle(in this case a fixed ball). I can detect when a collision occurs but I don't know how to modify the direction of a ball once it hits an obstacle. Here's some code:
struct Vec2
{
float x, y;
};
struct Ball
{
void onCollision(const Ball& fixedBall)
{
// This function will be called when a collision occurs
// Speed will be constant, only direction needs to change
}
void update()
{
position += direction * speed;
}
Vec2 position, direction; // direction is a normalized vector
float speed, radius;
};
You will need to invert the speed by negating it.
if (collision)
speed = speed * -1
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 10 months ago.
Improve this question
I have a simple 2D game of pool. You can give a ball some speed, and it'll move around and hit other balls. But I want a ball to stop eventually, so I added some acceleration, by running this code every frame:
balls[i].ax = -balls[i].vx * 0.1;
balls[i].ay = -balls[i].vy * 0.1;
...
if(hypot(balls[i].vx, balls[i].vy) < 0.2){
balls[i].vx = 0;
balls[i].vy = 0;
}
And it works... But I find it weird, not realistic. I have no physics knowledge, but I'm pretty sure friction should not depend on speed.
How can I improve physics of slowing down without too much complexity?
The rolling friction formula is this: F_k,r=μ_k,r_Fn. It only factors in the properties of the surface (μ_k) and the force on the ball (r_Fn). This should decelerate with a constant value, just adjust it until it looks roughly correct.
Example code:
x = 1 // mess around with this until it looks right
if (ball.xVelocity > x) { ball.xVelocity -= x } else { ball.xVelocity = 0 }
if (ball.yVelocity > x) { ball.yVelocity -= x } else { ball.yVelocity = 0 }
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
In my fragment shader, I need to use uvec4.
My shader:
layout (location = 0) out uvec4 final_color;
void main(void) {
final_color.r = 0;
final_color.g = 4294967295;
final_color.b = 0;
return;
}
I think, this will be green because unsigned_int max is 4294967295. (2^32-1) but it is just black.
I tried singed int max, 2147483647, it's black too. but 2137483648 works. Where is the boundary?
I want know max value, like 255(8bit) or 1.0f(float, vec4).
my program draw result to window directly. I need to see it.
Well, you can't. A uvec4 contains unsigned, 32-bit integers. That output can only be written to an image that uses an unsigned integer image format. Note that this is different from a normalized integer image format.
You cannot create a default framebuffer that contains non-normalized integers, only floats and normalized integers. So you can't "see it".
As such, that value is not a "color"; it's just data stored in a texture. What it means is entirely up to how you will eventually use the value in that texture to get something displayable.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I want to create a lens flare in OpenGL C++. I referred this video and star rendering code.
double calculateGlowSize(double diameter, double temperature, double distance) {
static const double DSUN = 1392684.0;
static const double TSUN = 5778.0;
// Georg's magic formula
double d = distance; // Distance
double D = diameter * DSUN;
double L = (D * D) * pow(temperature / TSUN, 4.0); // Luminosity
return 0.016 * pow(L, 0.25) / pow(d, 0.5); // Size
}
but i want the output to be more like the image shown below.
How do i create a lens flare effect?
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I am recently implement a voxel cone tracing algorithm, the first step is to voxelize polygons...
I am using sparse octree to convert the polygon, everything was fine except for texture mapping
mipmap=6 (64x64x64)
// We find its smallest AABB
AABB aabb = FindAABB(in);
// better aabb with same width, height and depth
AABB betterAABB = FixAABB(aabb);
// Create Octree and marked the grids that are touched by triangles in the mesh
// dfunc is a callable object which return true when triangle and grid are overlapped
// cb is a callable object, it is called when octree reaches its leaf..
Octree<uint8> tree(betterAABB, mipmap);
for(auto &f: in.faces)
tree.findTouchedLeaves(f, dfunc, cb);
Callback function...
struct Callback
{
void operator()(OctreeNode<uint8> *node, const Face &obj)
{
// Check if the node is already marked.
if(!node->data.value)
{
const glm::vec3 ¢er=node->aabb.getCenter();
out.push_back(center);
// Problem here!! how to calculate precise uv for texture mapping
color.push_back(text->getPixel((obj.o.texCoord+obj.a.texCoord+obj.b.texCoord)/3.0f));
}
node->data.value=1;
}
std::vector<glm::vec3> out;
std::vector<glm::vec4> color;
const ImageTexture *text;
} cb;
as you can see... I directly average three UVs of o,a,b.(terrible right?)
It is more imprecise when a triangle is larger than a grid.
Should I need to use rasterize-based algorithm?
My question is: How to precisely coloring a grid?
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I do have a class of Matrix and I access to it with two loops and have stored in it all the values I wanted to have in it.
Matrix MatriceJ(width, height);
for (int i=0;i<width;i++)
{
for (int j=0;j<height;j++)
{
MatriceJ.at(i,j)=....
}
}
But now, I would like to store the MatriceJ in an IplImage* for that I can multiply its different elements, one by one, with the other IplImages.
Can you help me with that?
This should get you started. I assume the data to be unsigned char and one channel, please adjust accordingly.
// Create the image
int depth = IPL_DEPTH_8U; // please adjust
int channels = 1; // please adjust
IplImage* img = cvCreateImage(cvSize(width,height), depth, channels);
// Now assume there is a matrix MatriceJ
// Copy the data to our newly created IplImage*
for (int i=0;i<height;i++)
{
uchar* ptr = (uchar*)(img->imageData + i*img->widthStep);
for (int j=0;j<width;j++)
{
ptr[j] = MatriceJ(i,j);
}
}