Help in mathematics for creating walls

Hi, I am trying to come up with the general equation that would help me work out my wall building module. 

What my module does is it calculates the positions of each wall depending on starting values and a length put in. The length is divided up in the following way:

Say my wall has a length of 100, and an input value of 525 is put in. Starting x and y values are both 0.

The module will get the largest whole number that will result by dividing 525 by 100 and divide the remainder by that number. 

My current formula is 

firstWall.x = startX + (wallLength / 2)

nextWalls.x = prevWall.x + (remainder / numWalls) + (wallLength / 2)

However, this is extremely limited.

For instance, I cannot create vertical walls with this formula or virtually, any walls that do not have an angle of 0, 180, or 360. 

I know there is some trigonometry involved in this, but I don’t know where to start. 

Any suggestions would be great, thank you! I will be figuring this out in the meantime.

I’d really like to help with this, but I’m not fully understanding what you want to do here.

Can you write out some ‘for example’ case?

By this I mean, give us a set of theoretical inputs for this ‘function’ and then show us what the output should be.  Give at least three examples.

Example 1

Inputs:

x = 0

y = 0 

length == 100

modifier = 525

Output:

???

Please explain what ??? should be and make up two more examples.

Also, a hand-drawn picture or other might help.

Again, I want to help but I’m just not quite understanding the question.  i.e. I don’t understand what this modifier value 525 is for.

Also, can you define what a ‘wall’ is? Do you mean a rectangle?  If so, what is the width of this rectangle and is that important in the calculations?

Are you saying you want to be able to do something like this?

local function drawWall( startX, startY, angle, segLen, width, length) ... end

Then, if you call it like this:

-- Note: I am treating 90-degrees and pointing along the positive x-axis -- drawWall( 0, 0, 90, 100, 10, 525 )

The function would draw 6 wall segments, where the the first 5 are 100 pixels long and the last is 25?  

====|====|====|====|====|=

This ‘wall’ would be 10 pixels thick and the left-most wall segment would have its left edge at <0,0>

Ok, I will gladly explain this.

‘wallChunk’ is a module that a built to create a single ‘wall’ which is 127x29. It’s an image of a platform. 

‘wall’ is a module a built to create multiple 'wallChunk’s so that the creation process is much easier.

wall.new() has 6 parameters, with chunkLength (127) and chunkWidth (29) already defined at the top of the module.

The parameters are startX, startY, length, angle and two other parameters that are not important to this topic. 

startX specifies the left edge of the first wallChunk created.

startY specifies the upper edge of the firstWallChunk created.

Length specifies the length of the wallChunks, and is important to calculating the spaces in between the wallChunks.

Angle is the angle of each wallChunk.

Case 1:

So for instance, let’s just say I have the call

wall.new(0, 0, 400, 0, other two parameters)

And for easy math, let’s say that chunkLength is 130. 

The result would be that three walls are created like so:

||| ||| (each wall is 130x29) ||| ||| --- \<-- Gap 5 pixels wide ||| ||| ||| ||| --- \<-- Gap 5 pixels wide ||| ||| ||| |||

Explanation:

My module takes the remainder of length / chunkLength via the modulo operator. Subtracts that remainder from the length, then does length / chunkLength to get the number of wallChunks. (maybe I should probably math.floor it)

So effectively this is calculated: 

numChunks = math.floor(length / chunkLength)

This results in the value 3. Now I use the remainder to space out the walls. The remainder should fit evenly between each wall, so it should be divided by numChunks - 1.

remainder = length % chunkLength || || \/ remainder = 400 % 130 (outputs 10) 10 is divided by 2 (numChunks - 1) Hence the gap of 5.

Notice in the output above: 130 + 5 + 130 + 5 + 130 = 400

It’s a little trickier when it’s angled because this calculation needs to be performed over the x and y-axis.

I’m thinking sin, cos, tan functions to figure out the width of the legs. (triangle formed by the hypotenuse, x-axis, and another invisible line connecting the two) 

I’m using x-axis instead of the y-axis because it’s easier for me to follow.

:D  no-one can follow that.

your “explanation” covers details that don’t help-us-help-you (in fact they muddy the water rather than clear it)

what you should do (and i believe is what roaminggamer was asking for) is a SIMPLE/DIRECT statement of the ACTUAL problem.

fe:  “I need to know how long the x-axis would be of a segment at 30-degrees if its total length (ie hypotenuse) is 127”

Well, what I think I need is something that will calculate the lengths of legs, because what I came up with right now is returning negative values. 

For example, calculate the lengths of legs at a hypotenuse of 127 with an angle measure of 40 degrees. 

Actually, nvm, I just had to use radians instead of degrees.

as i suspected - i don’t mean to be hard on you, but sometimes you really gotta figure out what the question is before you can expect an answer.

