Share Your Code

Grid Manipulation Sample Code

Posted by Ninja Pig Studios, Posted on February 7, 2012, Last updated March 3, 2012

This code allows you to move your player by touching the screen. The grid will then manipulate according to the players position. You can change the radius of the manipulated area, and change the line width. We are having problems with lag if there are multiply objects on the grid. If anyone can help solve the issue, we will update our code accordingly. Hope this helps!


Please click the LIKE button above if you like this code!

^^^^^^^^ Download .ZIP file above with all of the documents ^^^^^^^^

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
---------------------------------
--Grid Manipulation Sample Code--
--     Follow us on Twitter    --
--       @ninjapigstudios      --
--      Like us on Facebook    --
-- facebook.com/ninjapigstudios--
---------------------------------
 
_W = display.contentWidth --Assign Varibles--
_H = display.contentHeight
 
grid = display.newGroup()  --Make a new group for the grid object--
 
------Spawn Enemy's This is where it laggs------
local enemies = {}
local enemySpawn = {
        {100,200},
        {200,400},
        {0,300},
        {math.random(0,600),math.random(0,600)}
}
 
                local function newEnemy(e)
                        local spawnpoint = enemySpawn[math.random(1,#enemySpawn)]
                        enemy = display.newRect(spawnpoint[1],spawnpoint[2],25,25)
                        enemies[#enemies+1] = enemy
                        enemy:setFillColor(255,255,0)
                        transition.to(enemy, {time=2000, x=_W/math.random(1,9), y=_H/math.random(1,9)})
                end
 
                --spawnTimer = timer.performWithDelay(1000, newEnemy, 50)
-------------------------------------------------
 
local function dist( x,y, u,v )
        return math.sqrt((x-u)^2+ (y-v)^2) --Distance Equation--
end
 
--Set up some open tables for data--
local originalpoints = {}
local _points = {} 
local points = {} 
local lines = {} 
local displayLines = {}
displayLines._mode = "kv"
 
--Creates the vertical lines--
local  k = 1
for j = 0, 320, 20 do
lines[ k ] = {}
points[j] = {}
        for i = 0, 480, 20 do
                points[j][i] = { i, j }
                originalpoints[#originalpoints + 1] =  {points[j][i][1], points[j][i][2]}
                _points[#_points + 1] = points[j][i]
                lines[ k ][ #lines[ k ]+1  ] = points[j][i]
        end
 k = k + 1
end
 
--Creates the horizontal lines--
for i = 0, 480, 20 do
lines[ k ] = {}
 
        for j = 0, 320, 20 do
                lines[ k ][ #lines[ k ]+1  ] = points[j][i]
        end
         k = k + 1
end
 
local player = display.newImage("glowingplayer.png") --Add the glowing player--
 
Runtime:addEventListener("touch", function( event)  --Add some touch listeners to the player--
        player.x = event.x  --Download the code off http://bit.ly/zK7AiH for all of the sample images--
        player.y = event.y
end )
 
--Draw the grid--
function drawGrid() 
for _,line in ipairs(displayLines) do
        if line and line.removeSelf then
                line:removeSelf()
        end
end
displayLines ={}
displayLines._mode = "kv"
 
 
for p = 1,#_points do
        local point = _points[p]
        local u,v =  originalpoints[p][1], originalpoints[p][2]
        local d = dist( u,v, player.x ,player.y )
        if d < 20 then
                d=20
        end
        
        local f = 6.67300 * (10000 / d^2 )
        if f > 20 then f = 20 end
        local q, w = (u-player.x ), (v-player.y)
        q = (q / d) * f
        w = (w / d) * f
        point[1] = u + q
        point[2] = v + w
end 
 
--Set up some code for the enemies to effect the grid--
for p = 1,#_points do
        local point = _points[p]
        --local u,v =  originalpoints[i][1], originalpoints[i][2]
        for i= 1, #enemies do
                local ex, ey = enemies[i].x, enemies[i].y
                --local d = dist( point[1],point[2], enemy.x ,enemy.y )
                local d = math.sqrt((point[1]-ex)^2+ (point[2]-ey)^2)
                
                if d < 100 then
                        if d < 10 then
                                d=10
                        end
        
                        local f = 6.67300 * (5000 / d^2 )
                        if f > 20 then f = 20 end
                        local q, w = (point[1]-ex ), (point[2]-ey)
                        q = (q / d) * f
                        w = (w / d) * f
                        point[1] = point[1] + q
                        point[2] = point[2]  + w
                end
end
 
 
 
end
 
--Actually draws the lines on the screen--
for _,line in ipairs(lines ) do
        local displayline = display.newLine( 0,0,0,0 )
        for _, point in ipairs( line ) do
                displayline:append( point[1], point[2]  )
        end
        displayline.width = 3
        grid:insert(displayline)
        displayline:setColor( 0,0,255,155) --Change color here--
        --Optional, set up some random colors every frame--
        --displayline:setColor( math.random(0,0),math.random(0,200),math.random(0,200), 200)
        displayLines[ #displayLines + 1 ] = displayline
end
end
 
Runtime:addEventListener( "enterFrame",drawGrid  ) 


Replies

xxxfanta
User offline. Last seen 1 year 14 weeks ago. Offline
Joined: 25 Dec 2010

Looks very good!

Is it possible to comment a bit (at least the math related. Most of it isn't hard to figure out) - or do you have any links for the docs to this effect?

Chris

Ninja Pig Studios
User offline. Last seen 5 weeks 2 days ago. Offline
Joined: 26 Jan 2011

Thanks for the positive feedback. I'll add some comments so that it's easier to understand.

Regards,
Jordan Schuetz
Ninja Pig Studios

xxxfanta
User offline. Last seen 1 year 14 weeks ago. Offline
Joined: 25 Dec 2010

That's nice! :)

I'll check again if there is something I could do to improve the performance!

Chris

Gary Duke
User offline. Last seen 1 year 37 weeks ago. Offline
Joined: 24 Nov 2011

I love the gravity-warp visual. It's used to great effect in the iPhone game, "Orbital".

I would check the code out, but I will add that I find this "tweet to get this" trend here problematic as I, for one, wouldn't want to authorize a twitter app that has access to my data and sends tweets for me.

Why not just paste your code or direct URL here for all to see? More people will see it that way! And more people could offer to help optimize it.

You can still advertise your website and twitter info at the bottom.

Cheers!

Ninja Pig Studios
User offline. Last seen 5 weeks 2 days ago. Offline
Joined: 26 Jan 2011

@Gary Duke,

I attached the code up above like you requested.

@xxxfanta

Your code is now commented up above.

Regards,
Jordan Schuetz
Ninja Pig Studios

nicholasclayg
User offline. Last seen 6 days 6 hours ago. Offline
Joined: 16 May 2011

It's neat. I was doing something like this, but dropped it due to my current project. I wasn't using a grid though, I was using it to hover over terrain.

I did do a level making system I'm using that makes a grid for reference, and prints and stores the X and Y coordinates (based on something @jawhye built) using similar methods to what you are doing here.

This is much better though, my stuff was way longer and complicated, this so much cleaner! I like it. It's cool to see what corona can do and with these effects

-ng

mila.habaer
User offline. Last seen 32 weeks 7 hours ago. Offline
Joined: 13 Aug 2011

Hi Ninja Pig Studios,

I use twitter and got the code, it is cool.

I am a new Indie developer of Corona, Is it possible to use your code in my Corona Game ?(Game is commercial) , if it is possible, what is the condition to use your code?

Regards,

Mila

Ninja Pig Studios
User offline. Last seen 5 weeks 2 days ago. Offline
Joined: 26 Jan 2011

Hi Mila,

Thank you for contacting me. It is okay to use the code in your game or application as long as you give credit to Ninja Pig Studios in the About or Credits page. Also once you finish your game, please send me the link so I can download it and check it out. Appreciate your honesty.

Regards,
Jordan Schuetz
Ninja Pig Studios