set random angle with exclusion

Get help using Construct 2

Post » Wed Feb 29, 2012 9:34 pm

Here's what i have.


Event 4 is checking if newly created sprite angle is equal to the previous sprites angle. If both angles are equal then it sets new angle for new sprite.

Example:
If first sprites angle is equal 90 then second sprite is set to random(angle), but this random(angle) for second sprite can produce angle equal to 90 as well.

How can I prevent this?
ImageImageImageImage
B
140
S
58
G
36
Posts: 2,547
Reputation: 31,170

Post » Wed Feb 29, 2012 10:08 pm

You could try choose(random(0,firstangle - t),random(firstangle + t, 360)
Where 't' is your tolerance.
This should work, I have not tested it. But you can see what I mean.
B
90
S
30
G
24
Posts: 3,189
Reputation: 32,400

Post » Wed Feb 29, 2012 11:00 pm

Thanks for answer @sqiddster it should work but it's not :/

floor(choose(random(0,Last_Left_Angle-90)/90,random(Last_Left_Angle+90, 360)/90))*90

if Last_Left_Angle = 90
then choose(random(0,0)/90, random(180,360)/90)*90
- choose(0,[2 or 3 or 4])*90
- will give 0, 180, 270, 360 - correct

if Last_Left_Angle = 180
then choose(random(0,90)/90, random(270,360)/90)*90
- choose([0 or 1], [3 or 4])*90
- will give 0, 90, 270, 360 - correct

but sprites still are on the same angles :/
ImageImageImageImage
B
140
S
58
G
36
Posts: 2,547
Reputation: 31,170

Post » Wed Feb 29, 2012 11:13 pm

OK, I had a look at your capx.
All you need to do is store a global variable last_angle.
if last_angle = 90 then set angle to choose(0,180,270)
if last_angle = 0 then set angle to choose(90,180,270)
etc.

This is not the most elegant solution, but I am sick ;)
B
90
S
30
G
24
Posts: 3,189
Reputation: 32,400

Post » Wed Feb 29, 2012 11:17 pm

haha yeah, it's a harsh solution but will do for now ;)
get well @sqiddster :)
ImageImageImageImage
B
140
S
58
G
36
Posts: 2,547
Reputation: 31,170

Post » Wed Feb 29, 2012 11:42 pm

I feel bad for implementing such a brute solution... perhaps when my cognitive functions increase :D
B
90
S
30
G
24
Posts: 3,189
Reputation: 32,400

Post » Thu Mar 01, 2012 12:04 am



still wrong,I'm really missing something obvious here.
ImageImageImageImage
B
140
S
58
G
36
Posts: 2,547
Reputation: 31,170

Post » Thu Mar 01, 2012 12:22 pm

Your code is weird. On creation you directly set a random angle, and then you store it in Last_Left_Angle to discard it afterward... What is the point? It will still look like a basic choose(0,90,180,270)

But I think I understand what you want to do, I would do it like that:

[code]Global number Last_Left_Angle=-1 // no discarding at start
+TrenchTile: Wall = "Left"
+TrenchTile: Pick nearest to (0,0)
+TrenchTile: Y > 60
Local choice = "" // to store the possible angles 0,1,2,3 * 90
+System: repeat 4 times
+System: loopindex is not equal to Last_Left_Angle
    -> System: set choice to (choice = "") ? loopindex : ","&loopindex
-> System: Create object TrenchTile on layer "Tiles Front" at (Left_Wall+Wall_Move, TrenchTile.Y-TrenchTile.Height)
-> System: set Last_Left_Angle to floor(random(4))
-> TrenchTile: set angle to Last_Left_Angle*90[/code] Yann2012-03-01 12:23:09
B
60
S
22
G
14
Posts: 1,479
Reputation: 16,346

Post » Thu Mar 01, 2012 1:28 pm

[quote]On creation you directly set a random angle, and then you store it in Last_Left_Angle to discard it afterward[/quote]

@Yann Yes i know... I was playing with that stuff trying different solutions.

I've tried your code but it still gives me same thing. Newly created sprite still can have angle from previous one.
ImageImageImageImage
B
140
S
58
G
36
Posts: 2,547
Reputation: 31,170

Post » Thu Mar 01, 2012 10:38 pm

yeah I was too tired I guess
You can notice that I didn't even use the "choice" variable I built

And I also made a mistake building this variable

Code: Select all
Global number Last_Left_Angle=-1 // no discarding at start
+TrenchTile: Wall = "Left"
+TrenchTile: Pick nearest to (0,0)
+TrenchTile: Y > 60
  Local choice = ""  // to store the possible angles 0,1,2,3 * 90
  +System: repeat 4 times
  +System: loopindex is not equal to Last_Left_Angle
    -> System: set choice to (choice = "") ? loopindex : choice&","&loopindex
  -> System: Create object TrenchTile on layer "Tiles Front" at (Left_Wall+Wall_Move, TrenchTile.Y-TrenchTile.Height)
  -> System: set Last_Left_Angle to int(tokenat(choice,floor(random(tokenCount(choice,","))),","))
  -> TrenchTile: set angle to Last_Left_Angle*90



with an example
randomWithExclusion.capx
Last edited by Yann on Sun Sep 14, 2014 9:30 pm, edited 1 time in total.
B
60
S
22
G
14
Posts: 1,479
Reputation: 16,346

Next

Return to How do I....?

Who is online

Users browsing this forum: No registered users and 27 guests