Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Help in mathematics for creating walls
Started by sdktester15 Jun 04 2018 06:47 PM

14 replies to this topic
[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

sdktester15

[GLOBAL: userInfoPane.html]
sdktester15
  • Corona Geek

  • 1,121 posts
  • Corona SDK

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.



[TOPIC: post.html]
#2

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 6,608 posts
  • Corona SDK

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?



[TOPIC: post.html]
#3

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 6,608 posts
  • Corona SDK

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>



[TOPIC: post.html]
#4

sdktester15

[GLOBAL: userInfoPane.html]
sdktester15
  • Corona Geek

  • 1,121 posts
  • Corona SDK

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



[TOPIC: post.html]
#5

sdktester15

[GLOBAL: userInfoPane.html]
sdktester15
  • Corona Geek

  • 1,121 posts
  • Corona SDK

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.



[TOPIC: post.html]
#6

davebollinger

[GLOBAL: userInfoPane.html]
davebollinger
  • Corona Geek

  • 1,127 posts
  • Enterprise

: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"



[TOPIC: post.html]
#7

sdktester15

[GLOBAL: userInfoPane.html]
sdktester15
  • Corona Geek

  • 1,121 posts
  • Corona SDK

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. 



[TOPIC: post.html]
#8

sdktester15

[GLOBAL: userInfoPane.html]
sdktester15
  • Corona Geek

  • 1,121 posts
  • Corona SDK

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



[TOPIC: post.html]
#9

davebollinger

[GLOBAL: userInfoPane.html]
davebollinger
  • Corona Geek

  • 1,127 posts
  • Enterprise

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")



[TOPIC: post.html]
#10

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 6,608 posts
  • Corona SDK

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 )




Edited by roaminggamer, 05 June 2018 - 09:03 AM.


[TOPIC: post.html]
#11

sdktester15

[GLOBAL: userInfoPane.html]
sdktester15
  • Corona Geek

  • 1,121 posts
  • Corona SDK

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.



[TOPIC: post.html]
#12

SGS

[GLOBAL: userInfoPane.html]
SGS
  • Corona Geek

  • 1,813 posts
  • Corona SDK

@ed what this does do?

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


[TOPIC: post.html]
#13

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 6,608 posts
  • Corona SDK

 

@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.



[TOPIC: post.html]
#14

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 6,608 posts
  • Corona SDK

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



[TOPIC: post.html]
#15

SGS

[GLOBAL: userInfoPane.html]
SGS
  • Corona Geek

  • 1,813 posts
  • Corona SDK

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




[topic_controls]
[/topic_controls]