Page 1 of 1

Shaking love.graphics.drawn objects

Posted: Tue Apr 15, 2014 7:51 pm
by ErvinGamez
Hard to explain, but I'll try nevertheless...

Now, I used the gridlocked player tutorial, and changed it a little bit, but what I'm trying to do, is make the walls (smoothly) shake/move about (like, give it an effect that it is in space), but I tried everything, and nothing worked. Check out the attachment.

Thanks for help, if you still don't get it, tell me.

Re: Shaking love.graphics.drawn objects

Posted: Wed Apr 16, 2014 7:46 am
by ErvinGamez
Anyone?

Or is this literally impossible?

Re: Shaking love.graphics.drawn objects

Posted: Wed Apr 16, 2014 8:30 am
by Jasoco
If you make each wall tile a separate object with its own properties, you could create them all with random shake values and in their update callbacks handle the movement.

If I did it, this is my method:

Code: Select all

blockObject = {}

function blockObject:init(v)
	local _i = {}
	setmetatable(_i, {__index = self})
	_i:reset(v)
	return _i
end
function blockObject:reset(v)
	local v = v or {} -- Prevent potential future "table doesn't exist" errors

	self.x = v.x or 0
	self.y = v.y or 0

	self.shakeOffsetX = v.shakeOffsetX or 0
	self.shakeOffsetY = v.shakeOffsetY or 0

	self.shakeSpeed = v.shakeSpeed or 0

	self.offsetAmountX = v.offsetAmountX or 4 -- How far offset do you want the tile to move
	self.offsetAmountY = v.offsetAmountY or 4 -- from its centered point?

	self.drawX = v.drawX or self.x -- Set up the default position in case shake values are 0
	self.drawY = v.drawY or self.y
end

function blockObject:update(dt)
	if self.shakeSpeed > 0 then
		self.shakeOffsetX = self.shakeOffsetX + dt * (self.shakeSpeed * self.offsetAmountX)
		self.shakeOffsetY = self.shakeOffsetY + dt * (self.shakeSpeed * self.offsetAmountY)

		self.drawX = self.x + math.sin(self.shakeOffsetX)
		self.drawY = self.y + math.sin(self.shakeOffsetY)
	end
end

function blockObject:draw()
	-- Handle drawing the block here
	love.graphics.draw(my image, self.drawX, self.drawY, ...)
end

Code: Select all

-- To create the blocks
block = {}
for y=1, #map do
	block[x] = {}
	for x=1, #map[y] do
		if map[y][x] == 1 then
			block[x][y] = blockObject { 
			                            x = x * 32,
			                            y = y * 32,
			                            shakeOffsetX = math.random() * math.pi,
			                            shakeOffsetY = math.random() * math.pi,
			                            shakeSpeed = whateverYouWant,
			                            ...
		                            }
		end
	end
end

Code: Select all

-- In Update
for y=1, #map do
	for x=1, #map[y] do
		if map[y][x] == 1 then
			block[x][y]:update(dt)
		end
	end
end

Code: Select all

-- In Draw
for y=1, #map do
	for x=1, #map[y] do
		if map[y][x] == 1 then
			block[x][y]:draw()
		end
	end
end
You should give a thread more than just 12 hours before bumping. It's frowned upon to bump too soon. Threads don't fall off the first page easily on this forum. Give it a few days before bumping.

Re: Shaking love.graphics.drawn objects

Posted: Wed Apr 16, 2014 11:02 am
by ErvinGamez
Jasoco wrote:If you make each wall tile a separate object with its own properties, you could create them all with random shake values and in their update callbacks handle the movement.

If I did it, this is my method:

Code: Select all

blockObject = {}

function blockObject:init(v)
	local _i = {}
	setmetatable(_i, {__index = self})
	_i:reset(v)
	return _i
end
function blockObject:reset(v)
	local v = v or {} -- Prevent potential future "table doesn't exist" errors

	self.x = v.x or 0
	self.y = v.y or 0

	self.shakeOffsetX = v.shakeOffsetX or 0
	self.shakeOffsetY = v.shakeOffsetY or 0

	self.shakeSpeed = v.shakeSpeed or 0

	self.offsetAmountX = v.offsetAmountX or 4 -- How far offset do you want the tile to move
	self.offsetAmountY = v.offsetAmountY or 4 -- from its centered point?

	self.drawX = v.drawX or self.x -- Set up the default position in case shake values are 0
	self.drawY = v.drawY or self.y
end

