Forum

> > CS2D > Scripts > Sin and Cos bug... or something.
Forums overviewCS2D overview Scripts overviewLog in to reply

English Sin and Cos bug... or something.

15 replies
To the start Previous 1 Next To the start

old Sin and Cos bug... or something.

Infinite Rain
Reviewer Off Offline

Quote
Hello guys, some time ago I discovered a glitch/bug/IDK waht in Lua scripting
I think everyone of you saw my script: Jumping script

So the problem is when you jump, the player is teleporting using sin and cos and when you pointing to up (dir 0), you teleporting faster and jump through more distance than you jumping and pointing down(dir 180). What's the problem? Why is it happening?!

P.S(Everything went good when I used images)

old Re: Sin and Cos bug... or something.

EngiN33R
Moderator Off Offline

Quote
@user kalis: Just go learn some trigonometry and see examples of trig functions usage in scripts.

@user Infinite Rain: Unless I'm missing something or don't know something, that doesn't seem to be possible. Following logic in a perfect situation:

• You are jumping either directly up or directly down - 0 and 180 degrees respectively, x and, by extension, sine, is not used;
• Since only cosine is used, we need to analyse its values;
• If you look at the unit circle, cosine of up, which is 0 degrees, is 1, thus you'd propel yourself 1 pixel up. Cosine of down, on the other hand, which is 180°, is -1, and thus you'd propel yourself down 1 pixel.

Are you sure it's not a visual deception?

old Re: Sin and Cos bug... or something.

Infinite Rain
Reviewer Off Offline

Quote
user EngiN33R has written
@user kalis: Just go learn some trigonometry and see examples of trig functions usage in scripts.

@user Infinite Rain: Unless I'm missing something or don't know something, that doesn't seem to be possible. Following logic in a perfect situation:

• You are jumping either directly up or directly down - 0 and 180 degrees respectively, x and, by extension, sine, is not used;
• Since only cosine is used, we need to analyse its values;
• If you look at the unit circle, cosine of up, which is 0 degrees, is 1, thus you'd propel yourself 1 pixel up. Cosine of down, on the other hand, which is 180°, is -1, and thus you'd propel yourself down 1 pixel.

Are you sure it's not a visual deception?

Yeah, it's not a visual deception, because IT ACTUALLY jump more distance.
Maybe something with my technique of using sine and cosine?

look how I'm using it, lol.

1
2
x = x + math.sin(math.rad(rot)) * distance
y = y + -math.cos(math.rad(rot)) * distance

I asked Blaz and he said that I using everything wrong, and I must do smth like:

1
2
x = x + math.cos(math.rad(rot)) * distance
y = y + math.sin(math.rad(rot)) * distance
And something with rot and pi/2

old Re: Sin and Cos bug... or something.

EngiN33R
Moderator Off Offline

Quote
Well, of course you were using it wrong. Look up 'Unit circle' in Wikipedia. The x axis is taken as cosine, and y is taken as sine. I don't get why you were using -math.cos still. What BlazingNote gave you are the correct formulae to use.
edited 1×, last 23.05.12 04:50:45 pm

old Re: Sin and Cos bug... or something.

DC
Admin Off Offline

