Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Rotate to face direction — Gideros Forum

Rotate to face direction

sioncosionco Member
edited June 2012 in General questions
Hi, Im new here. I'm trying to create an overhead shooter.
A question, in my game I have loaded a tiled map (thanks to atilim's post) , and created a player image, which moves to wherever the mouse clicks. However, I'm unsure of how to get the players image to rotate to look in the direction of the target. What I have tried is below, it just makes him 'wobble'
 
local tilemap = TileMapMultiple.new("level1.lua")
 
stage:addChild(tilemap)
 
local qatan2 = math.atan2
local qsin = math.sin
local qcos = math.cos
local player1 = Bitmap.new(Texture.new("shooterRambo.png"))
 
player1:setAnchorPoint(0.5,0.5)
player1:setRotation(0)
player1.x = 100
player1.y = 100
player1.speed  =1
player1.targetX = 100
player1.targetY = 100
 
stage:addChild(player1)
 
function onEnterFrame(event)
	local x = player1:getX()
	local y = player1:getY()
	y = y + (qsin(qatan2(player1.targetY - y, player1.targetX - x))*1);
    x = x + (qcos(qatan2(player1.targetY - y, player1.targetX - x))*1);
 
	player1:setX(x)
	player1:setY(y)
	--This is what I have tried
	player1:setRotation(qatan2(player1.targetY - y, player1.targetX - x))
 
end
 
function onMouseDown(event)
	player1.targetX = event.x
	player1.targetY = event.y
end
 
stage:addEventListener(Event.MOUSE_DOWN, onMouseDown)
stage:addEventListener(Event.ENTER_FRAME, onEnterFrame)
Many thanks
Tagged:

Comments

  • evsevs Member
    @sionco

    You need...
     
    player1:setRotation(qatan2(player1.targetY - y, player1.targetX - x) * 180 * 7 / 22)
    cheers

    evs

  • sioncosionco Member
    ah, ok thank you
  • sioncosionco Member
    Hi, I've been playing around with this and I just can't figure out how I would change this to move the map and not the player.
    I've tried all sorts of things, and have had various results from the player and map moving to far in

    I've tried this:
    player1:setX(x)
    player1:setY(y)
    tilemap:setX(-x)
    tilemap:setY(-y)
    but it moves the map and player in each direction to far, I understand that I have to not change the players x and y, and keep him in the center of the screen, but I'm unsure of how.
    I've also tried this;
    player1:setX(application:getContentWidth()*0.5)
    player1:setY(application:getContentHeight()*0.5)
    tilemap:setX(-x)
    tilemap:setY(-y)

    but it doesn't really move the man.

    any help would be gratefully appreciated, as I'm learning this new tool
  • Your correct in that you want to "fix" your man in the centre of the screen, you then want to set the "camera" position so that your current "view" is in the correct place in the world.

    Given the way the gideros tilemap stuff works when you set the tilemap to 0,0 it draws the top left corner of the map in the top left corner of the screen and adjusting the x to make it negative will "scroll" the map to the left, this seems a little counter intuitive as your tilemap x coord goes from 0 (extreme left) to -(world_width-screen_width) (extreme right) however it's fairly easy to create a mapping from a "world" coordinate system to a "screen" coordinate system.

    It might be easier to post a simple example from your code and let us have a look
    WhiteTree Games - Home, home on the web, where the bits and bytes they do play!
    #MakeABetterGame! "Never give up, Never NEVER give up!" - Winston Churchill
  • sioncosionco Member
    edited June 2012
    Hi, thanks for the response. I think I see the problem, in that I haven't got a camera.
    I'll get searching for some examples.

    anway here's my code
    player1:setAnchorPoint(0.5,0.5)
    player1:setRotation(0)
    player1.x = 100
    player1.y = 0
    player1.speed  =1
    player1.targetX = 100
    player1.targetY = 100
     
    stage:addChild(player1)
     
    function onEnterFrame(event)
    	local x = player1:getX()
    	local y = player1:getY()
    	local mapX = tilemap:getX()
    	local mapY = tilemap:getY()
     
    	mapY = mapY + (qsin(qatan2(player1.targetY - mapY, player1.targetX - mapX))*1);
        mapX = mapX + (qcos(qatan2(player1.targetY - mapY, player1.targetX - mapX))*1);
     
    	if x > player1.targetX - 2 and x < player1.targetX +2 and y > player1.targetY - 2 and y < player1.targetY +2 then
    			x = player1.targetX
    			y = player1.targetY
    	else
    		player1:setRotation(qatan2(player1.targetY - y, player1.targetX - x) * 180 * 7 / 22)
     
    	end
    	if x > screenW-64 then
    		offset = offset+1
    	end
     
    	player1:setX(application:getContentWidth()*0.5)
    	player1:setY(application:getContentHeight()*0.5)
    	tilemap:setX(mapX)
    	tilemap:setY(mapY)
     
     
    end
     
    function onMouseDown(event)
    	player1.targetX = event.x-offsetX
    	player1.targetY = event.y-offsetY
    end
     
    function onKeyDown(event)
     
    end
     
    stage:addEventListener(Event.MOUSE_DOWN, onMouseDown)
    stage:addEventListener(Event.ENTER_FRAME, onEnterFrame)
    and the tilemapmultiple taken from
    http://www.giderosmobile.com/forum/discussion/comment/5546#Comment_5546
    TiledMap = Core.class(Sprite)
     
    local map
     
    function TiledMap:init(filename)
     
    	map = loadfile(filename)()
     
    	local tileset = map.tilesets[1]
    	tileset.sizex = math.floor((tileset.imagewidth - tileset.margin + tileset.spacing) / (tileset.tilewidth + tileset.spacing))
    	tileset.sizey = math.floor((tileset.imageheight - tileset.margin + tileset.spacing) / (tileset.tileheight + tileset.spacing))	
    	tileset.texture = Texture.new(tileset.image)
     
    	local layer = map.layers[1]
     
    	local tilemap = TileMap.new(layer.width, 
    								layer.height,
    								tileset.texture,
    								tileset.tilewidth,
    								tileset.tileheight,
    								tileset.spacing,
    								tileset.spacing,
    								tileset.margin,
    								tileset.margin,
    								map.tilewidth,
    								map.tileheight)
     
    	for y=1,layer.height do
    		for x=1,layer.width do
    			local i = x + (y - 1) * layer.width
    			local gid = layer.data[i]
    			if gid ~= 0 then
    				local tx = (gid - tileset.firstgid) % tileset.sizex + 1
    				local ty = math.floor((gid - tileset.firstgid) / tileset.sizex) + 1
    				tilemap:setTile(x, y, tx, ty)
    			end
    		end
    	end
     
    	tilemap:setAlpha(layer.opacity)
    	self:addChild(tilemap)
     
    end
     
    function TiledMap:getLayer(name)
     
    	-- if we have a name parameter and it's a string
    	if name and type(name) == 'string' then
    		-- search for name
    		for index = 1, #map.layers do
    			-- if found
    			if map.layers[index].name == name then
    				-- return it
    				return map.layers[index]
     
    			end
     
    		end
     
    	end
     
    end
     
    function TiledMap:getProperty(item, name)
     
    	-- if we have an item and 
    	if item and name and type(item) == 'table' and type(name) == 'string' then
     
    		for k, v in pairs(item.properties) do 
     
    			if k == name then  
     
    				return v
     
    			end	
     
    		end
     
    	end
     
    end
Sign In or Register to comment.