my solutions

The Partridge Family were neither partridges nor a family. Discuss.
randomGuy
Posts: 32
Joined: May 31st, 2018, 11:45 am

Re: my solutions

Post by randomGuy » August 31st, 2018, 12:55 pm

https://youtu.be/7cBUaFc1H8Q?list=PLqCJ ... l43S&t=773

Will we get the same result if we don't normalize the vector from the intersection point to the circle?
We are taking the sign of the dotted product so does size take a role here?

randomGuy
Posts: 32
Joined: May 31st, 2018, 11:45 am

Re: my solutions

Post by randomGuy » August 31st, 2018, 1:31 pm

I should ask questions after watching the full video haha

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: my solutions

Post by albinopapa » August 31st, 2018, 9:36 pm

I'm guessing you got your answer?
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

User avatar
chili
Site Admin
Posts: 3948
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Re: my solutions

Post by chili » September 1st, 2018, 8:48 am

Yeah, wait until you do Advanced 4. I show a way of getting the plank normal that is waaaay simpler.
Chili

randomGuy
Posts: 32
Joined: May 31st, 2018, 11:45 am

Re: my solutions

Post by randomGuy » September 3rd, 2018, 11:05 am

Yes albinopapa, I got my answer at the end of the video.

After you explained how rotation works I went back to the starfield and tried to implement it.
Instead of calculating sin and cos for every object we are just going to change our direction unit vectors x hat and y hat.
So now we're applying rotation to the field, rather than every star.

added float dAngle to Camera.h so I have control over from Game::UpdateModel()

Code: Select all

	if (wnd.kbd.KeyIsPressed('D'))
	{
		cam.setAngle(cam.getAngle() + 0.01f);
	}

	if (wnd.kbd.KeyIsPressed('A'))
	{
		cam.setAngle(cam.getAngle() - 0.01f);
	}
in camera.h

Code: Select all

	void DrawClosedPolyline(Vec2 entityPos, std::vector<Vec2> poly,Color c)
	{	
		Vec2 x = { cos(dAngle), sin(dAngle) };
		Vec2 y = { -sin(dAngle), cos(dAngle) };
		
		for( auto& v : poly )
		{	
			v *= zoom;
			v -= pos;
			Vec2 rotatedX = x * v.x;
			Vec2 rotatedY = y * v.y;
			v = rotatedX + rotatedY;
		

		}
		ct.DrawClosedPolyline( std::move( poly ),c );
	}

randomGuy
Posts: 32
Joined: May 31st, 2018, 11:45 am

Re: my solutions

Post by randomGuy » September 3rd, 2018, 11:15 am

It might even be better to put this part

Code: Select all

Vec2 x = { cos(dAngle), sin(dAngle) };
Vec2 y = { -sin(dAngle), cos(dAngle) };
in Camera::setAngle and have x hat and y hat unit vectors as parameters.

randomGuy
Posts: 32
Joined: May 31st, 2018, 11:45 am

Re: my solutions

Post by randomGuy » September 15th, 2018, 1:17 pm

https://youtu.be/cN97hkDrzcc?list=PLqCJ ... d8lA&t=172

Should we not get rotation in the LEFT direction when applying positive theta?
Take a look at the picture.
Attachments
rotation.jpg
(76.33 KiB) Not downloaded yet

randomGuy
Posts: 32
Joined: May 31st, 2018, 11:45 am

Re: my solutions

Post by randomGuy » September 15th, 2018, 3:22 pm

Is it because, the y direction isn't flipped?

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: my solutions

Post by albinopapa » September 16th, 2018, 2:56 am

randomGuy wrote:Is it because, the y direction isn't flipped?
Yes.

Your yHat of { 0, 1 } would be pointing down, whereas your diagram would be more like { 0, -1 }

So, before rotation: { 95, -29 }, after
counter clockwise rotation in screen coordinates { -29, -95 } ( y,-x )
and clockwise rotation in screen coordinates : { 29, 95 }{ -y, x }

Flipping the Y before you do the rotation, you get something more mathematically correct.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

randomGuy
Posts: 32
Joined: May 31st, 2018, 11:45 am

Re: my solutions

Post by randomGuy » September 20th, 2018, 4:50 am

https://youtu.be/9A5TVh6kPLA?list=PLqCJ ... 8lA&t=1824

The order in which triangles are drawn matters when we rotate so we need to draw the ones that are closest to us in the z direction last. We need to sort the indices array. I made a new class TriangleIndex which will hold an array of 3 indices and a color; Now our cube's array of indices holds TriangleIndex objects instead of just numbers so we can sort it before drawing.
So now how do we sort it? Our triangle has 3 points, which one do we pass to std::sort for comparing?
We pass the z value of the middle of the hypothenuse. But how do we get the hypothenuse of 3 points?
For each triangle index we make sure the first two points are the hypothenuse.

Code: Select all

		indices.emplace_back(2, 1, 0, w);//front
		indices.emplace_back(2, 1, 3, w);
		indices.emplace_back(3, 5, 1, b);//right
		indices.emplace_back(3, 5, 7, b);
		indices.emplace_back(6, 3, 2, c);//top
		indices.emplace_back(6, 3, 7, c);
		indices.emplace_back(4, 7, 5, g);//back
		indices.emplace_back(4, 7, 6, g);
		indices.emplace_back(2, 4, 0, gr);//left
		indices.emplace_back(2, 4, 6, gr);
		indices.emplace_back(1, 4, 0, m);//bottom
		indices.emplace_back(1, 4, 5, m);
and the sorting goes like this

Code: Select all

	std::sort(tIndices.begin(), tIndices.end(),
		[&](const TriangleIndex & a, const TriangleIndex & b) -> bool
	{
		std::vector<size_t> indicesA = a.indices;
		std::vector<size_t> indicesB = b.indices;
		float hA = (triangles.vertices[indicesA[0]].z + triangles.vertices[indicesA[1]].z) / 2;
		float hB = (triangles.vertices[indicesB[0]].z + triangles.vertices[indicesB[1]].z) / 2;

		return hA > hB;
	});
drawing

Code: Select all

	for (TriangleIndex& tIndex : tIndices) {
		Color c = tIndex.color;
		std::vector<size_t> indices = tIndex.indices;

		gfx.DrawTriangle(triangles.vertices[indices[0]], triangles.vertices[indices[1]],
			triangles.vertices[indices[2]], c);
	}

Post Reply