The Bullet Problem

Started by Wonkyth, Tue 21/07/2009 11:34:02

Previous topic - Next topic

Wonkyth

This is done all the time, so there has to be a reasonably easy way around it, but I cant seem to figure it out.

OK, You have a Bullet. it's moving in a direction at a certain speed. that direction can be nearly any angle, in fact it can be any angle you can get between the mouse and a given point. if you're drawing the Bullet straight onto the screen using surface.Draw() type commands then obviously you need not just a point in Euclidean space , but a point on your Cartesian graph(screen).
Obviously you can just round the numbers in your Euclidean space to find the place where your pixel should go, but I cant seem to get it right.
The Bullet then needs to move, say, three pixels -every frame- along the line and be rounded again.
And then you need t...........


I'm lost, I need help.
If you can't understand what I'm trying to say, then maybe I can make more sense if I answer small questions.

thanks.
"But with a ninja on your face, you live longer!"

GuyAwesome

Is KhrisMUC's answer to a similar question from Mirek any good to you? (Second reply - first post linked for context.) It takes the bullet on a path from the player's location, to the mouse, and beyond until it leaves the screen (or hits something presumably). It uses a Character rather than surface.Draw, but it might give you a starting point for working out the coordinates needed.

Wonkyth

That certainly helps with the continuous line problem, but still uses a few things to be done, like the ajustment of the actual location.

Thanks anyway.
"But with a ninja on your face, you live longer!"

GuyAwesome

Unless I'm misreading KhrisMUC's code, it already calculates the new position for cBullet every frame in rep_exec (adjusting the location*) rather than working out the end point up front and moving there directly (a continuous line).
So, all you need to do is draw your bullet at the current position (bx, by by that code) then the next frame remove it and draw it at the new coords. I think you'd need to make a second DrawingSurface, containing a 'clean' copy of the background image, and restore to that to remove the bullet between frames. (Or, a Character would be easier ;).)


*Or did I misunderstand your post and this refers to the 'removing the bullet between frames' part (without which, yes, you would be left with a continuous line along the bullet's path)? If so Sorry, and hopefully the rest of my post is more use.

Wonkyth

No, sorry it was me, I misread what you said KhrisMUC's code did, and I hadn't yet found the time to look at the code myself.
I saw "and beyond until it leaves the screen" and thought that that was all.

I'm sorry I didn't search the forums, as is required before asking a common question, and as I said, this is done all the time. :-\


Anyway, Thanks. I'll look at the code now. ;)
"But with a ninja on your face, you live longer!"

Wonkyth

NEW QUESTION.

I'm trying to make a small line point towards a moving position, the line is anchored to another point that can move.
I've had a quick look(not on these forums) for a formula to produce a finite ray, but I didn't find any.
If the forum search does come up with something, then shoot me, but I have slow dial-up and Need to get to bed, so don't have time do do a search myself at the moment. If I ask now, maybe I'll have an answer tomorrow.....

"But with a ninja on your face, you live longer!"

GuyAwesome

#6
I think KhrisMUC's code can be adapted to give you that, as well. If I understand it properly, it works out the 'steps' the bullet has to take between the start point (e.g. player coords) and the end point (e.g. mouse coords) based on a certain speed (which I guess is distance covered per frame). Someone feel free to step in if I'm talking complete rubbish here...

If you only work out the first 'step' then, with the right 'speed' value, you should have your finite ray, right? (Assuming that means 'a line to a point that is part way along the line from A to B'.) I think you'd need to move part of KhrisMUC's 'shooting code' section into rep_exec, so the start and end points are recalculated each time the line is drawn. I'm not sure how having 2 constantly refreshing DrawingSurface features (the bullet and the line) will interact together. It might just be a case of getting the condition nesting right (so they don't refresh each other out of existance), or it might just look a little - well - wonky :)

Unless they're not two separate features, and the line IS the bullet? That would be easy enough to do, just draw the line as between the current position (bx,by) and the previous one (bx-dx, by-dy), or calculate 2 new floats for a shorter distance (similar to how dx and dy are calculated in the 'shooting code', but with a value less than speed. (Very little of which will make ANY sense, until you've looked over KhrisMUC's original code.)

Khris

Wonkyth, it might be easier for you if I explain what my code does:

At first, I calculate the two-dimensional vector from the start to the end point. The vector is then normalized, this is done by dividing both the x and y component by its length (a normalized vector has a length of 1).
Now I multiply both x and y with the desired length.
All those calculations are done using floats to maintain accuracy.

In the bullet code, I multiplied the direction vector with 5.0, then added this to the bullet's position every other frame.
You'll want to multiply it by the desired line length, then add it to the point of origin to get the end of the line.

TerranRich

All you guys who never thought you'd need math (especially) trigonometry in school... aren't you glad to have been proven wrong? I love math, and KhrisMUC's solution is elegant and very useful for any project with a similar need.
Status: Trying to come up with some ideas...

Wonkyth

Thanks guys.

I still cant get it to do what I want, but then due to the nature of my code I've had to retrofit it a little.
I'll tinker with it a bit more before I come back and say I broke it. :P

Nah, I should be able to get it working, and considering I've put a good many hours into trying to figure it out myself, it's just a matter of time now before I have the first working version for my first game. Not an PAK adventure game, but until I've got a story to work on and a few artists who understand the way I think, this should satisfy me... ;)

Thanks once again, everyone.
"But with a ninja on your face, you live longer!"

SMF spam blocked by CleanTalk