Author Topic: Newtonian missiles (includes code snippet)  (Read 2853 times)

0 Members and 1 Guest are viewing this topic.

Offline Aardwolf

  • 211
  • Posts: 16,384
Newtonian missiles (includes code snippet)
I haven't figured out how to insert this into the code myself, so I'm posting it here.

This is in 2D, but adding a third dimension should be a piece of cake.

Ah screw it, I'll do it in 3D:

Code: [Select]
      void Missile::steer(float timestep, Position3D *target)
      {
       
              float dx = target->x - x, dy = target->y - y, dz = target->z - z;                                   //Calculate vector from missile's position to target
              float distanceToTarget = sqrtf(dx * dx + dy * dy + dz * dz);                              //Calculate magnitude of that vector
              //Find the direction to accelerate in, in the moving reference frame of the target
              float dvx = dx * preferredSpeed / distanceToTarget - (vx - target->vx);
              float dvy = dy * preferredSpeed / distanceToTarget - (vy - target->vy);
              float dvz = dz * preferredSpeed / distanceToTarget - (vz - target->vz);
              float inv = timestep * acceleration / sqrtf(dvx * dvx + dvy * dvy + dvz * dvz);             //Used to normalize the acceleration vector
              //Normalize it
              vx += inv * dvx;
              vy += inv * dvy;
              vz += inv * dvz;
       
      }


The missile attempts to maintain constant speed (and possibly velocity) relative to the target's motion. So it will lead automatically, making it very hard to avoid. I would recommend using a weapons.tbl flag, such as "Newtonian Missile".

What this doesn't do is take into account the heading of the missile. Realistically, the missile would either have to be changing heading rapidly or have a bunch of small steering thrusters. However, since it's FreeSpace, I think we can get by with letting modders put fake steering thrusters on their missiles.

Also, this would go well with an $Accel entry for secondaries, to control acceleration used in the formula.

  

Offline Aardwolf

  • 211
  • Posts: 16,384
Re: Newtonian missiles (includes code snippet)
This missile will travel at a speed greater than it's preferredSpeed if chasing a target moving away from it, so collision detection might need to be aware of the missile flag or something.

 

Offline Polpolion

  • The sizzle, it thinks!
  • 211
Re: Newtonian missiles (includes code snippet)
Now get Newtonian ships. :yes:

 

Offline Galemp

  • Actual father of Samus
  • 212
  • Ask me about GORT!
    • Steam
    • User page on the FreeSpace Wiki
Re: Newtonian missiles (includes code snippet)
Better than a missile flag, make it a homing option for HEAT, ASPECT and JAVELIN.
"Anyone can do any amount of work, provided it isn't the work he's supposed to be doing at that moment." -- Robert Benchley

Members I've personally met: RedStreblo, Goober5000, Sandwich, Splinter, Su-tehp, Hippo, CP5670, Terran Emperor, Karajorma, Dekker, McCall, Admiral Wolf, mxlm, RedSniper, Stealth, Black Wolf...

 
Re: Newtonian missiles (includes code snippet)
Better than a missile flag, make it a homing option for HEAT, ASPECT and JAVELIN.
What's Javelin seeking like?
Fun while it lasted.

Then bitter.

 

Offline WMCoolmon

  • Purveyor of space crack
  • 213
Re: Newtonian missiles (includes code snippet)
That's not really a missile, though. It's more like a ball that follows a ship. Given what you've coded there, it doesn't have to worry about rotating for its main thrusters to face a certain direction; it can use its full acceleration in any direction instantaneously. It actually won't even turn with the current code.

The technology required to create a device that moves like that would probably surpass Newtonian principles, and it's not really what people are gonna think of when they think of 'Newtonian missiles', so I would call it something else. However...I like the idea. I'd like to see some more types of homing missiles than what's there already. Check out weapon_home for the homing code, and vm_vec_* for FS2_Open's built-in math library. Specifically what you're doing looks like it could use vm_vec_sub, vm_vec_dist, and vm_vec_normalize.
-C

 

Offline Aardwolf

  • 211
  • Posts: 16,384
Re: Newtonian missiles (includes code snippet)
Are those assembly optimized?

 

Offline Backslash

  • 29
  • Bring Our Might To Bear
Re: Newtonian missiles (includes code snippet)
Oooh, interesting.  This code might help me with the match speed/vector option I want to make for gliding.  And ...dare I say... given enough time and thought and work, maybe even AI glide use? :nervous:

WMC is right... this is no mere missile, it's a horrible Unescapable Drone of Doom :D
What's Javelin seeking like?
Javelin locks on to the engine subsystem, and doesn't lock if said subsystem is not in 'sight'.  Thus, you get heat seeking (or rather 'engine-seeking') missiles that still need a lock to fire.  Javelins are originally from Wing Commander.

 
Re: Newtonian missiles (includes code snippet)
What's Javelin seeking like?
Javelin locks on to the engine subsystem, and doesn't lock if said subsystem is not in 'sight'.  Thus, you get heat seeking (or rather 'engine-seeking') missiles that still need a lock to fire.  Javelins are originally from Wing Commander.

