Missile Command

The Partridge Family were neither partridges nor a family. Discuss.
User avatar
chili
Site Admin
Posts: 3948
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Re: Missile Command

Post by chili » June 1st, 2012, 2:12 pm

Inside of your function, gfx is a pointer. In order to access the object that the pointer points to, you have to dereference the pointer. This is the same for objects, ints, or any kind of data.

You need to do this:
(*gfx).PutPixel

You can also write it like this (a shortcut):
gfx->PutPixel
Chili

User avatar
Asimov
Posts: 814
Joined: May 19th, 2012, 11:38 pm

Re: Missile Command

Post by Asimov » June 1st, 2012, 3:10 pm

Hi Chilli,

Thank you that worked great.
I used Missile.DrawMissile(&gfx); to call my function.

Anyway it draws the missile on screen. Just a couple of things. I forgot to set black to be transparent when I made the sprite, so I have to remake that.

Also the coordinates from the LUX sprite are done from the top left corner and so that is why the centre isn't on the line. Might have to compensate for that.

Next when I hit the mouse button I want it to travel towards the target. So I will add a fire function to my missile class. I am thinking the answer to working out the trajectory will be similar code to the DrawLine function, which is why I used DrawLine to test it.

I am kinda thinking on a 800*600 screen my missile might be a bit large, but I can change that later.

Anyway here is my screen shot. My missile was created in 3ds Max and looked a lot better much larger heh heh. The reason I chose Max instead of photoshop was this. I can re-render it at any size without loss of quality.

Asimov
Attachments
missile.jpg
(30.29 KiB) Downloaded 136 times
----> Asimov
"You know no matter how much I think I have learnt. I always end up hitting brick walls"
http://www.asimoventerprises.co.uk

User avatar
Asimov
Posts: 814
Joined: May 19th, 2012, 11:38 pm

Re: Missile Command

Post by Asimov » June 1st, 2012, 4:06 pm

Hi all,

Well I have a basic firing missile now. Basically at the moment it ignores the position of the crosshair and just fires up the screen. I did this to test the trigger which is a bool called Fired. While it is true the missile will move and it will become false when it hits it's destination.

So the next stage is to make the missile head towards it's target. Then multiple missiles, which is why I am using a class.

I am wondering if I should somehow lock the mouse in the screen while the game is being played as sometimes you can click outside while playing. Also I haven't got the error detection working properly yet. The other problem is that when the missile hits it's destination there will be an explosion. Trouble is that if this is next to the edge of the screen it is going to crash, because the edge of the explosion which will be a circle will go off the screen.

Anyway progress so far attached.

Asimov
Attachments
Missile Command.zip
(177.33 KiB) Downloaded 182 times
----> Asimov
"You know no matter how much I think I have learnt. I always end up hitting brick walls"
http://www.asimoventerprises.co.uk

User avatar
LuX
Posts: 1492
Joined: April 22nd, 2012, 12:33 pm
Location: Finland

Re: Missile Command

Post by LuX » June 1st, 2012, 9:53 pm

coordinates from the LUX sprite are done from the top left corner
Just add to the drawing position "+ half of width and height", maybe I'll add an option to choose the origin at some point.
as sometimes you can click outside while playing
Use the "if mouse is in screen" command provided in "mouse", if false, ignore mouse clicks.
Trouble is that if this is next to the edge of the screen it is going to crash
You can seriously start using clipped pixels, they are only illegal in North-Korea. :lol:
ʕ •ᴥ•ʔ

User avatar
Asimov
Posts: 814
Joined: May 19th, 2012, 11:38 pm

Re: Missile Command

Post by Asimov » June 2nd, 2012, 1:19 am

Hi Lux/Chilli,

Well you won't believe this, but I spent all night trying to work out how to move my sprite to the target. I knew the answer was in the Drawline function because really it is travelling in the line to the target.

