Explosive confusion

Get help using Construct 2

Post » Thu Nov 21, 2013 11:50 pm

Okay, I wanted to quickly create a little example of a bomb mechanic that works similar to Bomberman.

I'm doing it using an array storing the obstacles that block the blast (value of 1)and a function that uses a nested loop which creates the blast in the four directions around it(using cos and sin) and only continues the line of explosion if there isn't an obstacle.

I thought of this implementation as rather elegant.

Unfortunately it isn't quite working. In the example you click to create an explosion at that cell position. Now the blast that goes out north (270) doesn't actually get blocked by obstacles when creating explosions on the left side of the screen. Or it gets blocked where there are no obstacles at all. On the right side it however works fine and all other blast directions also work according to plan (everywhere).

The values in the array also look fine judging from the debugger view.

This should be simple, but instead it's driving me mad. I've looked over it a million times and can't figure out why it's behaving so strangely.



Oh yeah, here is the capx: https://dl.dropboxusercontent.com/u/2306601/bomb_1c.capx

Maybe it just needs a fresh set of eyes.
B
22
S
6
G
10
Posts: 1,034
Reputation: 7,514

Post » Fri Nov 22, 2013 2:15 am

after quite some testing, I found the problem. Seems to be some sort of computational error

use
Function.Param(0)+round(cos(loopindex("boomSides")*90))*loopindex("boomRange")
and
Function.Param(1)+round(sin(loopindex("boomSides")*90))*loopindex("boomRange")

when comparing the value in the array
Visual Novel 'Engine' in 100 Events
if you ever have to choose between buying Construct 2 on scirra.com or on Steam, read this: Review
B
22
S
9
G
1
Posts: 787
Reputation: 3,786

Post » Fri Nov 22, 2013 9:05 am

[QUOTE=mindfaQ] after quite some testing, I found the problem. Seems to be some sort of computational error

use
Function.Param(0)+round(cos(loopindex("boomSides")*90))*loopindex("boomRange")
and
Function.Param(1)+round(sin(loopindex("boomSides")*90))*loopindex("boomRange")

when comparing the value in the array[/QUOTE]

Thank you very much for taking a look and nailing the error!

A computational error indeed, I don't actually think this should need the round expression at all. I mean we aren't even dealing with floats here, all the variables used in the event have simple integer values.

I found that you only need to apply the round for the X value in the condition, the second one isn't actually needed. I guess this makes some sense since the error only occurs on the left side, meaning at a certain range of X coordinates.

The X calculation for the 270 angle needs to be always 0 though, which even my calculator agrees on. Yet Construct somehow needs round to make it work?!?

So yeah, this leads me to believe this might be an actual bug in Construct 2. It could be an accuracy issue with the math expression cos or simply a general issue with that array condition and using math in it.
B
22
S
6
G
10
Posts: 1,034
Reputation: 7,514

Post » Fri Nov 22, 2013 10:05 am

Sin, and cos will have some issues when it comes to rounding angles, plus the only time you might want to use it is when those rounding issues would pop up ie diagonals. Catch 22 I guess.
Also I'd highly suggest snapping your mouse xy with int(mouse.x/cell)*cell +cell/2 for tile games.
Image ImageImage
B
168
S
50
G
164
Posts: 8,227
Reputation: 105,573

Post » Fri Nov 22, 2013 11:31 am

[QUOTE=newt] Sin, and cos will have some issues when it comes to rounding angles, plus the only time you might want to use it is when those rounding issues would pop up ie diagonals. Catch 22 I guess.
[/QUOTE]

Right, but this is calculating the following three things for the north direction:

cos(3*90)*1
cos(3*90)*2
cos(3*90)*3

And all of these should equal 0. Yet I have to put round in front of it for some reason. Is this really something to be expected? I don't recall having had such an issue at any point in my ~5 years with C2 and Classic before that. And cos and sin were being used frequently.

[QUOTE=newt]Also I'd highly suggest snapping your mouse xy with int(mouse.x/cell)*cell +cell/2 for tile games.[/QUOTE]

That may be so, but I don't see the relevance here.
B
22
S
6
G
10
Posts: 1,034
Reputation: 7,514

Post » Fri Nov 22, 2013 11:37 am

Probably the rounding issue is due to how the expression is converted to an integer. Which likely is the equivalent to int(expression).

@newt
Couldn't you also just use round(mouse.x/cell)*cell
B
91
S
31
G
103
Posts: 5,235
Reputation: 67,756

Post » Fri Nov 22, 2013 12:22 pm

I was more like suggesting you use x, and y, offsets rather than sin, and cos.
In other words there is no extra benefit to using the trig, unless you wanted to do diagonals.

@Rojo, yes, its just that the cell/2 gets you the hotspot offset.

Image ImageImage
B
168
S
50
G
164
Posts: 8,227
Reputation: 105,573

Post » Fri Nov 22, 2013 7:10 pm

Now that I'm wide awake and not on my phone I did some tests. I'm pretty sure you'd have to use round() in CC as well. And as far as being just integer values cos(270) is not exactly 0 in either C2 or CC.
If you set some text to cos(270) you'll get 0, but that's because C2 does some rounding to give you prettier numbers for display. If you set the text to str(cos(270)) you'll bypass the pretty rounding and get -1.8369701987210297e-16.
B
91
S
31
G
103
Posts: 5,235
Reputation: 67,756

Post » Fri Nov 22, 2013 8:03 pm

[QUOTE=R0J0hound] Now that I'm wide awake and not on my phone I did some tests. I'm pretty sure you'd have to use round() in CC as well. And as far as being just integer values cos(270) is not exactly 0 in either C2 or CC.
If you set some text to cos(270) you'll get 0, but that's because C2 does some rounding to give you prettier numbers for display. If you set the text to str(cos(270)) you'll bypass the pretty rounding and get -1.8369701987210297e-16.[/QUOTE]

In the meantime I did actually recreate the same example with CC and ran into the exact same issue, haha. So I guess I should just shut up and accept my defeat at math. And use round
B
22
S
6
G
10
Posts: 1,034
Reputation: 7,514

Post » Fri Nov 22, 2013 9:01 pm

It could be useful though if the array rounded instead of floored values to integers. Although that may lead to other math issues in other situations...
B
91
S
31
G
103
Posts: 5,235
Reputation: 67,756

Next

Return to How do I....?

Who is online

Users browsing this forum: No registered users and 9 guests