PDA

View Full Version : 3d origin -> screen


Gargoyle
08-26-2001, 10:02 PM
--original text--
this object is half the way to a complete engine.
still missing rotations and filled areas...
but update is coming soon.
--end original--

here is the new version, rotations implemented and two little bugfixes.

-----
LAST UPDATE 09-28-01
2% speed increase and 3 little bugfixes...
-----

function objectOrigin3D(){} //create an objectOrigin3d object

//initiate the object with x,y,z value of virtual matrix
//the virtual width,height and the xandyoffset which represent the screens center
objectOrigin3D.prototype.init = function(xp, yp, zp, wid, hei, xoff, yoff){
this.wi=parseFloat(wid);
this.he=parseFloat(hei);
this.mco=Math.cos;
this.msi=Math.sin;
this.xo=xoff;
this.yo=yoff;
this.vi=parseFloat(this.xo/2.5);
this.d=yoff+20;
this.pX = parseFloat(xp);
this.pY = parseFloat(yp);
this.pZ = parseFloat(zp);
this.vX = parseFloat(0.0);
this.vY = parseFloat(0.0);
this.vZ = parseFloat(0.0);
this.ia = 0;
this.ua=false;
this.dd=(Math.PI/180)
}
//calculates the real values at the screen form the given virtuals and places the assigned movieclip there
objectOrigin3D.prototype.toScreen = function (Hx, Hy, Hz){
if (this.mymc == ""){
return false;
} else {
this.pX = parseFloat(Hx);
this.pY = parseFloat(Hy);
this.pZ = parseFloat(Hz);
this.sX = (((this.vi * this.pX) / (this.d + this.pZ)) + this.xo);
this.sY = (((-(this.vi) * this.pY) / (this.d + this.pZ)) + this.yo);
this.sh = (this.vi * this.wi) / (this.d + this.pZ);
this.st = (this.vi * this.he) / (this.d + this.pZ);
this.sa = (this.vi * this.ia) / (this.yo + 1 + this.pZ);
this.mc._x = this.sX;
this.mc._y = this.sY;
this.mc._width = this.sh;
this.mc._height = this.st;
if(this.ua){
this.mc._alpha = this.sa;
}
return true;
}
}
//calculates the real values at the screen form the given virtuals
objectOrigin3D.prototype.toValues = function (Hx, Hy, Hz){
this.pX = parseFloat(Hx);
this.pY = parseFloat(Hy);
this.pZ = parseFloat(Hz);
this.sX = (((this.vi * this.pX) / (this.d + this.pZ)) + this.xo);
this.sY = (((-(this.vi) * this.pY) / (this.d + this.pZ)) + this.yo);
this.sh = (this.vi * this.wi) / (this.d + this.pZ);
this.st = (this.vi * this.he) / (this.d + this.pZ);
this.sa = (this.vi * this.ia) / (this.yo + 1 + this.pZ);
}
//calculates the real values at the screen from the own virtuals
objectOrigin3D.prototype.updateValues = function (){
this.sX = (((this.vi * this.pX) / (this.d + this.pZ)) + this.xo);
this.sY = (((-(this.vi) * this.pY) / (this.d + this.pZ)) + this.yo);
this.sh = (this.vi * this.wi) / (this.d + this.pZ);
this.st = (this.vi * this.he) / (this.d + this.pZ);
this.sa = (this.vi * this.ia) / (this.yo + 1 + this.pZ);
}
//calculates the real values at the screen form the own virtuals and places the assigned movieclip there
objectOrigin3D.prototype.updateScreen = function (){
if (this.mc == ""){
return false;
} else {
this.sX = (((this.vi * this.pX) / (this.d + this.pZ)) + this.xo);
this.sY = (((-(this.vi) * this.pY) / (this.d + this.pZ)) + this.yo);
this.sh = (this.vi * this.wi) / (this.d + this.pZ);
this.st = (this.vi * this.he) / (this.d + this.pZ);
this.sa = (this.vi * this.ia) / (this.yo + 1 + this.pZ);
this.mc._x = this.sX;
this.mc._y = this.sY;
this.mc._width = this.sh;
this.mc._height = this.st;
if(this.ua){
this.mc._alpha = this.sa;
}
return true;
}
}
//introduces a movieclip to the object, to b placed according to the objects calculations (a victim in such sense :) )
objectOrigin3D.prototype.assignMC = function (mc){
this.mc = eval(mc);
}
//either use the objects alpha calculations of define yar own ones
objectOrigin3D.prototype.enableAlpha = function (){
if (this.mc == ""){
return false;
} else {
this.ia=this.mc._alpha;
this.ua = true;
return true;
}
}
//set an new alpha for the assinged movieclip
objectOrigin3D.prototype.setAlpha = function (s){
if (s == "" || isNaN(s) || s > 100){
return false;
} else {
this.ia = s;
this.ua = true;
return true;
}
}
//move in virtual space
objectOrigin3D.prototype.move = function (){
this.pX += this.vX;
this.pY += this.vY;
this.pZ += this.vZ;
}
//replace the origin of yar object in virtual space
objectOrigin3D.prototype.placeAt = function(Hx, Hy, Hz){
this.pX = parseFloat(Hx);
this.pY = parseFloat(Hy);
this.pZ = parseFloat(Hz);
}
//sets the movments speed and direction to the given values
objectOrigin3D.prototype.setVector = function (Hx, Hy, Hz){
this.vX = parseFloat(Hx);
this.vY = parseFloat(Hy);
this.vZ = parseFloat(Hz);
}
//accelerate the objects speed > 1 get faster <1 get slower
objectOrigin3D.prototype.accelerate = function (s){
this.vX = this.vX * parseFloat(s);
this.vY = this.vY * parseFloat(s);
this.vZ = this.vZ * parseFloat(s);
}
//set the speed value
objectOrigin3D.prototype.setSpeed = function (s){
if(this.vX != 0 || this.vY != 0 || this.vZ !=0){
var len= Math.sqrt((this.vX+this.vX)+(this.vY*this.vY)+(thi s.vZ*this.vZ));
var fak=parseFloat(s)/len;
this.vX = this.vX * fak;
this.vY = this.vY * fak;
this.vZ = this.vZ * fak;
}
}
//want to know how fast yar object is?
objectOrigin3D.prototype.Speed = function (){
len= Math.sqrt((this.vX+this.vX)+(this.vY*this.vY)+(thi s.vZ*this.vZ));
return len;
}

