Issue with rendering...

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
thegenius
Prole
Posts: 14
Joined: Sat Oct 01, 2011 1:25 am

Issue with rendering...

Post by thegenius »

I made the basis of a simple game, and I have run into a simple problem. In the game, there is a spaceship, it can move around, and shoot lasers, this is where the problem begins. Because the spaceship must rotate, the coordinates translate the spaceship to be center, then puts the center back into the top left corner so the lasers can be rendered, the problem is, that the lasers still appear to be affected by the earlier translation and rotation because the lasers will be off center and rotate with the ship. I know for certain that it's just a rendering problem, because the lasers still appear to get deleted when the numbers representing them leave the boundaries. I'll provide the code so you may be able to help me.

Code: Select all

width = love.graphics.getWidth()
height = love.graphics.getHeight()
xpos, ypos = width/2, height/2
xvel, yvel = 0, 0
rotation = 0
bounce = 0.01
friction = 1.0085
boost = 3000
lRTime = 60
laserReload = 0
laserInitVel = 15
laserNum = 0
lasers = {}

function love.update()
	width = love.graphics.getWidth()
	height = love.graphics.getHeight()

	if love.keyboard.isDown("up") then
		xvel = math.cos(math.rad(rotation))*5
		yvel = math.sin(math.rad(rotation))*5

	end
	if love.keyboard.isDown("down") then
		if boost > 0 then
			xvel = math.cos(math.rad(rotation))*10
			yvel = math.sin(math.rad(rotation))*10
			boost = boost - 1
			else
			xvel = math.cos(math.rad(rotation))*5
			yvel = math.sin(math.rad(rotation))*5
		end
	end
	if love.keyboard.isDown("left") then
		rotation = rotation - 1
	end
	if love.keyboard.isDown("right") then
		rotation = rotation + 1
	end
	if love.keyboard.isDown(" ") then --Making a laser. Once a laser is made, it's given a position and a velocity
		if laserReload <= 0 then
			table.insert(lasers, (--[[["laser.."laserNum] = ]]{xpos+(math.cos(math.rad(rotation))*10),ypos+(math.sin(math.rad(rotation))*10), math.cos(math.rad(rotation))*laserInitVel, math.sin(math.rad(rotation))*laserInitVel}))
			laserReload = laserReload + lRTime
		end
	end
	--next part is simple collision st00f, temporary
	if xpos-10 <= 0 then
		xvel = xvel * -1 * bounce
		xpos = 10
	end
	if xpos+10 >= width then
		xvel = xvel * -1 * bounce
		xpos = width-10
	end
	if ypos-10 <= 0 then
		yvel = yvel * -1 * bounce
		ypos = 10
	end
	if ypos+10 >= height then
		yvel = yvel * -1 * bounce
		ypos = height-10
	end

	if laserReload > 0 then
		laserReload = laserReload - 1
	end

	for a, b in pairs(lasers) do--Keepin' those lasers moving. If they leave the
		b[1] = b[1] + b[3]/10
		b[2] = b[2] + b[4]/10
		if b[1] >= width+10 or b[1] <= 0-10 or b[2] >= height+10 or b[2] <= 0-10 then
			table.remove(lasers, a)
		end
	end

	xpos = xpos + xvel/10
	ypos = ypos + yvel/10
	xvel = xvel/friction
	yvel = yvel/friction
end

function love.draw()
	love.graphics.setColor(255,63,0)
	love.graphics.print("Boost Fuel: "..boost, 10, 100)
	love.graphics.setColor(0,127,255)--From here...
	love.graphics.translate(xpos, ypos)
	love.graphics.rotate(math.rad(rotation+90))
	love.graphics.polygon("fill", 0, -10, 10, 10, 0, 5, -10, 10)
	love.graphics.setColor(255,255,255)
	love.graphics.line(0, 5, 0, -10)--To here, components of the ship are rendered.
	love.graphics.translate(0, 0)
	love.graphics.rotate(math.rad(0))
	for c, d in pairs(lasers) do--Rendering them lasers. Length affected by speed.
		love.graphics.line(d[1]-d[3]/5, d[2]-d[4]/5, d[1]+d[3]/5, d[2]+d[4]/5)
	end
end
Attachments
asteroidsclone.love
(1.03 KiB) Downloaded 90 times
User avatar
TechnoCat
Inner party member
Posts: 1611
Joined: Thu Jul 30, 2009 12:31 am
Location: Milwaukee, WI
Contact:

Re: Issue with rendering...

Post by TechnoCat »

User avatar
thegenius
Prole
Posts: 14
Joined: Sat Oct 01, 2011 1:25 am

Re: Issue with rendering...

Post by thegenius »

Ah, thank you, that's the exact solution to the problem.
User avatar
GijsB
Party member
Posts: 380
Joined: Wed Jul 20, 2011 10:19 pm
Location: Netherlands

Re: Issue with rendering...

Post by GijsB »

You should do something like :

mrad = math.rad

than you can just use it the same way btu with mrad, example = mrad(90)
since you use it alot, the program will run faster than :D

Edit : oh no i forgot you have to make it local D:
it has to be :
local mrad = math.rad
Last edited by GijsB on Mon Oct 03, 2011 7:43 pm, edited 1 time in total.
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Issue with rendering...

Post by bartbes »

It would not run way faster, unless it's a local. (Which it is not in your post.)

EDIT: And of course I mean that premature optimization is evil, stuffs, more stuff, so don't care about those tiny little things for now.
Post Reply

Who is online

Users browsing this forum: No registered users and 4 guests