• Create Account

# Could anyone help me?

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

7 replies to this topic

### #1ronnieb555  Members   -  Reputation: 112

Like
0Likes
Like

Posted 20 May 2012 - 01:06 PM

Hi,
For my assignment at University I am required to create an old arcade game, I chose Missile Command. I have some things working in the game but something I cannot get to work is getting the rocket to fly towards the crosshair (the player) and then stop/explode when it reaches the crosshair location. Does anyone know how I can do it? #

So far I have the rocket flying towards the crosshair but the rocket is not starting from the base (blue triangle), it is starting at the same X coordinate as the crosshair.

My game will be attached to this thread.

To get the game to run you will need to change the directory of the images located on lines 113+ on Form1 as I have not found out how to save the image files within the program.

If you can help can you reply as soon as possible as I have a deadline that is fast approaching and I am focusing all my attention to this part of the game as it it the most important part. Please ignore the falling missiles from the top of the screen, I am also working on this to get them to spawn randomly across the X coordinates.

I have been using Visual Studio 2008 as that is the version used by my Uni.

Thanks.

Edit: To play the game the controls are set up with W, A, S, D for movement and Space for shooting, I am working on getting it to use the mouse.

Edit 2: The Bullet class is the players Rocket (The blue one) and the Rocket class is the NPC rockets (The Red ones).

#### Attached Files

Edited by ronnieb555, 20 May 2012 - 02:52 PM.

### #2Khatharr  Crossbones+   -  Reputation: 3078

Like
0Likes
Like

Posted 20 May 2012 - 05:51 PM

I'm not a C# user, but looking at your bullet.cs I see what looks like some problems.

Firstly, you have a member function named "draw" which is doing work that is not related to drawing: namely it's updating the bullet logic and then performing the draw.

Updating the logic for the object should belong to its own function.

Examining the logic in question, you adjust the position of the bullet according to the velocity factors which the object was created with and then do some bounds testing. While this could work theoretically I think you'd have better luck using point-to-point interpolation:

*When you create the object give it a starting position, an ending position and a velocity..
*Use Pythagorean Theorem to calculate the distance between the two points. (dest_x - start_x is your triangle's base, dest_y - start_y is your triangle's height. The hypotenuse is the distance you need to travel.)
*Determine how many frames/steps are required to travel this distance at the desired velocity.

*Then during the bullet's update set its X to ((dest_x - start_x) * cur_step) / total_steps
*And it's Y to ((dest_y - start_y) * cur_step) / total_steps
*Increment cur_step
*When cur_step == total_steps then explode the bullet

As a side note, this isn't really a DirectX or XNA related question, and your topic title isn't really helpful for grabbing the attention of people that may know answers to your specific question.

Hope that helps. Good luck with the project!

P.S. - Depending on your coordinate orientation you may need to reverse (dest_y - start_y) to (start_y - dest_y) and possibly also with the x dimension. If the bullet goes the wrong direction, that's your sign. ;)

Edited by Khatharr, 20 May 2012 - 05:54 PM.

void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

### #3ronnieb555  Members   -  Reputation: 112

Like
0Likes
Like

Posted 20 May 2012 - 06:18 PM

Thanks for the reply. I posted it in this area because I was told by my lecturer that this code was directx, is it not?

Would you be able to provide a more simple explanation for the method you have described? This is my first "big" programming assignment and I am fairly new to this type of programming.

Thanks.

### #4ronnieb555  Members   -  Reputation: 112

Like
0Likes
Like

Posted 21 May 2012 - 09:10 AM

I have managed to implement your method I think, but only up until this section;

"

*Then during the bullet's update set its X to ((dest_x - start_x) * cur_step) / total_steps

*And it's Y to ((dest_y - start_y) * cur_step) / total_steps"

Up to there the bullet does not travel across the screen and pops onto the screen only when the player(crosshair) is on the left, top of the screen.

I also have a u question, how can I work out the cur_step and the total_step?

I have attached the file with your code implemented as best I could.

### #5Khatharr  Crossbones+   -  Reputation: 3078

Like
0Likes
Like

