# Rotated coordinates

I have a sprite that rotates as it rolls around and I’m trying to add a bounce functionality that moves it up then back down but when I try to bounce it bounces in the direction it is rotated to. I am trying to get it to always bounce up, no matter how it is rotated. Anyone have any idea how to do this?

Here is some example code of what I have so far:

``````
--# Main
-- Bounce

-- Use this function to perform your initial setup
function setup()
debugea()
createBallImage()
ball = Sprite(ballImg, 0,0, ballImg.width, ballImg.height)
end

function createBallImage()
ballImg = image(100,100)
setContext(ballImg)
fill(255,255,0)
ellipse(50,50, 100, 100)
sprite("Platformer Art:Guy Standing", 50, 50, 75)
setContext()
end

function bounce(obj)
height = 100
hold = 0.1

local m = obj
local savedScale = vec2(1,1)
local savedPos = obj.pos
local size = obj.size
local squash = vec2(1.5, 0.75)
local moveDown = savedPos.y - size.y/2 * (1 - squash.y)

tween(0.15, m, {scale = squash}, tween.easing.quadInOut)
tween(0.15, m, {pos = vec2(savedPos.x, moveDown)}, tween.easing.quadInOut,
function()
local moveUp = savedPos.y + height * 0.9
local unsquash = vec2(0.85, 1.25)

tween(0.15, m, {scale = unsquash}, tween.easing.quadInOut)
tween(0.2, m, {pos = vec2(savedPos.x, moveUp)}, tween.easing.quadOut,
function()
-- Reached top of jump

local holdHeight = savedPos.y + height
tween(hold, m, {pos = vec2(savedPos.x, holdHeight)}, tween.easing.linear,
function()
tween(0.15, m, {scale = savedScale}, tween.easing.quadInOut)
tween(0.15, m, {pos = savedPos}, tween.easing.quadIn)
end)
end )

end )
--
end

-- This function gets called once every frame
function draw()
-- This sets a dark background color
background(40, 40, 50)

-- This sets the line thickness
strokeWidth(5)

ball.angle = ball.angle + 1
pushMatrix()
translate(WIDTH/4, HEIGHT/2)
rotate(ball.angle)
ball:draw()
popMatrix()
end

function touched(touch)
if touch.state == ENDED then
bounce(ball)
end
end

--# Sprite
Sprite = class()

function Sprite:init(img, x, y, width, height)
-- you can accept and set parameters here
self.img = img
self.pos = vec2(x,y)
self.size = vec2(width, 0)
self.size.y = height or width
self.scale = vec2(1,1)
self.angle = 0
self.fill = color(255)
end

function Sprite:draw()
pushStyle()
pushMatrix()
fill(self.fill) noStroke()
sprite(self.img, self.pos.x, self.pos.y, self.size.x * self.scale.x, self.size.y * self.scale.y)
popMatrix()
popStyle()
end
``````

When you say ‘a sprite’ do you mean a physics object?
If you post the code, it would be easier to understand why it doesnt work…

@Jmv38, my apologies, I have added some example code above

I think this does whar you want. See changes:

``````
--# Main
-- Bounce

-- Use this function to perform your initial setup
function setup()
-- debugea()
createBallImage()
ball = Sprite(ballImg, 0,0, ballImg.width, ballImg.height)
end

function createBallImage()
ballImg = image(100,100)
setContext(ballImg)
fill(255,255,0)
ellipse(50,50, 100, 100)
sprite("Platformer Art:Guy Standing", 50, 50, 75)
setContext()
end

function bounce(obj)
height = 100
hold = 0.1

local m = obj
local savedScale = vec2(1,1)
local savedPos = obj.pos
local size = obj.size
local squash = vec2(1.5, 0.75)
local moveDown = savedPos.y - size.y/2 * (1 - squash.y)

tween(0.15, m, {scale = squash}, tween.easing.quadInOut)
tween(0.15, m, {pos = vec2(savedPos.x, moveDown)}, tween.easing.quadInOut,
function()
local moveUp = savedPos.y + height * 0.9
local unsquash = vec2(0.85, 1.25)

tween(0.15, m, {scale = unsquash}, tween.easing.quadInOut)
tween(0.2, m, {pos = vec2(savedPos.x, moveUp)}, tween.easing.quadOut,
function()
-- Reached top of jump

local holdHeight = savedPos.y + height
tween(hold, m, {pos = vec2(savedPos.x, holdHeight)}, tween.easing.linear,
function()
tween(0.15, m, {scale = savedScale}, tween.easing.quadInOut)
tween(0.15, m, {pos = savedPos}, tween.easing.quadIn)
end)
end )

end )
--
end

-- This function gets called once every frame
function draw()
-- This sets a dark background color
background(40, 40, 50)

-- This sets the line thickness
strokeWidth(5)

ball.angle = ball.angle + 1
pushMatrix()
translate(WIDTH/4, HEIGHT/2)
--    rotate(ball.angle)    -- changes
ball:draw()
popMatrix()
end

function touched(touch)
if touch.state == ENDED then
bounce(ball)
end
end

--# Sprite
Sprite = class()

function Sprite:init(img, x, y, width, height)
-- you can accept and set parameters here
self.img = img
self.pos = vec2(x,y)
self.size = vec2(width, 0)
self.size.y = height or width
self.scale = vec2(1,1)
self.angle = 0
self.fill = color(255)
end

function Sprite:draw()
pushStyle()
pushMatrix()
fill(self.fill) noStroke()
-- changes
translate(self.pos.x, self.pos.y)
scale(self.scale.x, self.scale.y)
rotate(ball.angle)
sprite(self.img)
-- changes
popMatrix()
popStyle()
end
``````

ps: Replace ball by self in rotate

@Jmv38, Thanks!