Hint to improve my code - part 2 (SOLVED)

Hello guys

I’m still studying Defold and i finished a new mission. So it is time to ask for help to improve my code or Defold usage.

If you dont know the first post click here to visit it.

Project Configuration:
info about my project:
Window: 540x435
Ball Objetc: 128x128

Mission
Do a ball move through the canvas from any direction to any direction, bouncing when touch any of four edges of canvas.

My Game object script. (it is working. but im sure that can be improved) thank you guys!

Gif of project:

Game Object Code

local WorldWidth = tonumber(sys.get_config("display.width"))
local WorldHeigth = tonumber(sys.get_config("display.height"))

function init(self)
    msg.post(".", "acquire_input_focus")
    self.speed = 360
    self.angle = 360
    self.myWidth  = 64
 	self.myHeight = 64

end

local function atRightEdge(self)
	local p = go.get_position()
	local worldRightLimit = WorldWidth -  self.myWidth/2
	if p.x >= worldRightLimit then 
		return true
	end
	return false
end

local function atLeftEdge(self)
	local p = go.get_position()
	local worldLeftLimit = self.myWidth/2
	if p.x <= worldLeftLimit then 
		return true
	end
	return false
end

local function atTopEdge(self)
	local p = go.get_position()
	local worldTopLimit = WorldHeigth -self.myHeight/2
	if p.y >= worldTopLimit then 
		return true
	end
	return false
end

local function atBotEdge(self)
	local p = go.get_position()
	local worldTopLimit = self.myHeight/2
	if p.y <= worldTopLimit then 
		return true
	end
	return false
end

local function changeDirection(self)
	if atRightEdge(self) or atLeftEdge(self)  then 
    	self.speed = self.speed * -1
    	
    end
    if atTopEdge(self) or atBotEdge(self) then
    	self.angle = self.angle * -1
    end
end 

local function move(self,dt)
	local p = go.get_position()
    p.x = p.x + self.speed * dt
    p.y = p.y + self.angle * dt
    
    go.set_position(p)
    
end

function update(self, dt)
    move(self, dt)
    changeDirection(self)
end

I got a bit confused by the speed and angle. In your code speed is the horizontal movement and angle is the vertical movement. I expected angle to actually describe an angle of travel for the ball at a defined speed.

The four functions to detect edge collisions is in my book a clear case of DRY (don’t repeat yourself). For every function you make a call to go.get_position(). You could also reduce the return value from:

if p.x >= worldRightLimit then 
	return true
end
return false

to:

return p.x >= worldRightLimit

Personally I’d probably cut the whole script down to:

local W = tonumber(sys.get_config("display.width"))
local H = tonumber(sys.get_config("display.height"))

function init(self)
	self.direction = vmath.vector3(1, 1, 0)
	self.speed = 200
	self.size = go.get("#sprite", "size")
end

function update(self, dt)
	local p = go.get_position()
	p = p + (self.direction * self.speed * dt)
	go.set_position(p)

	-- left or right edge?
	if p.x <= (self.size.x / 2) or p.x >= (W - self.size.x / 2) then
		self.direction.x = -self.direction.x
	end
	-- top or bottom edge?
	if p.y <= (self.size.y / 2) or p.y >= (H - self.size.y / 2) then
		self.direction.y = -self.direction.y
	end
end

yeah you are right. Sorry about that. when i have started to write this scrip i was considering use angle to change the ball direction. but after many unsuccessful tests I handled to did it using Y values. but i had forget to update the variable name.

This is a super logic hint. Thankyou.

Did you write a Defold Book? Please share the link.

wow a super hint. with this i can dynamically get sprite size values. but there is a problem.

If I change the ‘scale’ at Game Object [go.get("#sprite", “size”)] continues returning the sprite size (image size) and not the the image size updated by scale values. Is there a way to get correct image size values (after changed the scale value on Game Object)?

Thank you for your Help.

I didn’t mean it literaly. It’s an idiom: http://idioms.thefreedictionary.com/in+my+book

@Pkeod on the other hand started writing a book on Defold but it is not finished. Any update on the book btw @Pkeod?

local sprite_size = go.get("#sprite", "size")
local world_scale = go.get_world_scale()
local scaled_size = vmath.vector3(sprite_size.x * world_scale.x, sprite_size.y * world_scale.y, sprite_size.z * world_scale.z)
1 Like

Haha. thanks for this new english expression. Was fun. :smiley:

This instruction didn’t work to get the GO scale, but with your hint I searched at API and find go.get_scale(), it is not the same thing but works. because i’m using the same scale to X and Y.

Thanks for all. Now i’m going to study Factories component and Detect Collisions. See you.

It’s in the API, but I see now that it returns the uniform scale. I’m sorry about that. In the case of get_world_scale() my example should have been:

local sprite_size = go.get("#sprite", "size")
local world_scale = go.get_world_scale()
local scaled_size = sprite_size * world_scale
1 Like