none of that “chunk” esoterica posted above matters at all if it boils down to “Q: why is math.cos not returning the expected value?” (then assuming you posted just the relevant code, someone would have quickly “A: trig requires radians”)

This is the best I can figure from what you’re asking:

https://github.com/roaminggamer/RG_FreeStuff/raw/master/AskEd/2018/06/wallBuider.zip

wallBuider.png

local segLength = 127 local segWidth = 19 local debugEn = true local wallM = {} function wallM.new( group, x, y, angle, length, fill, allowEndGap ) group = group or display.currentStage angle = angle or 0 length = length or 260 fill = fill or {1,1,1} -- if( length \< segLength ) then return nil, nil, nil end -- local parts = {} -- -- local numSegments = math.floor( length/segLength ) local remaining = length - (numSegments \* segLength) -- local numGaps = (allowEndGap==true) and numSegments or numSegments-1 local gapLen = 0 if( numGaps \> 0 ) then gapLen = remaining/numGaps end -- print( numSegments, remaining, numGaps, gapLen ) -- local vec = ssk.math2d.angle2Vector( angle, true ) vec = ssk.math2d.scale( vec, segLength + gapLen ) table.dump(vec) -- for i = 1, numSegments do print( i, x, y ) local seg = display.newRect( group, x, y, segLength, segWidth ) seg.anchorX = 0 seg.anchorY = 0 seg.rotation = angle - 90 seg:setFillColor( unpack(fill)) -- parts[#parts+1] = seg -- if( debugEn ) then local tmp = display.newCircle( group, x, y, 5 ) tmp:setFillColor(unpack(\_R\_)) tmp.alpha = 0.75 end -- x = x + vec.x y = y + vec.y end return parts, x, y end return wallM

io.output():setvbuf("no") display.setStatusBar(display.HiddenStatusBar) -- ===================================================== -- ===================================================== require "ssk2.loadSSK" \_G.ssk.init() -- ===================================================== local back = display.newImageRect( "protoBackX.png", 720, 1386 ) back.x = display.contentCenterX back.y = display.contentCenterY if( display.contentWidth \> display.contentHeight ) then back.rotation = 90 end -- ===================================================== -- Example Below -- ===================================================== local wallM = require "wallM" -- -- wallM.new( group, x, y, angle, length, fill ) -- local allowEndGap = false -- Section 1 local parts,x,y parts, x, y = wallM.new( nil, 50, 50, 180, 260, \_Y\_, allowEndGap ) -- Section 2 parts, x, y = wallM.new( nil, x, y, 135, 260, \_C\_, allowEndGap ) -- Section 3 parts, x, y = wallM.new( nil, x, y, 45, 130, \_O\_, allowEndGap ) -- Section 4 parts, x, y = wallM.new( nil, x, y, -45, 130, \_PURPLE\_, allowEndGap )

Well… this sure speeds up the process. 

Thank you very much! I’ll just have to adjust the physics and my own functions, but this takes care of most of it. I am very grateful.

@ed what this does do?

io.output():setvbuf("no")

IIRC - Turns off buffering to console so messages come out immediately.  I’ve used that setting in my projects forever so I kind of forget now.

Here it is… https://docs.coronalabs.com/api/type/File/setvbuf.html

Oh it is an xCode thing… I’m Windows so that’s why I’ve not heard of it.

--creating them local parts, x, y parts, x, y = wall.new(group, centerX - wallAnchorRepos, centerY - wallAnchorRepos, 90, length, false, true) parts, x, y = wall.new(group, x, y, 180, length, false, true) parts, x, y = wall.new(group, x, y, 270, length, false, true) parts, x, y = wall.new(group, x, y, 360, length, false, true) --other functions function M.hideWalls() ? end function M.showWalls() ? end&nbsp;

Quick question, how would I set these walls’ alphas to 0?

Currently, I am having a problem where the walls appear in my scene transitions, so I want to hide them during these transitions.

Put them all in a group and adjust the group alpha.  

Messing with individual  alphas would be terribly time consuming.

That said, I feel like SSKhas a solution for this… checking.

If I read this right, the problem you’re having is that your scene content is visible during some kind of fade transition and the fade doesn’t look right.

Is that correct?

If “Yes,” then SSK has some code that can be taken and modified a little to fix the issue.

Essentially, it takes a snapshot of the whole scene and hides the content.  Then it shows the snapshot as a texture.  This texture will fade nicely.

After the fade is done, you remove the image and re-show the group/content.

The code is in misc.easyBlur.  You just wouldn’t do a blur.

https://roaminggamer.github.io/RGDocs/pages/SSK2/libraries/misc/#easyblur

@sdktester15,

I wanted to point out.  You should really have started a new thread.  Your new question is not related to the topic.

I know it is sometimes easier to just keep a conversation going, but that dilutes the value of the original question and answer pair.

That is my take anyways.

Thank you for this, and sorry about the long thread.