I knew it. I was thinking about the Javelin HS from wing commander as I read that :D
Fun while it lasted.

Then bitter.

 

Offline Nuke

  • Ka-Boom!
  • 212
  • Mutants Worship Me
Re: Newtonian missiles (includes code snippet)
Now get Newtonian ships. :yes:

all you'd really need is a "force newtonian features" which would turn on gliding permanently for that mission, and also force additive weapon velocity, and any other already implemented features that simulate newtonian physics.

the script approach doesn't seem to work very well. i think breaking of the collision detection only happens if your real speed is higher than your max speed. i learned this while testing the drone missiles i made for the early cockpit demos i put on youtube. i assume you can edit the tables and allow a max speed of like 1000, then hack the script to cap your velocity at 1000, then in theory scripted newtonian would work. i still need to add newtonian torque though, which is something im not entirely sure how to do. also atmospherics could be done. you could make it possible to do reentrys and launches on low gravity planets as well as smal scale orbital mechanics. you couldnt deal with an earthlike scenario because the thrust required for escape velocity would easily throw you to the mission bounds in a heartbeat once orbit is achieved and your out of the atmosphere's drag influence. could do things like supplying boosters that only have enough deltav for for orbit, then make the ships engines an order of magnitude less powerful. or by limiting fuel. of course you could also do planar gravity and atmo (for pure atmo missions), or spherical gravity and atmo (for missions involving orbital maneuvers).
I can no longer sit back and allow communist infiltration, communist indoctrination, communist subversion, and the international communist conspiracy to sap and impurify all of our precious bodily fluids.

Nuke's Scripting SVN

 

Offline Flaser

  • 210
  • man/fish warsie
Re: Newtonian missiles (includes code snippet)
I'm somewhat sure that to do the kind of orbital mechanics the engine would need to support a floating point space ergo instead fixed point, "integer" values for position you store this position data with an ever decreasing precision the further you get from your reference point. This float space then collapses into a fixpoint bubble around you to allow precise calculations like collision detection. This is how I-War does it AFAIK.

The problems arise since you have to keep track of a lot of things, and if your shots/missiles etc. leave your fixpoint bubble and interacts with stuff in floating point space very weird stuff can start happening.

....and this would only work for single player, for multiplayer or any scenario where simultaneous events have to occur with great accuracy you'd need to handle several "fixpoint" bubbles at once and do the the coordinate conversion between bubbles with great enough accuracy.

I have a vague recollection that someone already experimented with this, he (she?) was one of the new coders who appeared about 1/2 a year ago. I don't have enough time to go thread diving, but it was mentioned earlier that I'm sure of.
"I was going to become a speed dealer. If one stupid fairytale turns out to be total nonsense, what does the young man do? If you answered, “Wake up and face reality,” you don’t remember what it was like being a young man. You just go to the next entry in the catalogue of lies you can use to destroy your life." - John Dolan

 

Offline WMCoolmon

  • Purveyor of space crack
  • 213
Re: Newtonian missiles (includes code snippet)
Are those assembly optimized?

No, but they are used everywhere else in the code and some of them are defined as preprocessor defines (so there's no overhead from calling the "function").

I know someone was making things more OOP friendly, but I don't know if he's still working on it or what the progress of that is. Regardless, it's a good idea to use the vm_vec_* functions for consistency's sake, they'll be replaced if/when something better comes along.
-C

 

Offline Nuke

  • Ka-Boom!
  • 212
  • Mutants Worship Me
Re: Newtonian missiles (includes code snippet)
to do full scale orbital mechanics, yes, theres not enough space to do a whole solar system, but a small planet and some moons, or asteroids, can be simulated.

yea there was a thread somone started claiming they wanted to do a large space modification to the engine complete with procedural planetary terrain and alot of cool features. but it turns out the freespace engine would need a major rewrite for it to work.  essentially all vectors would contain, aside from the 3 floating point numbers, a set of 3 long integers. each integer vector would represent a sector of space about 100,000 or so meters across. you would to 2 passes of rendering, first rendering all non-local sectors as a background, then local sectors would be rendered. large objects in all sectors would be xformed with integer precision (sence decimal sizes there would be indistinguishable at the ranges associated) and rendered first. objects in the current sector as well as adjacent sectors (if youre near an edge) would xformed (with decimal precision) and be rendered. or something like that.
I can no longer sit back and allow communist infiltration, communist indoctrination, communist subversion, and the international communist conspiracy to sap and impurify all of our precious bodily fluids.

Nuke's Scripting SVN

 
Re: Newtonian missiles (includes code snippet)
That might have been razorjack (tried to implement a planetary engine) but said it was impossible, or, at best, nearly impossible.