Posted 21 May 2012 - 08:35 PM

I have a bit more time right now than I did last time I replied. So I'll take a closer look. While you may be using DirectX for rendering this issue is more of a general programming issue. It may be better under the C# section.

I'll edit after I've taken a look at your code.
void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

### #6Khatharr  Crossbones+   -  Reputation: 3078

Like
0Likes
Like

Posted 21 May 2012 - 09:05 PM

Okay, it's not showing me an edit button and I CBA to reload, so I'll just make a new post.

Since I don't know C# I'll show you a rough Ruby version of the class to demonstrate the technique.

class Bullet
def initialize(start_x, start_y, dest_x, dest_y, velocity)
@start_x = start_x
@start_y = start_y
@dist_x = dest_x - start_x
@dist_y = dest_y - start_y
distance = Math.sqrt((dist_x * dist_x) + (dist_y * dist_y)).to_i
@total_steps = distance / velocity #assuming velocity is measured in distance per frame

@curstep = 0
@x = @start_x
@y = @start_y
end

def update
if @curstep == @total_steps
explode
else
@x = @start_x + ((@dist_x * @curstep) / @total_steps)
@y = @start_y + ((@dist_y * @curstep) / @total_steps)

@curstep += 1
end
end

def draw
#draw the bullet (no movement should be processed here)
end
end

Of course that's pretty rough example, but it should demonstrate the technique. Calculate your distance in the ctor and use that distance to determine the number of frames it will take. (If velocity is distance-per-frame then total frames will be distance / velocity.)

When you update your position the math is:

(total distance) * (current step / total steps)

but since you're using integers you want to make sure that the multiplication takes place before the division (to prevent loss of precision).

For instance, to calculate half of 4 mathematically you'd say:

4 * (1 / 2)

But in programming, if you're using integers then you'd get a result of zero from that because (1 / 2) rounds down to zero. So instead you'd say:

(4 * 1) / 2

Which would evaluate to 2.

Generally when you're mixing multiplications and divisions you should try to do the divisions last since division reduces precision.

Because of this our position calculation becomes:

(total distance * current step) / (total steps)

You do that calculate the x and y positions separately this way for every frame. The idea is that (current step / total steps) represents the fraction of the total motion that you've reached. Multiplying that fraction by the total distance you need to move gives you the correct position for that frame. When current steps becomes equal to total steps then you've reached the final frame and you can explode the bullet. This technique is called "interpolation". The start and end are the "poles" and you're calculating an inter-pole position. This is a useful technique used in several ways in graphics programming.

You may want to take a Physics 100 course if you're planning on making games. A 100 course is usually pretty easy and basic physics deals a lot with this kind of calculation.

I'm not familiar with the C# syntax, but in your ctor could you test the value of 'Form1.Base.xfire' by printing it out or looking at it in the debugger and see if it's the correct value? I see you're using Visual Studio. VS has a very nice debugger. It can really help out with problems like this. You may want to spend a few minutes to familiarize yourself with its use.

Hope that helps!

Edited by Khatharr, 21 May 2012 - 09:28 PM.

void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

### #7ronnieb555  Members   -  Reputation: 112

Like
0Likes
Like

Posted 22 May 2012 - 08:07 AM

I know you are not familiar with C# but the distance part wouldn't work, I could add it as a Boolean but then I cannot use the .to_i at the end. Also I set the velocity in the main form (Form1) on line 176. When I added everything except the distance parts the bullet would draw but only flash on the screen above and below the cross hair.

I have posted this thread on the game programming and general programming threads so hopefully someone that knows how to use C# will help, thanks for your help though Khatharr I will let you know how I get on. If you have anymore suggestions please feel free to let me know.

### #8ronnieb555  Members   -  Reputation: 112

Like
0Likes
Like

Posted 23 May 2012 - 03:34 PM

If anyone still has any suggestions I would be so grateful for them.

I have attached the newest version with some changes over the original (the NPC missiles now spawn randomly and fall down, the cities blow up when hit, the npc missiles can now be destroyed by the player rockets.

Thanks for any help.

#### Attached Files

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

PARTNERS