Review of class rendering code using marmalade c++

Started by
3 comments, last by Pink Horror 9 years, 1 month ago

Hi All,

I'm trying to clone space invader of atari and I have problem with bounding rect calculations.. the rect is not correctly bounded.. maybe I have problem with the transformations


void Barrier::Render()
{
	 
	if (mBarrierImage)
	{
		CIwFMat2D Transform;
		// Build transform
		Transform.SetIdentity();
		Transform.SetRot(0);
		Transform.ScaleRot(1);
		Transform.SetTrans(Position);
		Iw2DSetTransformMatrix(Transform);

		Iw2DSetColour(0xffffffff);
	 
	CIwFVec2 pos = Transform.t;
	BoundingRect.x = Position.x;
	BoundingRect.y = Position.y;
	BoundingRect.w = (float)mBarrierImage->GetWidth();
	BoundingRect.h = (float)mBarrierImage->GetHeight();
	Iw2DSetColour(0xff0000ff); // Set red
	Iw2DDrawRect(CIwFVec2(pos.x, pos.y), CIwFVec2(mBarrierImage->GetWidth(), mBarrierImage->GetHeight())); // Draw red outline

}

Advertisement

You have asked about this at least four times in the past month.

Each time you ask something slightly different, but they all have some facets in common.

You seem to be confused about the difference between the graphics that are displayed and the way the data is kept inside the game.

I'll suggest that the project is beyond your current skill level. Take a step back and develop a simpler game. Then come back to it when you've had your own educational "level up" experience.

Common recommendations for a first interactive graphical game are Pong, then after you've mastered that, often a Tetris or Breakout/Arkanoid game. They are much simpler and will teach you much about the internal representation of data.

Thanks so much for your suggestion and your help. But would you tell me what's the problem with the above code? At least explaining to me the problem above would give me hints to work on simpler game. Because for example in a pong game, I will still have to use bounding boxes,..etc

I agree with Frob.

The code you have posted above is at least missing a curly bracket after the if statement. And you're also mixing rendering with your game model, which is a big architectural mistake. You should never change your model state within your rendering code.

If you're struggling with bounding rects, you need to back up a bunch and just get a simple bounding rect working in isolation. Forget about your game and pong, that's too much complexity for you right now. Before you even write your first line of code, you need to have a conceptual understanding of what you're trying to do. Draw a picture if it helps (I do this all the time with a hand held white board). If you can't get a conceptual understanding of what you're trying to do, then it's futile to write code.

So, let's start super simple:

What is a boundary?

It's a line, right?

Let us define a simple flat line like Y = 0

What points are above it? Which ones are below it? How can you tell? Can we write code which can return a boolean value for this? If not, then you've got some basics in code and mathematics which need to be reviewed. Let's assume that you can.

Well, let's complicate the problem a bit now. Let's draw two horizontal lines.

Line 1: Y = 0

Line 2: Y = 2

Can we figure out which points lie between lines 1 and 2? It should be pretty straight forward.

Now, let's complicate the problem slightly more. Can we draw two vertical lines as well?

Line 3: X = 0

Line 4: X = 2

Can we figure out if a point lies between those two lines? Can we figure out if a point lies between all four lines?

If you can, now you've got a bounding box!

Typically with most bounding box implementations, you specify a rectangle by setting the top left coordinate, then specifying a width and height. Then the bounding rectangle may have a method which returns a boolean value on whether or not the rectangle contains a point. The math for this is super simple.

Now, if you want to get crazy, you can also rotate this bounding box. The question is, what do you rotate around? Do you use the top left corner or the center? How do you know if a point is within a rotated bounding box?

Simple: You figure out the rotation of the bounding box, the pivot point of that box, and then you unrotate the bounding box around the pivot point, and you also unrotate the position you're testing against.

Can you do all of this? Where do you get stuck?

If it helps, forget about rendering anything to the screen and stick to pure mathematical representation until you get it right. Use a C++ console application to test your bounding box code. If you still struggle with this, you're going to have to do some self-teaching. The answers are already out there, you have to find them.

The essence of all programming is breaking a problem down into its smallest parts, solving those, then putting the parts back together to solve a bigger problem.

Thanks so much for your suggestion and your help. But would you tell me what's the problem with the above code? At least explaining to me the problem above would give me hints to work on simpler game. Because for example in a pong game, I will still have to use bounding boxes,..etc

You didn't say where the BoundingRect variable comes from or what it's used for.

You didn't say where the Position variable comes from.

Your missing curly brace makes it unclear whether the Transform variable in the second part is the same as the one in the first part, because I'm not sure they're supposed to be the same scope.

You didn't even say what's wrong with the bounding box. It's just not "correctly bounded".

What do you expect anyone to do?

This topic is closed to new replies.

Advertisement