Login
User Name:

Password:



Register
Forgot your password?
Vote for Us!
parse description bug
Dec 15, 2017, 10:08 pm
By Remcon
Couple bugs
Dec 12, 2017, 5:42 pm
By Remcon
Bug in disarm( )
Nov 12, 2017, 6:54 pm
By GatewaySysop
Bug in will_fall( )
Oct 23, 2017, 1:35 am
By GatewaySysop
Bug in do_zap( ), do_brandish( )
Oct 18, 2017, 1:52 pm
By GatewaySysop
LOP 1.45
Author: Remcon
Submitted by: Remcon
LOP Heroes Edition
Author: Vladaar
Submitted by: Vladaar
Heroes sound extras
Author: Vladaar
Submitted by: Vladaar
6Dragons 4.3
Author: Vladaar
Submitted by: Vladaar
Memwatch
Author: Johan Lindh
Submitted by: Vladaar
Users Online
CommonCrawl, DotBot, Yahoo!, Bing

Members: 0
Guests: 20
Stats
Files
Topics
Posts
Members
Newest Member
477
3,706
19,240
608
LAntorcha
Today's Birthdays
There are no member birthdays today.
Related Links
» SmaugMuds.org » Codebases » SWR FUSS » Incorrect vector calculations...
Forum Rules | Mark all | Recent Posts

Incorrect vector calculations in the space code
< Newer Topic :: Older Topic >

Pages:<< prev 1, 2, 3, 4 next >>
Post is unread #1 Dec 31, 2006, 8:40 am
Go to the top of the page
Go to the bottom of the page

Caius
Magician
GroupMembers
Posts132
JoinedJan 29, 2006

After working on a pet project where I used a lot of vector math, I took a second look at the the space code in SWR and realized the calculations are ranging from imprecise to outright wrong. Example:
if( abs( target->vx - ship->vx ) <= 1000
    && abs( target->vy - ship->vy ) <= 1000
    && abs( target->vz - ship->vz ) <= 1000 )
{
    // Some code here ...
}

SWR is full of ifchecks like this, and the intent is clearly to find out if the distance between two ships are 1000 or less. However, this is not the correct way to do it. The result will actually be more than 1000. For better precision we want the following. First I'm replacing the vx, vy and vz variables with a structure like this:
typedef struct  vector3       Vector3;

struct vector3
{
    float x, y, z;
};

For reasons that will soon become apparent I also use this structure for hx/y/z, and for the positions of planets and starsystems as well. So after putting 'Vector3 *pos;' inside ship_data we can access the values like this: ship->pos->x (remember to allocate memory before accessing!). Now, next step is to make a function that (correctly) returns the distance between two vectors:
float vector_distance( Vector3 *a, Vector3 *b )
{
    return sqrt((a->x - b->x) * (a->x - b->x)
                 +(a->y - b->y) * (a->y - b->y)
                 +(a->z - b->z) * (a->z - b->z));
}

So we'll rewrite the above ifcheck as follows:
if( vector_distance( ship->pos, target->pos ) <= 1000 )
{
    // Some code here...
}

Not only is it correct, it looks better and is easier to read as well! Now, the point of using the Vector3 structure is that now we can use the vector_distance() function to get the distance between anything in space. Examples:
// distance between a planet and a ship
dist = vector_distance(planet->pos, ship->pos);

// though in SWRFuss 1.2 it'll be something like this (assuming p1 is a Vector3)
dist = vector_distance(ship->starsystem->p1, ship->pos);

// distance between two starsystems
dist = vector_distance(starsystem1->pos, starsystem2->pos);

// or even the distance between two ship in different systems:
dist = vector_distance(ship->starsystem->pos, target->starsystem->pos);

// between a missile and a ship:
dist = vector_distance(ship->pos, missile->pos);

// etc, etc...

Unfortunately distances aren't the only thing handled either clumsily or incorrectly. Which brings me to the point of this post. Now, since I've already worked with vectors for a little while, I could go through the code and make it behave as expected. Normally it'd make sense to write a snippet or something, but in this case the sheer number of little changes would make a snippet unwieldy. Therefore: If the community is interested in an SWR codebase (and SWFotE, I suppose) that handles vector calculations correctly, I can work on a stock SWRFuss1.2 and supply a diff file of the changes I've done (and properly commented of course).