//with these commands u ask the object for values or set them.

//the coordinates width/height and alpha on the real screen
objectOrigin3D.prototype.ScreenX = function (){
return this.sX;
}
objectOrigin3D.prototype.ScreenY = function (){
return this.sY;
}
objectOrigin3D.prototype.ScreenWidth = function (){
return this.sh;
}
objectOrigin3D.prototype.ScreenHeight = function (){
return this.st;
}
objectOrigin3D.prototype.ScreenAlpha = function (){
return this.sa;
}
//positions r the positions in the virtual space
objectOrigin3D.prototype.PosZ = function (){
return this.pZ;
}
objectOrigin3D.prototype.setPosZ = function (s){
this.pZ = parseFloat(s);
}
objectOrigin3D.prototype.PosY = function (){
return this.pY;
}
objectOrigin3D.prototype.setPosY = function (s){
this.pY = parseFloat(s);
}
objectOrigin3D.prototype.PosX = function (){
return this.pX;
}
objectOrigin3D.prototype.setPosX = function (s){
this.pX = parseFloat(s);
}
//the Vec's describe the movement speed and direction of yar object in virtual space
objectOrigin3D.prototype.VecZ = function (){
return this.vZ;
}
objectOrigin3D.prototype.setVecZ = function (s){
this.vZ = parseFloat(s);
}
objectOrigin3D.prototype.VecX = function (){
return this.vX;
}
objectOrigin3D.prototype.setVecX = function (s){
this.vX = parseFloat(s);
}
objectOrigin3D.prototype.VecY = function (){
return this.vY;
}
objectOrigin3D.prototype.setVecY = function (s){
this.vY = parseFloat(s);
}
//inverses the direction of the movment according to the selected axis
objectOrigin3D.prototype.bounceX = function (){
this.vX = -(this.vX);
}
objectOrigin3D.prototype.bounceY = function (){
this.vY = -(this.vY);
}
objectOrigin3D.prototype.bounceZ = function (){
this.vZ = -(this.vZ);
}
//yar object already has speed and direction the direction and speed may b influenced here by adding values to the vector
objectOrigin3D.prototype.addToVec = function (Hx, Hy, Hz){
this.vX += parseFloat(Hx);
this.vY += parseFloat(Hy);
this.vZ += parseFloat(Hz);
}
//rotation around an axis. remember the objects r going to rotate around the origin.
//so if u want something to rotate around another point, first create an new origin for it also using this class.
objectOrigin3d.prototype.rotateX = function (s){
this.pY = ((this.pY * mco(this.dd * s)) - (this.pZ * msi(this.dd * s)));
this.pZ = ((this.pY * msi(this.dd * s)) - (this.pZ * mco(this.dd * s)));
this.vY = ((this.vY * mco(this.dd * s)) - (this.vZ * msi(this.dd * s)));
this.vZ = ((this.vY * msi(this.dd * s)) - (this.vZ * mco(this.dd * s)));
}
objectOrigin3d.prototype.rotateY = function (s){
this.pZ = ((this.pZ * mco(this.dd * s)) - (this.pX * msi(this.dd * s)));
this.pX = ((this.pZ * msi(this.dd * s)) - (this.pX * mco(this.dd * s)));
this.vZ = ((this.vZ * mco(this.dd * s)) - (this.vX * msi(this.dd * s)));
this.vX = ((this.vZ * msi(this.dd * s)) - (this.vX * mco(this.dd * s)));
}
objectOrigin3d.prototype.rotateZ = function (s){
this.pX = ((this.pX * mco(this.dd * s)) - (this.pY * msi(this.dd * s)));
this.pY = ((this.pX * msi(this.dd * s)) - (this.pY * mco(this.dd * s)));
this.vX = ((this.vX * mco(this.dd * s)) - (this.vY * msi(this.dd * s)));
this.vY = ((this.vX * msi(this.dd * s)) - (this.vY * mco(this.dd * s)));
}

