PDA

Click to See Complete Forum and Search --> : Problems with 3D calculations

thekingrules
05-27-2002, 09:01 AM
How can I represent the position of a point at a point (x,y,z) in flash if the viewer is at a different point looking towards (0,0,0) (or elsewhere). I want to have an number of points and be able to move round them.

I currently use the following function where an multi dimensional array contains the co-ordinates of a number of points
e.g input = new Array(new Array(0, 0, 0), new Array(100, 0, 0), new Array(100, 0, 100), new Array(0, 0, 100));
and is the input, the output is their place on the screen and obspoint is the position of the observer:

obspoint = new Array(0, 200, 0);
obstarget = new Array(0, 0, 0);

function threeDtotwoD(input) {
n = input.length;
output = input;
for (i=0; i<n; i++) {
dx = obspoint-input[i];
dy = obspoint-input[i];
dz = obspoint-input[i];
if (dy == 0 || Math.atan(dz/dy) == NaN) {
output[i] = 0;
} else {
output[i] = 275+(100*Math.atan(dz/dy));
}
if (dy == 0 || Math.atan(dx/dy) == NaN) {
output[i] = 0;
} else {
output[i] = 200+(100*Math.atan(dx/dy));
}
}
return output;
}

I think that I am doing it wrong as it sometimes goves funny results. Are there any better ways to do this.

Sorry if the above is a bit hard to understand, I'm in a hurry

Smilev
05-30-2002, 01:32 AM
what Im using is 2 coodinates x and y for the all the objects ...
then the observer calculates on every frame the radius and the angle to the objects where its position is the beginning of the subcoordinate system.. so _x is the shortest distance between the point and the radius and _xscale/_yscale depends on the radius something like 100- radius*0,3 ... If that makes sense :D

here is the result:

http://www.geocities.com/stanimir.geo/flash/3d.html

here is the code:
Movieclip.prototype.position = function () {
r = Math.sqrt((x-_root.obs.x)*(x-_root.obs.x)+(y-_root.obs.y)*(y-_root.obs.y));
B = Math.atan((y-_root.obs.y)/(x-_root.obs.x1));
var A = Math.PI/180*_root.obs.direction;
var C = B-A;
_xscale = 100-r*0.3;
if (_xscale<0) {
_xscale = 0;
}
_yscale = _xscale;
var dist = Math.sin(C)*r;
if (_root.obs.x>x) {
_x = 580-(_root.obs._x-_xscale*dist/5);// where 580 is the width of your movie
} else {
_x = (_root.obs._x-_xscale*dist/5);
}
_y = 235;
}
==================

And then to be visible only if its in front of the observer

Movieclip.prototype.visibility = function () {
var Bdeg = (B*180)/Math.PI;
if (_root.obs.x>x) {
var Bdeg = 180+Bdeg;
} else if (_root.obs.y>y) {
var Bdeg = 360+Bdeg;
}
var viewRight = _root.obs.direction-45;
var viewLeft = _root.obs.direction+45;
if (viewRight<0 && _root.obs.y>y) {
var viewRight = 315;
var viewLeft = 405;
}
if (Bdeg>=viewRight and Bdeg<=viewLeft) {
_visible = true;
} else {
_visible = false;
}
}

I don't know if that's what you wanted ..I hope it's helpful to you

thekingrules
05-31-2002, 10:27 AM
Thanks for the help that seems to work nicely except when you get very close to an object. Your game looks promising, keep up the good work.