function blockObject:update(dt)
	if self.shakeSpeed > 0 then
		self.shakeOffsetX = self.shakeOffsetX + dt * (self.shakeSpeed * self.offsetAmountX)
		self.shakeOffsetY = self.shakeOffsetY + dt * (self.shakeSpeed * self.offsetAmountY)

		self.drawX = self.x + math.sin(self.shakeOffsetX)
		self.drawY = self.y + math.sin(self.shakeOffsetY)
	end
end

function blockObject:draw()
	-- Handle drawing the block here
	love.graphics.draw(my image, self.drawX, self.drawY, ...)
end

Code: Select all

-- To create the blocks
block = {}
for y=1, #map do
	block[x] = {}
	for x=1, #map[y] do
		if map[y][x] == 1 then
			block[x][y] = blockObject { 
			                            x = x * 32,
			                            y = y * 32,
			                            shakeOffsetX = math.random() * math.pi,
			                            shakeOffsetY = math.random() * math.pi,
			                            shakeSpeed = whateverYouWant,
			                            ...
		                            }
		end
	end
end

Code: Select all

-- In Update
for y=1, #map do
	for x=1, #map[y] do
		if map[y][x] == 1 then
			block[x][y]:update(dt)
		end
	end
end

Code: Select all

-- In Draw
for y=1, #map do
	for x=1, #map[y] do
		if map[y][x] == 1 then
			block[x][y]:draw()
		end
	end
end
You should give a thread more than just 12 hours before bumping. It's frowned upon to bump too soon. Threads don't fall off the first page easily on this forum. Give it a few days before bumping.

Thanks, I'll try this out.
Actually, I did give it more than 12 hours, 7pm to 7am = 12 hours.

Re: Shaking love.graphics.drawn objects

Posted: Wed Apr 16, 2014 11:15 am
by ErvinGamez
Jasoco wrote:If you make each wall tile a separate object with its own properties, you could create them all with random shake values and in their update callbacks handle the movement.

If I did it, this is my method:

Code: Select all

blockObject = {}

function blockObject:init(v)
	local _i = {}
	setmetatable(_i, {__index = self})
	_i:reset(v)
	return _i
end
function blockObject:reset(v)
	local v = v or {} -- Prevent potential future "table doesn't exist" errors

	self.x = v.x or 0
	self.y = v.y or 0

	self.shakeOffsetX = v.shakeOffsetX or 0
	self.shakeOffsetY = v.shakeOffsetY or 0

	self.shakeSpeed = v.shakeSpeed or 0

	self.offsetAmountX = v.offsetAmountX or 4 -- How far offset do you want the tile to move
	self.offsetAmountY = v.offsetAmountY or 4 -- from its centered point?

	self.drawX = v.drawX or self.x -- Set up the default position in case shake values are 0
	self.drawY = v.drawY or self.y
end

function blockObject:update(dt)
	if self.shakeSpeed > 0 then
		self.shakeOffsetX = self.shakeOffsetX + dt * (self.shakeSpeed * self.offsetAmountX)
		self.shakeOffsetY = self.shakeOffsetY + dt * (self.shakeSpeed * self.offsetAmountY)

		self.drawX = self.x + math.sin(self.shakeOffsetX)
		self.drawY = self.y + math.sin(self.shakeOffsetY)
	end
end

function blockObject:draw()
	-- Handle drawing the block here
	love.graphics.draw(my image, self.drawX, self.drawY, ...)
end

Code: Select all

-- To create the blocks
block = {}
for y=1, #map do
	block[x] = {}
	for x=1, #map[y] do
		if map[y][x] == 1 then
			block[x][y] = blockObject { 
			                            x = x * 32,
			                            y = y * 32,
			                            shakeOffsetX = math.random() * math.pi,
			                            shakeOffsetY = math.random() * math.pi,
			                            shakeSpeed = whateverYouWant,
			                            ...
		                            }
		end
	end
end

Code: Select all

-- In Update
for y=1, #map do
	for x=1, #map[y] do
		if map[y][x] == 1 then
			block[x][y]:update(dt)
		end
	end
end

Code: Select all

-- In Draw
for y=1, #map do
	for x=1, #map[y] do
		if map[y][x] == 1 then
			block[x][y]:draw()
		end
	end
end
You should give a thread more than just 12 hours before bumping. It's frowned upon to bump too soon. Threads don't fall off the first page easily on this forum. Give it a few days before bumping.
I have no idea where to put the top 2.

Re: Shaking love.graphics.drawn objects

Posted: Wed Apr 16, 2014 8:07 pm
by Jasoco
The top one should be in its own file.

The second one is in the level creation function.

You can use the same method for anything that should be an object.

Try implementing the code and play around with it. You can make a different object for each tile type and have different things happening in each tile.

Also, 12 hours is still too little. It's generally ground upon. Especially since you can easily edit your post with no problem. Technically I am supposed to give you a warning.