and here is a little example what u r able to do with it

veejay
08-27-2001, 04:35 AM
Thats some heavy bit of coding in there.....need to test it.

BTW, check this out too

http://www.layer51.com/proto/d.asp?p=1&f=180

cheers

stereoHead
08-27-2001, 05:02 AM
nice, really nice but your example was a bit "heavy" even on my quite new machine.:)

Gargoyle
08-27-2001, 09:37 AM
@veejay
yeah i know also other guys were dealing with it... ,but i just wanted to reinvent it coz i need to understand it for i l8er want it to do something... my class is used by another which i am coding right now... this then creates creates objects and every of them consists of several of this classes that'S why this is just one half.
this class is just needed for storing purpose of the coordinates of the corners of the objects the other class l8er is going to set...

@stereohead

if u r using a mac, u now know why coders use pc's
if not it is mainly running smooth here only problem r the balls themselfes coz every of them consists of 12 layers with animations also so...
if they come to front it is the flash environment havinbg problems to calculate 12 X 8 gradients...
as u perhaps already may know gradients r very cpu hungry all the way...

atari
08-27-2001, 01:54 PM
Gargoyle, it looks so nice. I'll make a test when I'm with my computer. By the way, it seems to work fine :) and not to flick! :p

M.

stereoHead
08-27-2001, 02:30 PM
Which I had a Mac but since I moved to Thailand they sure are a rare sight:(

Yeah, I know about the gradients but find it hard not to use them.

Got some syntax errors checking your code! Line 222, 228, 234.

Keep up the good work:)

Ulf

Gargoyle
08-27-2001, 04:57 PM
@ atari Thnx :)

@ stereohead syntax errors? please tell me more...
r u able to give me the lines of code?

atari
08-27-2001, 05:06 PM
I suppose that's produced by _x, _y and so terminations.
The lines you've said contents those parameters :)

In spite Flash 5 can output syntax problems it's not a syntax problem. It is maybe a Flash 5 checking problem so don't mess at all ;)

M.

atari
08-27-2001, 05:09 PM
Whoops! I'm sorry but I can't edit my reply so, Gargoyle, these are the _wrong_ (not really wrong) lines.


// Line 222
objectOrigin3d.prototype.rotateAxis_X(s){ ;

// Line 228
objectOrigin3d.prototype.rotateAxis_Y(s){

// Line 234
objectOrigin3d.prototype.rotateAxis_Z(s){


Cheers ;)

M.

Gargoyle
08-27-2001, 05:32 PM
thanks atari hmm yes mayy be flash doesn'T like the ..Axis_X construct at the end scoz it is compareable to ._x
hmm perhaps just throwing the "_" out...
but how du u gain these sntax errors i also would like to read them :)

stereoHead
08-28-2001, 04:40 AM
check syntax: press ctrl-t in the active code window.

Or here's one of the "errors" the others are the same.

Symbol=Symbol 1, Layer=Layer 3, Frame=1: Line 222: ';' expected
objectOrigin3d.prototype.rotateAxis_X(s){

atari
08-28-2001, 07:38 AM
This errors come out when you "Check Syntax" (Actions Panel). Shortcut is Control+T.

Although Flash 5 may output wrong errors (you know what I mean ;)) it often helps with mispelling and bracket problems (specially when you're drunk or whatever :D).

M.

Gargoyle
08-28-2001, 07:49 AM
no guys :) me is still a newbie :)
forgot to copy he last changes i corected...
rotationX "= function (..."
btw the routines were updated some methodnames shortend and also some internals i recommend using the new version...

atari
08-28-2001, 08:02 AM
Hehe Gargoyle :D
I didn't see that mispell. By the way, I've just seen you've updated your first entry so I'll take a look with that when I can.

2% Speed increased? I can't see the changes (I don't have it saved from the first time). Would you mind posting the changes? Just to have a look at that and try to help you improove it.

At first, I see that you're dealing with Math Object so I'd try to create a new method for Math to take certain _demi-complex_ operations.

I'd change eval() sentence as well.


M.

Gargoyle
08-28-2001, 09:49 AM
changes

shorten varnames and method calls...

declared
this.dd=(Math.PI/180)
this.mco=Math.cos;
this.msi=Math.sin;

u would prefer [ ] syntax instead of eval?
hmm as far as i know this doesn'T really make a diffrence. in fact u gonna call this function long before u use other objects methods...

atari
08-28-2001, 10:19 AM
Well. Eval method re-writes code.

Let's have an example:

temp0 = "hi";
temp1 = "bye";
temp2 = "cool";
tempCollection = new Array();
for (var i=0; i<=2; i++) {
tempCollection.push(eval("temp" add i)
}


What flash does is:

temp0 = "hi";
temp1 = "bye";
temp2 = "cool";
tempCollection = new Array();
[iteration begins]
tempCollection.push("hi");
[iteration - evaluation - code rewrited]
tempCollection.push("bye");
[iteration - evaluation - code rewrited]
tempCollection.push("cool");
[iteration - break forced by condition]


Flash books information of what variables are being used in an Array way (although you don't tell Flash to do that), so... when you're using brackets to eval things you're calling to an array Flash is booking into it system.

Then, Flash don't have to rewrite code to work in this way while eval does make Flash stop and think what element of its array is being called by ActionScript.

Hope this helps ;)

M.

Gargoyle
08-29-2001, 01:11 AM
please write down the code u would like to b used...

then we gonna compare the result in flasm. and look what needs lesser lines of code after assamblation

MixMatch
08-29-2001, 01:23 AM
Originally posted by Gargoyle
[B
-----
LAST UPDATE 09-28-01
2% speed increase and 3 little bugfixes...
-----

[/B]

Are you sure that wasn't 8-28-01???
BTW, this is extremely nice, and of course, way outside my range... but I did notice that there is a humongous increase in speed and very unnoticable decrease in quality when this is played in low quality:D (I'm using a pretty recent PC and it was really slow at HIGH quality too...)

Anyways, Congrats Gargoyle:D

atari
08-29-2001, 07:55 AM
Gargoyle, you just have an eval sentence :D so... I think that this won't be perceptible for us. By the way, .swf occupies 2 bytes more (wow) while using eval. Although, what I was talking about is that Flash operational quote increases using eval sentences (as I explained in the previous reply).

M.

Gargoyle
08-29-2001, 11:16 AM
@ Mixmatch sure sorry but in germany u place the day at first. my fault.

@ atari, this is the code

setMember
push 'objectOrigin3D'
getVariable
push 'prototype'
getMember
push 'assignMC'
function (mc)
push 'this'
getVariable
push 'mc', 'mc'
getVariable
getVariable
setMember
end // of function

as u can c it exactly does what it shall do

push 'mc', 'mc' //init the constant 'mc'
getVariable //get it's content
getVariable //get the contents content

exactly like this i would have coded it so in this case with just one value there is exactly one
iteration - evaluation which is also necessary here.

u r right in one sense if u have to do more than one evaluation it is better to use the [] syntax, but in this case it doesn't change anything also the [] just generate one more getVariable so... using yar example u would b right for flash then starts to store the result in another vairable and then recombines them after but not here.

even if u don't like the eval syntax as u dan c in the code this already was pushed to maximum efficency by flash so no urge to change it.

much more important is to develop the second part of the engine yes the one calculating the polygones for the raytracer and for going again over the code and increase it'S speed by reassambling it.

right now i am working on the objectPolygon class
and after at the object3DObject class
which also is including lightsources and stuff...
the polygon clas itself is going to use 3 objectOrigin3D clases for storing the points in space and a color object for every point.
and it will b able to create a filled triangle out of this data with faked gradients.

the object object class then l8er is going to put all these polygon classes together and creates balls or landscape out of it...
for these purpose we need even faster routines. so i gonna rewrite them using flasm after i get a clue how.

perhaps i also need to change the whole objects structure for i don't want redundant data in a ball created using these methods... and storing the points doubled for every polygon if they r connected is waste of performance...
well c this class decribes a position in virtual space and is able to transform it into a screen view and it also stores speed + direction + rotation of this point according to the spaces origin. so the other classes gonna burst my mind not this one this is complete and working...
it still needs a performance push to b useable and the others too so... don'T worry u guys get a high performance renderer just gimme the time to develop it.

atari
08-29-2001, 01:39 PM
Wow Gargoyle, nice reply!

You said:

exactly like this i would have coded it so in this case with just one value there is exactly one
iteration - evaluation which is also necessary here.

u r right in one sense if u have to do more than one evaluation it is better to use the [] syntax, but in this case it doesn't change anything also the [] just generate one more getVariable so... using yar example u would b right for flash then starts to store the result in another vairable and then recombines them after but not here.

even if u don't like the eval syntax as u dan c in the code this already was pushed to maximum efficency by flash so no urge to change it.


I totally agree with that. What I was trying to explain is that Eval sentences (when used so often) makes Flash work slower than it should.

I'm sure you'll do it nice but if you need something just call me :)

Actually, I've been thinking about 3D engines but (as you can see everywhere) all of them are just _empirical phormulae_.

3D and so _require_ matrix transformations, not in order to get a new position for an object but for illumination.

M.

luke
08-29-2001, 04:16 PM
note on performace:
those getting poor performance out of Gargoyle's fine work are probably running a higher screen resolution. On my machine here (dual 450 P3) it runs rough with my screen res @ 1260x1024. If I shrink the flash movie window, performance is fine.

atari - thanks for the info on eval() vs. []. I had been fiddling with the two as well, though not at the depth you have. good work.


cheers,

_luke

Gargoyle
08-29-2001, 09:43 PM
I'm sure you'll do it nice but if you need something just call me

thanx... but first of all i need time, time i don't have at the moment...

atari
08-30-2001, 07:03 AM
Time... Day should have 48 hours for us! :D
I don't know what has happened with my previous reply. I quoted a part of the code and the result is a //4.

I don't know what's happening...

Thanks Luke ;)

M.

--- modearator note---
don't worry atari i read em before validating u wrote something about the opcode. and
phu also don't remember... but u found it intresting i think but nor sure.