This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#1 2013-01-13 22:28:41

RenOkshadia3537
New Scratcher
Registered: 2013-01-09
Posts: 33

Javascript Help

I got bored so I decided to make a personal javascript library (I think that's what it is, I've only been using plain javascript, not even jQuery), and I wanted to make a javascript function, that works much like the

move (10) steps
block. But the problem is, for some reason my conversion isn't working. I added a method onto the Math object, like this:

Code:

Math.convertRadToDeg=function(radians){
    var degreeInRadians = Math.PI/180;
    var degrees=degreeInRadians*radians;
    return degrees;
};

Except when I put Math.sin(9) in there, it returns 0.007192824475800312 , and according to google calculator, Math.sin(9) in degrees is 0.15643446504 .
I'm relatively new to Javascript (which is why I've been focusing on plain javascript), and would like to know if there's a better way or what's wrong with my conversion equation, or  if it's something else that's causing it. Here's the code that I'm using it for:

Code:

[i]object[/i].move=function(el,st,dir){
    var oldX=el.style.left;
    var oldY=el.style.top;
    var newX=(Math.convertRadToDeg(Math.sin(dir)))*st;
    var newY=(Math.convertRadToDeg(Math.cos(dir)))*st;
    newY+=oldY;
    newX+=oldX;
    el.style.left=newX;
    el.style.top=newY;
};

Any help would be appreciated.

Offline

 

#2 2013-01-14 00:02:06

RenOkshadia3537
New Scratcher
Registered: 2013-01-09
Posts: 33

Re: Javascript Help

bump

Also forgot that the [code] BBcode doesn't allow [i] in it...

Offline

 

#3 2013-01-14 01:18:35

Hardmath123
Scratcher
Registered: 2010-02-19
Posts: 1000+

Re: Javascript Help

Sine doesn't return an angle, it returns a ratio. If you're unfamiliar with trigonometry, try out our own Scratch Wiki page here.

To solve your problem, you need to first write a function to convert degrees to radians (not the other way around). Then do this:

Code:

Math.sin(Math.convertDegreesToRadians(90));

Does that help?  smile


Hardmaths-MacBook-Pro:~ Hardmath$ sudo make $(whoami) a sandwich

Offline

 

#4 2013-01-14 18:12:19

RenOkshadia3537
New Scratcher
Registered: 2013-01-09
Posts: 33

Re: Javascript Help

Hardmath123 wrote:

Sine doesn't return an angle, it returns a ratio. If you're unfamiliar with trigonometry, try out our own Scratch Wiki page here.

To solve your problem, you need to first write a function to convert degrees to radians (not the other way around). Then do this:

Code:

Math.sin(Math.convertDegreesToRadians(90));

Does that help?  smile

Not really.
When I do:

Code:

Math.sin(Math.convertDegToRad(90));

I get as a result -0.9540914674728181
where the Math.convertDegToRad function is like this:

Code:

Math.convertDegToRad=function(degrees){
    var conversion=180/Math.PI;
    var result=degrees*conversion;
    return result;
};

What's wrong?

(Also, as a side note: I got this message:

Sorry, New Scratchers / TBGers cannot post urls to websites other than Scratch in their posts. Please remove all BB code [url] tags.

for quoting you when you had this in url tags: wiki.scratch.mit.edu/wiki/Trigonometry

Offline

 

#5 2013-01-14 20:11:51

Hardmath123
Scratcher
Registered: 2010-02-19
Posts: 1000+

Re: Javascript Help

Well, new Scratchers can't post URLs, though I assumed the Wiki was allowed. Strange.

Anyway, "conversion" should be the opposite: Math.PI/180. A nice trick is to try to figure out what you get from your formula by inputting 180: you should get PI.  smile


Hardmaths-MacBook-Pro:~ Hardmath$ sudo make $(whoami) a sandwich

Offline

 

#6 2013-01-14 22:29:37

RenOkshadia3537
New Scratcher
Registered: 2013-01-09
Posts: 33

Re: Javascript Help

Hardmath123 wrote:

Well, new Scratchers can't post URLs, though I assumed the Wiki was allowed. Strange.

Anyway, "conversion" should be the opposite: Math.PI/180. A nice trick is to try to figure out what you get from your formula by inputting 180: you should get PI.  smile

I did that and it worked, but the object still isn't moving like it's supposed to. It's in fact, adding in the style attribute for right, in which it's never specified every in any of my code, here's the function:

Code:

object.move=function(el,st,dir){ //element, steps, direction
    var oldX=el.style.left;
    var oldY=el.style.top;
    var newX=((Math.sin(Math.convertDegToRad(dir)))*st)+oldX;
    var newY=((Math.cos(Math.convertDegToRad(dir)))*st)+oldY;
    el.style.left=newX;
    el.style.right=newY;
};

and when I put

Code:

div=document.getElementById('divIMade');
div.style.left='5px';
div.style.top='5px';
object.move(div,1,90);

and I inspect the element, it's new coordinates are:
left: 15px; top: 5px; right: 0.(bunch of zeros)6123233995736765px;

The left is 9 more than it should be (because i said that the steps should be 1), and I never specified right.

Offline

 

#7 2013-01-15 05:50:15

Hardmath123
Scratcher
Registered: 2010-02-19
Posts: 1000+

Re: Javascript Help

Well, here are your problems:

1. Switch the sin and cos functions. I won't go into the geometry here, but trust me, you've got them opposite.

2. The last line of you first function does in fact say el.style.right when it should be el.style.top.

3. This isn't a problem, but don't use the word "object" for an object. The word "Object" (capitalized) is part of the JavaScript language as a built-in, so it's not too nice to use "object". I don't know what the rest of your code looks like, but I personally would write a library like this:

Code:

function(myLibraryName) {
myLibraryName.aCoolFunction = function() {
}
myLibraryName.aConstant = 42;
}(window.myLibraryName)

This lets you change the name of your library really easily by substituting whatever you want into the last line (window.banana would rename your library to banana). The upshot is you don't have any name conflicts.  smile


Hardmaths-MacBook-Pro:~ Hardmath$ sudo make $(whoami) a sandwich

Offline

 

Board footer