So, are you interested in this? And Samson, would you put it into the official release? I'm not sure I'd do this if it didn't get put into good use.
       
Post is unread #2 Dec 31, 2006, 8:57 am
Go to the top of the page
Go to the bottom of the page

Samson
Black Hand
GroupAdministrators
Posts3,639
JoinedJan 1, 2002

Yes, I would definitely be willing to accept a patch that corrects the vector calculations in the codebase. A diff against the current 1.2 package would be perfect. What you've teased us with so far looks like it will correct an entire class of problems most of us probably didn't know exists :)
       
Post is unread #3 Dec 31, 2006, 9:07 am
Go to the top of the page
Go to the bottom of the page

Caius
Magician
GroupMembers
Posts132
JoinedJan 29, 2006

Great stuff. All right, I'll get down to it shortly. I'll make one for SWR and one for SWFotE. But right now I'm off to celebrate the new year.

Happy New Year everyone!
       
Post is unread #4 Jan 2, 2007, 3:23 pm
Go to the top of the page
Go to the bottom of the page

Caius
Magician
GroupMembers
Posts132
JoinedJan 29, 2006

You sure you want it against the 1.2 package? There seems to be a few issues with it, at least on my system. Like placing hundreds of mobs into limbo, and load_ship_file() seems to crash every now and then.

Nevertheless, I'm almost done with the SWR version. I haven't done much except correcting the distance calculations. I'm a little reluctant to do certain changes since the point of this little project is to correct errors, not to change gameplay. But I'll also fix the handling of direction.
       
Post is unread #5 Jan 2, 2007, 10:43 pm
Go to the top of the page
Go to the bottom of the page

Samson
Black Hand
GroupAdministrators
Posts3,639
JoinedJan 1, 2002

Well if there are other problems with 1.2 that you're encountering, post about them. Having hundreds of mobs in limbo is not an intended result. And neither are crashes with load_ship_file. It's just that the SWR bases don't get the same level of attention since most of us are using something from the main Smaug branch.
       
Post is unread #6 Jan 4, 2007, 1:23 am   Last edited Nov 25, 2007, 6:03 pm by Samson
Go to the top of the page
Go to the bottom of the page

Caius
Magician
GroupMembers
Posts132
JoinedJan 29, 2006

From what I've seen, all the mobs placed in limbo are from the coruscant_streets area file. I'll post a proper post on it when I've had time to look into it further.

It's obviously related to this: http://www.smaugmuds.org/index.php?a=topic&t=2378

And this: http://www.smaugmuds.org/index.php?a=topic&t=2307
       
Post is unread #7 Jan 4, 2007, 2:18 am
Go to the top of the page
Go to the bottom of the page

Caius
Magician
GroupMembers
Posts132
JoinedJan 29, 2006

I put in some debugging log strings here and there, and I'm certain of the following: It's got something to do with moving characters into virtual rooms. This explains why it's only coruscant_streets, since (afaik) that's the only area in SWR that uses virtual rooms.
       
Post is unread #8 Dec 11, 2008, 11:11 am   Last edited Dec 11, 2008, 11:12 am by Caius
Go to the top of the page
Go to the bottom of the page

Caius
Magician
GroupMembers
Posts132
JoinedJan 29, 2006

I'm necroing this thread. I never got around to finishing this. But now (2 years later!) I'm more or less done. Everything I've tested so far works as expected. I still have some things so test, like missiles.

Anyway. If you want my code, I'll donate it. If not, I'll just put it in my drawer.

Edit: Since this thread really discusses two things, I should clarify that it's the vector math I'm talking about here.
       
Post is unread #9 Dec 11, 2008, 11:27 am
Go to the top of the page
Go to the bottom of the page

Kayle
Off the Edge of the Map
GroupAdministrators
Posts1,195
JoinedMar 21, 2006

I'll have to get a community response on it (that could take forever :P) but I'd be more than willing to use your vector stuff to fix the math behind the space system.
       
Post is unread #10 Dec 11, 2008, 11:32 am
Go to the top of the page
Go to the bottom of the page

Caius
Magician
GroupMembers
Posts132
JoinedJan 29, 2006

As I told you earlier these changes can cause incompatibilities with existing snippets. I looked briefly over the space snippets over at MudBytes, and found that the asteroids, slave circuits and tractor beam snippets will not install without modifications. The modifications are mainly (perhaps only) in calculations of distances.
       
Post is unread #11 Dec 11, 2008, 12:21 pm
Go to the top of the page
Go to the bottom of the page

Kayle
Off the Edge of the Map
GroupAdministrators
Posts1,195
JoinedMar 21, 2006

Assuming enough people want to see the math in the system fixed, we could fix the snippets and, with permission, re-release them as a FUSS style snippet.
       
Post is unread #12 Oct 14, 2009, 10:26 am
Go to the top of the page
Go to the bottom of the page

dbna2
Sorcerer
GroupMembers
Posts600
JoinedDec 2, 2008

Was this fix ever Posted?
       
Post is unread #13 Oct 14, 2009, 10:28 am
Go to the top of the page
Go to the bottom of the page

Kayle
Off the Edge of the Map
GroupAdministrators
Posts1,195
JoinedMar 21, 2006

It's not really a fix. Because it breaks compatibility with snippets. So no, it was never posted.
       
Post is unread #14 Oct 14, 2009, 10:41 am
Go to the top of the page
Go to the bottom of the page

Caius
Magician
GroupMembers
Posts132
JoinedJan 29, 2006

I don't remember if I ever provided a link to the code. I seem to remember I did, but apparently not in this thread...

Anyway, for anyone interested or curious: Vector stuff

As for documentation there is little. There are some helpful (?) comments in the code, and then there is the first post in this thread. That's it. There are two parts of the code. The vector3.c/h files contains the basic math, while vector3_aux.c/h contains some examples of higher level functions that may be useful.

This is not a snippet, it's not plug and play.
       
Post is unread #15 Oct 14, 2009, 11:21 am
Go to the top of the page
Go to the bottom of the page

Keirath
Magician
GroupMembers
Posts144
JoinedJan 24, 2008

Is this going to make its way into an SWRFUSS release?
       
Post is unread #16 Oct 14, 2009, 11:22 am
Go to the top of the page
Go to the bottom of the page

Caius
Magician
GroupMembers
Posts132
JoinedJan 29, 2006


Keirath said:

Is this going to make its way into an SWRFUSS release?

See Kayle's last post. In other words, no.
       
Post is unread #17 Oct 14, 2009, 11:29 am
Go to the top of the page
Go to the bottom of the page

Keirath
Magician
GroupMembers
Posts144
JoinedJan 24, 2008

Heh alright. I'm going to see if I can figure out how to do this myself. Not good with these math functions. Well that and the space system in general.
       
Post is unread #18 Oct 14, 2009, 12:10 pm
Go to the top of the page
Go to the bottom of the page

David Haley
Sorcerer
GroupMembers
Posts903
JoinedJan 29, 2007

Is preserving compatibility with snippets really worth leaving bugs in?

It seems to me that it might be easier to add notes to the snippets saying that they need adjustment, and then maybe fixing the most popular snippets (or outright incorporating them into the base, like the SmaugFUSS project has done for several).
       
Post is unread #19 Oct 14, 2009, 12:22 pm
Go to the top of the page
Go to the bottom of the page

Caius
Magician
GroupMembers
Posts132
JoinedJan 29, 2006


David Haley said:

Is preserving compatibility with snippets really worth leaving bugs in?

It seems to me that it might be easier to add notes to the snippets saying that they need adjustment, and then maybe fixing the most popular snippets (or outright incorporating them into the base, like the SmaugFUSS project has done for several).

I'm inclined to agree.
       
Post is unread #20 Oct 14, 2009, 12:46 pm   Last edited Oct 14, 2009, 12:47 pm by Keirath
Go to the top of the page
Go to the bottom of the page

Keirath
Magician
GroupMembers
Posts144
JoinedJan 24, 2008

I'd definitely agree. I would like to see space work correctly. Though, I know many of my snippets will need work.
       
Pages:<< prev 1, 2, 3, 4 next >>