SA-MP Forums Archive
How to do this? - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: How to do this? (/showthread.php?tid=661235)



How to do this? - akib - 27.11.2018



Suppose 'O' is a center of a circle.
I want to create random objects, more clearly, I want to create random fire inside of the circle.
But how I can do it?


Re: How to do this? - Rolux - 27.11.2018

Код:
new Float:centerX = 0.0;//center of the circle
new Float:centerY = 0.0;//center of the circle

new Float:radius = 3.0;//radius of the circle
new Float:random = frandom(0, 6.28);

new Float: px = centerX/2+floatcos(random,degrees)*radius;
new Float: py = centerY/2+floatsin(random,degrees)*radius;
	    
printf("x%f y%f",px,py);
Код:
Float:frandom(Float:max, Float:min = 0.0, dp = 4)//made by ******
{
    new Float:mul = floatpower(10.0, dp),

    imin = floatround(min * mul),
    imax = floatround(max * mul);
    return float(random(imax - imin) + imin) / mul;
}



Re: How to do this? - akib - 28.11.2018

What is 6.28 in this line?
Код:
new Float:random = frandom(0, 6.28);



Re: How to do this? - niCe - 28.11.2018

That's 2 pi (pi = 3.141592654), 2 pi radians equal 360 degrees. However, since in the code above anglemode in floatcos and floatsin is set to degrees, "random" should be values between 0-360, not in radians.


Re: How to do this? - akib - 28.11.2018

This code isn't working for me :/ it creates object outside of the circle...

if the center is 2814.9570,1313.2760,10.7500
and it's X Y is 2814.9570,1313.2760 then

Код:
Float:frandom(Float:max, Float:min = 0.0, dp = 4)//made by ******
{
    new Float:mul = floatpower(10.0, dp),

    imin = floatround(min * mul),
    imax = floatround(max * mul);
    return float(random(imax - imin) + imin) / mul;
}

COMMAND:fireup(playerid,params[]){
	new Float:centerX = 2814.9570;//center of the circle
	new Float:centerY = 1313.2760;//center of the circle

	new Float:radius = 3.0;//radius of the circle
	for(new i=0;i<8;i++){
		new Float:rrandom = frandom(0, 6.28);
		new Float: px = centerX/2+floatcos(rrandom,degrees)*radius;
		new Float: py = centerY/2+floatsin(rrandom,degrees)*radius;
		CreateObject(18688, px,py, 10.7500,0,0,0,300.0);
		printf("%f,%f",px,py);
	}
	//2814.9570,1313.2760,10.7500
	return 1;
}
and results:
Код:
1408.816772,658.124267
1406.247192,655.061950
1405.515258,657.019592
1408.596923,658.296081
1409.357910,657.322021
1406.039794,655.248657
1409.434814,657.053833
1407.825805,654.668395



Re: How to do this? - m4karow - 28.11.2018

you can check my older thread maybe it helps

https://sampforum.blast.hk/showthread.php?tid=657613


Re: How to do this? - NaS - 28.11.2018

Quote:
Originally Posted by akib
Посмотреть сообщение
This code isn't working for me :/ it creates object outside of the circle...

if the center is 2814.9570,1313.2760,10.7500
and it's X Y is 2814.9570,1313.2760 then

Код:
Float:frandom(Float:max, Float:min = 0.0, dp = 4)//made by ******
{
    new Float:mul = floatpower(10.0, dp),

    imin = floatround(min * mul),
    imax = floatround(max * mul);
    return float(random(imax - imin) + imin) / mul;
}

COMMAND:fireup(playerid,params[]){
	new Float:centerX = 2814.9570;//center of the circle
	new Float:centerY = 1313.2760;//center of the circle

	new Float:radius = 3.0;//radius of the circle
	for(new i=0;i<8;i++){
		new Float:rrandom = frandom(0, 6.28);
		new Float: px = centerX/2+floatcos(rrandom,degrees)*radius;
		new Float: py = centerY/2+floatsin(rrandom,degrees)*radius;
		CreateObject(18688, px,py, 10.7500,0,0,0,300.0);
		printf("%f,%f",px,py);
	}
	//2814.9570,1313.2760,10.7500
	return 1;
}
and results:
Код:
1408.816772,658.124267
1406.247192,655.061950
1405.515258,657.019592
1408.596923,658.296081
1409.357910,657.322021
1406.039794,655.248657
1409.434814,657.053833
1407.825805,654.668395
Like niCe said, those angle calculations are in degrees, so instead of 6.28 you should use 360.0, otherwise the objects will only get spread between 0° and 6.28°.

Oh and remove the /2 from centerX and centerY, it's not neccessary. This already explains why the results are only half of your original coords.


Re: How to do this? - akib - 29.11.2018

Thanks, it's working now. <3