开发者

What is causing Uncaught TypeError: number is not a function?

As you can tell I'm trying to put together a very simple 3d engine. I'm writing it in javascript. I think it's likely that there is an error in the formula, but for the life of me I can't find it. So now I'm thinking there might be something else I haven't considered yet. The error occurs on line 21 (dx = Math.cos.....)

Here is the relevant part of my code:

// Camera Position in x,y,z
var c = [ 0,0,0 ];

// Viewer position [x,y,z]
var v = [ 0,0,0 ];

// Angle of view [x, y, z]
var a = [ 0.01, 0.01, 0.01 ];

var point =  [ 0,0, 50 ];

dx = Math.cos(a[1])(Math.sin(a[2])(point[1] - c[1]) + Math.cos(a[2])(point[0] - c[0])) - Math.sin(a[1])(point[2] - c[2]);
dy = Math.sin(a[0])(Math.cos(a[1])(point[2] - c[2]) + Math.sin(a[1])(Math.sin(a[2])(point[1] - c[1]) + Math.cos(a[2])(point[0] - c[0]))) + Math.cos(a[0])(Math.cos(a[2])(point[1] - c[1]) - Math.sin(a[2])(point[0] - c[0]));
dz = Math.cos(a[0])(Math.cos(a[1])(开发者_运维技巧point[2] - c[2]) + Math.sin(a[1])(Math.sin(a[2])(point[1] - c[1]) + Math.cos(a[2])(point[0] - c[0]))) - Math.sin(a[0])(Math.cos(a[2])(point[1] - c[1]) - Math.sin(a[2])(point[0] - c[0]));

bx = (dx - v[0])(v[2]/dz);
by = (dy - v[1])(v[2]/dz);


You need to use * to multiply:

dx = Math.cos(a[1])*(Math.sin(a[2])*(point[1] - c[1]) ... etc


I'm no Javascript expert, but I'm guessing Math.cos(a[1]) returns a number, and doing Math.cos(a[1])(5) is trying to use a number as a function. If you want to multiply two numbers you should use the '*' multiplication operator.


You cannot multiply by writing (a)(b)(c) like you do in math.
This is parsed as two function calls, so you get an error that the (a) isn't a function.

Instead, write a * b * c.


You are leaving out multiplication operator, e.g.:

x = Math.cos(a[1]) * (Math.sin(a[2]) * (...
-------------------^-----------------^

-- Rob

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