Bullet animation

Started by
12 comments, last by BCullis 11 years, 4 months ago

1) It doesn't do the same thing, it calculates a starting point and a predicted end point. His method calculates a direction and will interpolate it.
???
There's no interpolation of any kind in Xaer0's code. The only thing it does is directly calculate the end point. The same end point whose components are curiously named "finalX" and "nextY" in your code.
Advertisement

[quote name='KnolanCross' timestamp='1355319181' post='5009816']
4) The method Xaer0 suggested may move the bullet several pixels at each time the formula is executed, which may lead the bullet to bypass something it shouldn't (such as a wall). The method that I suggested will give the bullet trajectory, pixel by pixel.


This might be a good place to point out that if you do collision detection in "pixel space", you are doing it wrong and should have a close look at why proper collision detection is very different from "checking for intersections every frame". After that you will realize why brute forcing collision detection by moving stuff in a painful pixel by pixel fashion to check every single one along the way is getting you down voted.

Sorry, but every time someone is throwing trigonometry at trivial problems it's making me cry.
[/quote]

Sorry I wasn't clear, but I never said he should use pixel by pixel approach on colision, just that this will give him the trajectory that he could use to calculate the collision in a simple way (not using vectors, which is way more complex).


[quote name='KnolanCross' timestamp='1355319181' post='5009816']
1) It doesn't do the same thing, it calculates a starting point and a predicted end point. His method calculates a direction and will interpolate it.
???
There's no interpolation of any kind in Xaer0's code. The only thing it does is directly calculate the end point. The same end point whose components are curiously named "finalX" and "nextY" in your code.
[/quote]

Every dt it will move the bullet, this will interpolate its trajectory.
I took it from a code I use on a game where nextX and nextY was used, it was just adapted to fill the post.

PS: Love how people negativate others without posting a reason around here.

Currently working on a scene editor for ORX (http://orx-project.org), using kivy (http://kivy.org).


Actually, depending on his needs, it may be better to use a discrete pixel by pixel method.

You can calculate the angle of the click and the mouse by using:
[source lang="cpp"]angle = atan2f(clickedPosition.y - currentPos.y, clickedPosition.x - currentPos.x);[/source]

Then use the angle to calculate a "final" position, given a maximum distance that a bullet shall move:
[source lang="cpp"]finalX = currentPos.fX + (cos(angle) * maximumMovement);
nextY = currentPos.fY + (sin(angle) * maximumMovement);

[/source]
And finally use the bresenham algorithm to find all the pixels the buttlet would move by:

[source lang="cpp"]void bresenham(unsigned x0, unsigned y0, unsigned x1, unsigned y1){


int dx = absolute(x1-x0);
int dy = absolute(y1-y0);

int sx, sy;
if (x0 < x1){
sx = 1;
}
else{
sx = -1;
}

if (y0 < y1){
sy = 1;
}
else{
sy = -1;
}

int err = dx-dy;
while (!(x0 == x1 && y0 == y1)){

int errTimesTwo = 2 * err;
if (errTimesTwo > -dy){
err -= dy;
x0 = x0 + sx;
}

if (errTimesTwo < dx){
err += dx;
y0 += sy;
}

}

}
[/source]
Of course you would need some adjusts if you want the bullets to be dodgeable/renderable projectiles.
The advantages of this method is that it is really easy to find the bullet path and check for collisions if you need it.

Hope this helps biggrin.png

[quote name='KnolanCross']
PS: Love how people negativate others without posting a reason around here.
[/quote]

since you asked, I downvoted your first post because it's clearly above what the OP is attempting to do, and is imo quite a terrible method for doing collision checks. Xaer0 provided a great explanation about what to do, and is done in linear algebra.

you on the other hand used inconsistent naming convention(finalX, and nextY), didn't explain anything going on in the code you provided(you threw out a name of the algorithm, said what it does, but didn't actually explain how the algorithm achieves what it does, nor how to use it for anything.), that is why i down-voted you.
Check out https://www.facebook.com/LiquidGames for some great games made by me on the Playstation Mobile market.

(snip explanation) ... that is why i down-voted you.

Knolan, you've been around here for a few months, so don't think I'm trying to be patronizing by explaining this. As a forum that does a lot of teaching to beginners and hobbyists in comparison to discourse between experienced programmers, we have to find a balance when sharing information. A negative vote covers areas that include "this is going to confuse the person asking for help, or over-complicates the provided solution scenario".

Just being skilled in a craft doesn't make for a good teacher, it's as important to know how (and how much) to share as knowing what the solution is. While you may be partial to your solution, just like anyone else, is it really, objectively, as easy to implement and straightforward to understand to someone asking for "any" math solutions to a common linear algebra problem? If the answer is honestly "no", then at least indicate as such ("this is another less common approach") and make sure to explain it.

(I minored in math with my undergrad and I have no idea who Bresenham is, it's a likely bet the OP doesn't either, so talking through the algorithm a little would have gone a long way)

Hazard Pay :: FPS/RTS in SharpDX (gathering dust, retained for... historical purposes)
DeviantArt :: Because right-brain needs love too (also pretty neglected these days)

This topic is closed to new replies.

Advertisement