Quote
Carnage Contest actually uses a unit circle where 0 is at top. I'm not sure if CS2D does it the same way so just print the player rotation while you look up and if it is close to 0 then it is done the same way. In that case the calculation with -cos was actually correct.
(See http://www.carnagecontest.com/help_luaweapon2.php - it's for Carnage Contest but it's the same principle)

old Re: Sin and Cos bug... or something.

EngiN33R
Moderator Off Offline

Quote
We don't, we use x + cos(x) and y + sin(y). Well, at least I don't. Blaz was correct when he mentioned 'something about pi/2'.

1
2
x = x + math.cos(math.rad(rot)-math.pi/2) * distance
y = y + math.sin(math.rad(rot)-math.pi/2) * distance

This fixes it. CS2D uses the same kind of unit circle as CC does, since looking up gives you cosine of 1 and sine of 0.

old Re: Sin and Cos bug... or something.

Infinite Rain
Reviewer Off Offline

Quote
user EngiN33R has written
We don't, we use x + cos(x) and y + sin(y). Well, at least I don't. Blaz was correct when he mentioned 'something about pi/2'.

1
2
local x = player(id,"x") + math.cos(math.rad(rot)-math.pi/2) * distance
local y = player(id,"y") + math.sin(math.rad(rot)-math.pi/2) * distance

This fixes it.

As I read in WIKI 90 degree in radians is pi / 2?
So why we need to use
1
- math.pi / 2
???

old Re: Sin and Cos bug... or something.

EngiN33R
Moderator Off Offline

Quote
Logic chain strikes again:

• CS2D uses the same unit circle as CC (or, rather, vice versa, right?), where 0 is on top instead of on the right and it goes clockwise;
• When looking in the direction of the default unit circle's 0 (right), we get the sine and cosine values of 90;
• We therefore must subtract 90 in order for the cosine and sine values to propel us in the direction of the default unit circle's 0° - right - when looking at default 0° - right.

I think I got it right.

old Re: Sin and Cos bug... or something.

EngiN33R
Moderator Off Offline

Quote
There IS no speed difference. Here's my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
addhook("serveraction","propel")
function propel(id,a)
	if a==1 then
		local distance = 32
		local rot = player(id,"rot")
		local x = player(id,"x") + math.cos(math.rad(rot)-math.pi/2) * distance
		msg("math.cos(math.rad(rot)-math.pi/2) = "..math.cos(math.rad(rot)-math.pi/2))
		local y = player(id,"y") + math.sin(math.rad(rot)-math.pi/2) * distance
		msg("math.sin(math.rad(rot)-math.pi/2) = "..math.sin(math.rad(rot)-math.pi/2))
		msg("x displacement = "..x-player(id,"x"))
		msg("y displacement = "..y-player(id,"y"))
		parse("setpos "..id.." "..x.." "..y)
	end
end

It propels you equally in all sides.

old Re: Sin and Cos bug... or something.

omg
User Off Offline

Quote
ive read thru the code and rigorously tested variations, and my only conclusion is that factis' jump code glitches up a bit because it runs every frame; its designed to work in exactly the same way as engineer's code, and theres no other reason why they would act differently

if u try factis' code, it seriously moves funny when u point in different directions; i havent looked at every detail, but the part with the setpos is completely correct...it just glitches up somehow on running

i also moved the jumping portion of the code from the always function to the ms100 function and there was a noticeable increase in accuracy, though still not perfect

the accuracy on engineer's code (or any other setpos code that is triggered only once) is virtually perfect

old Re: Sin and Cos bug... or something.

Snurq
BANNED Off Offline

Quote
user Infinite Rain has written
user EngiN33R has written
@user kalis: Just go learn some trigonometry and see examples of trig functions usage in scripts.

@user Infinite Rain: Unless I'm missing something or don't know something, that doesn't seem to be possible. Following logic in a perfect situation:

• You are jumping either directly up or directly down - 0 and 180 degrees respectively, x and, by extension, sine, is not used;
• Since only cosine is used, we need to analyse its values;
• If you look at the unit circle, cosine of up, which is 0 degrees, is 1, thus you'd propel yourself 1 pixel up. Cosine of down, on the other hand, which is 180°, is -1, and thus you'd propel yourself down 1 pixel.

Are you sure it's not a visual deception?

Yeah, it's not a visual deception, because IT ACTUALLY jump more distance.
Maybe something with my technique of using sine and cosine?

look how I'm using it, lol.

1
2
x = x + math.sin(math.rad(rot)) * distance
y = y + -math.cos(math.rad(rot)) * distance

I asked Blaz and he said that I using everything wrong, and I must do smth like:

1
2
x = x + math.cos(math.rad(rot)) * distance
y = y + math.sin(math.rad(rot)) * distance
And something with rot and pi/2


If it works, it works. It's a shame that DC got the degrees etc wrong, but the first piece of code is correct for cs2d. and +- = -

If you wwant to fix the angle to the way the mathematicians use it,do this
1
2
3
new_rot = (rot - 90) % 360
x = x + math.cos(math.rad(new_rot)) * distance
y = y + math.sin(math.rad(new_rot)) * distance
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview