Velocity Reviews > missiles with parabolic paths for Magnant (C language).

# missiles with parabolic paths for Magnant (C language).

mohydine
Guest
Posts: n/a

 09-17-2003

Hi,

I am trying to implement some parabolic trajectories for the missiles
used in the game Magnant (http://www.insectwar.com) . It would enable
Ants to shoot more realistic arrows to other ants.

Right now ants are shooting point to point arrows, that follow along a
straight path (a line).

I am wondering if anybody knows a good algorithm or a good formula for
implementing this?

The GPL code is currently written like this ( stratagus engine v1.18 ):

I can send the complete .C if necessary.

int dx;

int dy;

int xstep;

int ystep;

int i;

if( !(missile->State&1) ) {

// initialize

dy=missile->DY-missile->Y;

ystep=1;

if( dy<0 ) {

dy=-dy;

ystep=-1;

}

dx=missile->DX-missile->X;

xstep=1;

if( dx<0 ) {

dx=-dx;

xstep=-1;

}

// FIXME: could be better written

if( missile->Type->Class == MissileClassWhirlwind

|| missile->Type->Class == MissileClassFlameShield ) {

// must not call MissileNewHeading nor frame change

} else if( missile->Type->Class == MissileClassBlizzard ) {

missile->SpriteFrame = 0;

} else if( missile->Type->Class == MissileClassPointToPoint3Bounces ) {

missile->DX-=xstep*TileSizeX/2;

missile->DY-=ystep*TileSizeY/2;

} else {

}

if( dy==0 ) { // horizontal line

if( dx==0 ) {

return 1;

}

} else if( dx==0 ) { // vertical line

} else if( dx<dy ) { // step in vertical direction

missile->D=dy-1;

dx+=dx;

dy+=dy;

} else if( dx>dy ) { // step in horizontal direction

missile->D=dx-1;

dx+=dx;

dy+=dy;

}

missile->Dx=dx;

missile->Dy=dy;

missile->Xstep=xstep;

missile->Ystep=ystep;

++missile->State;

DebugLevel3Fn("Init: %d,%d, %d,%d, =%d\n"

_C_ dx _C_ dy _C_ xstep _C_ ystep _C_ missile->D);

return 0;

} else {

// on the way

dx=missile->Dx;

dy=missile->Dy;

xstep=missile->Xstep;

ystep=missile->Ystep;

}

//

// Move missile

//

if( dy==0 ) { // horizontal line

for( i=0; i<missile->Type->Speed; ++i ) {

if( missile->X==missile->DX ) {

return 1;

}

missile->X+=xstep;

}

return 0;

}

if( dx==0 ) { // vertical line

for( i=0; i<missile->Type->Speed; ++i ) {

if( missile->Y==missile->DY ) {

return 1;

}

missile->Y+=ystep;

}

return 0;

}

if( dx<dy ) { // step in vertical direction

for( i=0; i<missile->Type->Speed; ++i ) {

if( missile->Y==missile->DY ) {

return 1;

}

missile->Y+=ystep;

missile->D-=dx;

if( missile->D<0 ) {

missile->D+=dy;

missile->X+=xstep;

}

}

return 0;

}

if( dx>dy ) { // step in horizontal direction

for( i=0; i<missile->Type->Speed; ++i ) {

if( missile->X==missile->DX ) {

return 1;

}

missile->X+=xstep;

missile->D-=dy;

if( missile->D<0 ) {

missile->D+=dx;

missile->Y+=ystep;

}

}

return 0;

}

// diagonal line

for( i=0; i<missile->Type->Speed; ++i ) {

if( missile->Y==missile->DY ) {

return 1;

}

missile->X+=xstep;

missile->Y+=ystep;

}

return 0;

Mohydine

--
Posted via http://dbforums.com

Joona I Palaste
Guest
Posts: n/a

 09-17-2003
mohydine <(E-Mail Removed)> scribbled the following:
> Hi,

> I am trying to implement some parabolic trajectories for the missiles
> used in the game Magnant (http://www.insectwar.com) . It would enable
> Ants to shoot more realistic arrows to other ants.

> Right now ants are shooting point to point arrows, that follow along a
> straight path (a line).

> I am wondering if anybody knows a good algorithm or a good formula for
> implementing this?

> The GPL code is currently written like this ( stratagus engine v1.18 ):

(snip code)

The code you posted was in a very unreadable format. Please don't
insert gratuitous blank lines between source code lines (there seemed
to be 2 blank lines between *every* line of your code), and please,
level.

--
/-- Joona Palaste ((E-Mail Removed)) ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste W++ B OP+ |
\----------------------------------------- Finland rules! ------------/
"You can pick your friends, you can pick your nose, but you can't pick your
relatives."

Richard Heathfield
Guest
Posts: n/a

 09-17-2003
mohydine wrote:

>
> Hi,
>
>
>
> I am trying to implement some parabolic trajectories for the missiles
> used in the game Magnant (http://www.insectwar.com) . It would enable
> Ants to shoot more realistic arrows to other ants.
>
>
>
> Right now ants are shooting point to point arrows, that follow along a
> straight path (a line).
>
>
>
> I am wondering if anybody knows a good algorithm or a good formula for
> implementing this?

Let the arrow be fired with an initial velocity V, and an initial angle of
alpha radians from the horizontal, in a gravitational field accelerating
falling bodies at g.

Its initial upward velocity is V sin alpha, and its sideways velocity V cos
alpha. If you ignore air resistance, it'll keep going sideways until it
hits something. Its upward velocity decreases by g each second.

Take it from there.

--
Richard Heathfield : http://www.velocityreviews.com/forums/(E-Mail Removed)
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton

Bertrand Mollinier Toublet
Guest
Posts: n/a

 09-17-2003
Richard Heathfield wrote:
> mohydine wrote:
>>Hi,
>>
>>I am trying to implement some parabolic trajectories for the missiles
>>used in the game Magnant (http://www.insectwar.com) . It would enable
>>Ants to shoot more realistic arrows to other ants.
>>
>>Right now ants are shooting point to point arrows, that follow along a
>>straight path (a line).
>>
>>I am wondering if anybody knows a good algorithm or a good formula for
>>implementing this?

>
> Let the arrow be fired with an initial velocity V, and an initial angle of
> alpha radians from the horizontal, in a gravitational field accelerating
> falling bodies at g.
>
> Its initial upward velocity is V sin alpha, and its sideways velocity V cos
> alpha. If you ignore air resistance, it'll keep going sideways until it
> hits something. Its upward velocity decreases by g each second.
>
> Take it from there.
>

Richard,

I'd like to allow myself to quote you: "This is a wonderful answer. It's
off-topic, it's incorrect, and it doesn't answer the question."

doesn't really answer the question (in that, IMHO, it is not all that
helpful for an algorithm). I guess we'll have to settle to qualify it as

If you liked physics as much as I did, I can understand why you could
not resist the urge to show off what you knew, but don't you think it
deserved to at least be flagged as OT, let alone cross-posted and fu2 to
sci.physics.shooting-arrows or something ?

Ah well... don't mind me...
--
Bertrand Mollinier Toublet
Currently looking for employment in the San Francisco Bay Area
http://www.bmt.dnsalias.org/employment

Mark McIntyre
Guest
Posts: n/a

 09-17-2003
On Wed, 17 Sep 2003 16:38:36 -0400, in comp.lang.c , mohydine
<(E-Mail Removed)> wrote:

>
>Hi,
>
>
>
>I am trying to implement some parabolic trajectories for the missiles
>used in the game Magnant (http://www.insectwar.com) . It would enable
>Ants to shoot more realistic arrows to other ants.
>
>I am wondering if anybody knows a good algorithm or a good formula for
>implementing this?
>

Virtually any A level maths student or physical science undergrad
wouild know an algo for this. In fact you could could look up the
formula for the parabola in pretty much any textbook. This is not a C
question.

When you're posting from web-based sources, you need to remember that
they almost certainly screw up your line endings. In your case, the
cut-paste resulted in every CR being translated into CRLF and every LF
_also_ being translated into CRLF.

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>

Richard Heathfield
Guest
Posts: n/a

 09-17-2003
Bertrand Mollinier Toublet wrote:

<extremely elementary projectiles stuff snipped>

> Richard,
>
> I'd like to allow myself to quote you: "This is a wonderful answer. It's
> off-topic, it's incorrect, and it doesn't answer the question."
>

That's a relief.

> It is just off-topic and
> doesn't really answer the question (in that, IMHO, it is not all that

It was intended to set the OP thinking. I /could/ have scribbled out the C
code for him, but that would have been "doing the homework", which I was
loathe to do. I was hoping to get him to realise that he's gonna have to

> I guess we'll have to settle to qualify it as
> only "good" instead of wonderful.

<grin>

>
> If you liked physics as much as I did, I can understand why you could
> not resist the urge to show off what you knew,

Oh, but I /did/ resist. I was all set to show him how to calculate time of
flight, height at a given time, maximising range whilst taking air
resistance into account, and heaven knows what else; but I managed to
prevent myself from doing this, on the grounds that it would teach him

> but don't you think it
> deserved to at least be flagged as OT, let alone cross-posted and fu2 to
> sci.physics.shooting-arrows or something ?

It didn't occur to me, to be honest with you. Sorry about that.

--
Richard Heathfield : (E-Mail Removed)
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton

nrk
Guest
Posts: n/a

 09-17-2003
Bertrand Mollinier Toublet wrote:

<snip>

>
> I'd like to allow myself to quote you: "This is a wonderful answer. It's
> off-topic, it's incorrect, and it doesn't answer the question."
>
> (Un)fortunately, your answer is correct enough. It is just off-topic and
> doesn't really answer the question (in that, IMHO, it is not all that
> helpful for an algorithm). I guess we'll have to settle to qualify it as
> only "good" instead of wonderful.
>
> If you liked physics as much as I did, I can understand why you could
> not resist the urge to show off what you knew, but don't you think it
> deserved to at least be flagged as OT, let alone cross-posted and fu2 to
> sci.physics.shooting-arrows or something ?
>
> Ah well... don't mind me...

Actually, it is a pretty reasonable answer (OT, but reasonable). All the OP
needs to do is recollect some high-school physics that says
s = ut + 0.5 at^2, s being distance, u being initial velocity, a being
acceleration and t being time, to simulate the x and y co-ords. of the
projectile.

-nrk.

mohydine
Guest
Posts: n/a

 09-18-2003

HI all,

First thanks for your answers. Unfortunately, this is not helping me a
lot.

As you suggested, I used simple parabolic formula, w/o using t, but
using a simple y=f(x).

x=x+step;

so y=yo+Cst*Sqrt[(x-xm)^2-(xo-xm)^2];

where (xo,yo) is starting point, xm is such that dy(xm) /dx=0.

I can get xm from

Xd=missile->DX; // impact of the missile X

Yd=missile->DY; // impact of the missile Y

Xo=missile->SourceX; //Source missile X

Yo=missile->SourceY; //Source missile Y

then

Xm=(-1/Cst*(Yd-Yo)*(Yd-Yo)+Xd*Xd-Xo*Xo)/(2*(Xd-Xo));

Since I know which parabola I am going to use(3 parameters known)

it works fine if yD(destination(impact of missible))= y0, but I can't
have it working when yd<yo. Even if I know my 3 parameters formula

any idea? Mathematically, I can't find the problem. So that is why I am
wondering if this has something to do with the C implementation or
something else.

Sorry for posting so much code.

Mohydine

--
Posted via http://dbforums.com

Rajasekar Ramakrishnan
Guest
Posts: n/a

 09-18-2003
hi,
i am actually new to c.l.c
and i happen to read lots of mails in this newsgroup

and the very bad part of this newsgroup
is that ppl start to hurt a newbie, if the person asks for a very basic doubt

and that too, joona i palaste, if i could see ur mails i could only
know that you do not provide solutions
but mostly ask people to not send basic doubts to this comp.lang.c

i think the news readers need to be more professional, not all of them

please try to help a novice or if not, keep mum , so that he is not
discouraged

rajasekar.

Joona I Palaste wrote:

> mohydine <(E-Mail Removed)> scribbled the following:
> > Hi,

>
> > I am trying to implement some parabolic trajectories for the missiles
> > used in the game Magnant (http://www.insectwar.com) . It would enable
> > Ants to shoot more realistic arrows to other ants.

>
> > Right now ants are shooting point to point arrows, that follow along a
> > straight path (a line).

>
> > I am wondering if anybody knows a good algorithm or a good formula for
> > implementing this?

>
> > The GPL code is currently written like this ( stratagus engine v1.18 ):

>
> (snip code)
>
> The code you posted was in a very unreadable format. Please don't
> insert gratuitous blank lines between source code lines (there seemed
> to be 2 blank lines between *every* line of your code), and please,
> level.
>
> --
> /-- Joona Palaste ((E-Mail Removed)) ---------------------------\
> | Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
> | http://www.helsinki.fi/~palaste W++ B OP+ |
> \----------------------------------------- Finland rules! ------------/
> "You can pick your friends, you can pick your nose, but you can't pick your
> relatives."

Joona I Palaste
Guest
Posts: n/a

 09-18-2003
Rajasekar Ramakrishnan <(E-Mail Removed)> scribbled the following:
> This is a multi-part message in MIME format.
> --------------8ABFF92D41955E546EDA90A4
> Content-Type: text/plain; charset=us-ascii
> Content-Transfer-Encoding: 7bit

Don't do that.

> hi,
> i am actually new to c.l.c
> and i happen to read lots of mails in this newsgroup

> and the very bad part of this newsgroup
> is that ppl start to hurt a newbie, if the person asks for a very basic doubt

Expressing criticism is not hurting. Did you see me insult or flame the
OP anywhere?

> and that too, joona i palaste, if i could see ur mails i could only
> know that you do not provide solutions
> but mostly ask people to not send basic doubts to this comp.lang.c

Did you see me ask the OP not to send basic doubts anywhere? All I
said that he should reformat the code better.

> i think the news readers need to be more professional, not all of them

> please try to help a novice or if not, keep mum , so that he is not
> discouraged

Which would mean that if the OP's code was bad, we shouldn't have to
tell him that, because it would hurt his feelings and scar him
emotionally. Thus he wouldn't know that his code could be better, and
go on writing bad code. That doesn't fit in with my vision of the world.

You seem to be mistaking telling someone "you are not the greatest
coder to walk this Earth (yet)" with telling him "you are a pathetic
loser". I was doing the former, not the latter.

(v-card snipped)

--
/-- Joona Palaste ((E-Mail Removed)) ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste W++ B OP+ |
\----------------------------------------- Finland rules! ------------/
"You could take his life and..."
- Mirja Tolsa