I knew y was constant, and really I only had to work out x. Anyway finally I got a working solution.
At the moment though if I move the mouse after firing it will alter trajectory so really I should store the coordinates of the mouse at trigger time. That is the next step, but here is my code so far.

I wish I was a maths genius like Chilli, but I am crap LOL.

Also I need to work out how to do pixel clipping. I have just watched tutorial 20 but haven't come across that. I suspect in the putpixel you would check if a pixel is being drawn outside the screen and stop it drawing.

Code: Select all

void Missile::FireMissile(int crosshairX,int crosshairY)
{
	int dx= crosshairX - x;
	int dy = crosshairY - y;
	
	float m = (float)dx / (float)dy;
	float b=x - m*y;

	if (y>crosshairY)
	{
	y=y-5;
	
	x = m*y +b + 0.5f;

	}
	else
	{
		MissileFired(false);
		x=400;
		y=580;
	}
	
}
Asimov
----> Asimov
"You know no matter how much I think I have learnt. I always end up hitting brick walls"
http://www.asimoventerprises.co.uk

User avatar
LuX
Posts: 1492
Joined: April 22nd, 2012, 12:33 pm
Location: Finland

Re: Missile Command

Post by LuX » June 2nd, 2012, 8:28 am

If that works, good. But there's a way to do it that I prefer more. One that lets you know the angle it's been shot and a quick and simple to alter velocity...

"clipped" pixels didn't come in tutorials, it was something chili taught us here. But the idea is simple: you alter your PutPixel, or create a new one that instead of checking assert, checks if the pixel drawing positions are withing the screen or not and then draws if it is inside.
ʕ •ᴥ•ʔ

User avatar
Asimov
Posts: 814
Joined: May 19th, 2012, 11:38 pm

Re: Missile Command

Post by Asimov » June 2nd, 2012, 11:08 am

Hi all,

Well I realise that my code works, but will only work for one missile, so now I realise I have to take more of the program into the class eg look at the following code. This is the test for missile firing. In the game.cpp.

Code: Select all

if (mouse.IsInWindow() && mouse.LeftIsPressed())
	{
		//gfx.DrawLine(Missile.MissileX(),Missile.MissileY(),mouse.GetMouseX(),mouse.GetMouseY(),255,255,255);
		Crosshair(mouse.GetMouseX(),mouse.GetMouseY());
		Missile.MissileFired(true);
	}
	if (Missile.Fired==true)
	{
		Missile.FireMissile(mouse.GetMouseX(),mouse.GetMouseY());
	}
I have now changed this code to this

Code: Select all

if (mouse.IsInWindow() && mouse.LeftIsPressed())
	{
		//gfx.DrawLine(Missile.MissileX(),Missile.MissileY(),mouse.GetMouseX(),mouse.GetMouseY(),255,255,255);
		Crosshair(mouse.GetMouseX(),mouse.GetMouseY());
		Missile.MissileFired(true);
	}

		Missile.FireMissile(mouse.GetMouseX(),mouse.GetMouseY());
Notice I have removed the if statement. That is because now the if statement is in the class itself.
Next I have to do the same with the mouse check. If I leave the mouse check in the main game.cpp I will not be able to check for more than one missile.

Here is my missile firing function in my missile class

Code: Select all

void Missile::FireMissile(int crosshairX,int crosshairY)
{
	
	if (Fired==true)
	{
		
	
		int dx= crosshairX - x;
		int dy = crosshairY - y;
	
		float m = (float)dx / (float)dy;
		float b=x - m*y;

		if (y>crosshairY)
		{
		y=y-5;
	
		x = m*y +b + 0.5f;
		}
		else
		{
			MissileFired(false);
			x=400;
			y=580;
		}
	}
}
If it is not set to true it just returns.

So next I have to do the same with my mouse check. So I will create a function called something like mouse check in my missile class and this will be called from game.cpp. The beauty of this is that I can have as many missiles as I want then. Of course I will probably have to pass a pointer from the mouse class like I did with the gfx.

Edit: just realised I could use if(Fired) instead of if(Fired=true). Although I am not sure it makes that much difference.

Let me know if you think I am heading in the right direction, or if you don't understand me at all LOL.

Edit: Ok since posting the above I have done what I said I was going to. Below is my new code in my main.cpp. Look how neat it is and now I can run multiple missiles (Well that is the theory, once I work out how to stop the missile following my moving mouse LOL).

Code: Select all

Missile.MouseCheck(&mouse);
Missile.FireMissile(mouse.GetMouseX(),mouse.GetMouseY());
Edit:Edit I am on fire now. I have can call my whole missile firing check from one line now woohoo

Code: Select all

Missile.MouseCheck(&mouse);
Asimov
----> Asimov
"You know no matter how much I think I have learnt. I always end up hitting brick walls"
http://www.asimoventerprises.co.uk

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

Re: Missile Command

Post by chili » June 3rd, 2012, 12:19 am

Lookin' good Asimov.

To be honest, I'm not sure if passing the mouse object to the Missile object is the best idea from the viewpoint of design, but it's good experience none the less.

Looking for ware to seeing the finished product. :)
Chili

User avatar
Asimov
Posts: 814
Joined: May 19th, 2012, 11:38 pm

Re: Missile Command

Post by Asimov » June 3rd, 2012, 12:51 am

Hi Chilli,

I took the mouse out of the class again, because it wouldn't work with what I was trying to achieve. In fact the code has changed many times since the last post. I have got multiple missiles to fire now, and it goes to the target correctly now. It stores the mouse coodinates at the point of the left click before firing now.

You know what I have used pointers before your I saw you tutorials. I used them to accomplish a task, and I was guided to use them. However I didn't quite know what they did, and why they were needed until I watched your pointer tutorial. What I have achieved so far doesn't look too impressive, but it is heading in the right direction.

My biggest problem is that before you have lifted your mouse the missile has fired multiple times, and I found a bit in your tic tac toe tutorial which should have solved the issue, but unfortunately it didn't. Here is the problem code. I am testing !keypressedlastframe so it should wait for me to lift the mouse button before the next missile is fired. Also I don't know how to do clipping yet, so make sure the mouse pointer is in the middle of the screen before you run the test or it will crash nicely.

I have a long way to go before this is finished heh heh.

Code: Select all

if (!keypressedlastframe)
{
	keypressedlastframe=true;
	if (mouse.IsInWindow() && mouse.LeftIsPressed() && !Missile[missilecount].Fired)
		{
			Missile[missilecount].MissileFired(true,mouse.GetMouseX(),mouse.GetMouseY() );
			missilecount++;
			char buf[2048];
sprintf(buf,"The value of variable num is %d \n",missilecount);
OutputDebugStringA(buf);
			if (missilecount>=NOM){
				missilecount=0;
			}
		}		
}
else
{
	keypressedlastframe=false;
}

Youtube link to a video of my first test.
http://youtu.be/3h0z8j1oOA8
Asimov
Attachments
03_Missile Command.zip
(179.12 KiB) Downloaded 193 times
----> Asimov
"You know no matter how much I think I have learnt. I always end up hitting brick walls"
http://www.asimoventerprises.co.uk

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

Re: Missile Command

Post by chili » June 3rd, 2012, 4:40 am

Looking good bro. :)

I think your Missile class is well-built. The only thing I would change is:

Lose the missleCount variable; it is superfluous.
Rename your function names to make them easier to understand:
MissileFired -> FireMissile
FireMissile -> UpdateMissile

One more problem is that when you fire at an angle that is close to horizontal, the missiles move much faster than if you fire straight up. It's because you have made the y-direction speed constant. Gonna take a little bit of math to make it so the missile moves at a constant speed no matter the angle, and I know how much you like math Asimov. :lol: (Hint: the distance formula [i.e. Pythagorean theorem] will help you out here. I also recommend storing your x and y positions as floats.)
Chili

Post Reply