I have multiple objects and multiple containers, My plan is that any object can be dropped on any container, but once a container has an object dropped on it, I want it to be occupied and not allow any more objects on to it. Is there an isOcuppied call or something like that, I cant find any examples of this.
local function onTouch( event )
local t = event.target
local phase = event.phase
if "began" == phase then
-- Make target the top-most object
local parent = t.parent
parent:insert( t )
display.getCurrentStage():setFocus( t )
-- Spurious events can be sent to the target, e.g. the user presses
-- elsewhere on the screen and then moves the finger over the target.
-- To prevent this, we add this flag. Only when it's true will "move"
-- events be sent to the target.
t.isFocus = true
-- Store initial position
t.x0 = event.x - t.x
t.y0 = event.y - t.y
elseif t.isFocus then
if "moved" == phase then
-- Make object move (we subtract t.x0,t.y0 so that moves are
-- relative to initial grab point, rather than object "snapping").
t.xScale = 1.7
t.yScale = 1.7
t.x = event.x - t.x0
t.y = event.y - t.y0
--t1 = t1..t.value
elseif "ended" == phase or "cancelled" == phase then
for i = 1, #posX do
if (((t.x >= ((sizeX/2) + posX[i] - ((2/3) * sizeX))) and (t.y >= ((sizeY/2) + posY - ((1/3) * sizeY))) and (t.x <= ((sizeX/2) + posX[i] + ((2/3) * sizeX))) and (t.y <= ((sizeY/2) + posY + ((1/3) * sizeY)))) or ((t.x >= ((sizeX/2) + posX[i] - ((1/3) * sizeX))) and (t.y >= ((sizeY/2) + posY - ((2/3) * sizeY))) and (t.x <= ((sizeX/2) + posX[i] + ((1/3) * sizeX))) and (t.y <= ((sizeY/2) + posY + ((2/3) * sizeY)))) or ((t.x >= ((sizeX/2) + posX[i] - ((1/2) * sizeX))) and (t.y >= ((sizeY/2) + posY - ((1/2) * sizeY))) and (t.x <= ((sizeX/2) + posX[i] + ((1/2) * sizeX))) and (t.y <= ((sizeY/2) + posY + ((1/2) * sizeY))))) then
t.x, t.y = posX[i] + (sizeX/2), posY + (sizeY/2);
elseif (((t.x >= ((sizeX/2) + posX1[i] - ((2/3) * sizeX))) and (t.y >= ((sizeY/2) + posY1 - ((1/3) * sizeY))) and (t.x <= ((sizeX/2) + posX1[i] + ((2/3) * sizeX))) and (t.y <= ((sizeY/2) + posY1 + ((1/3) * sizeY)))) or ((t.x >= ((sizeX/2) + posX1[i] - ((1/3) * sizeX))) and (t.y >= ((sizeY/2) + posY1 - ((2/3) * sizeY))) and (t.x <= ((sizeX/2) + posX1[i] + ((1/3) * sizeX))) and (t.y <= ((sizeY/2) + posY1 + ((2/3) * sizeY)))) or ((t.x >= ((sizeX/2) + posX1[i] - ((1/2) * sizeX))) and (t.y >= ((sizeY/2) + posY1 - ((1/2) * sizeY))) and (t.x <= ((sizeX/2) + posX1[i] + ((1/2) * sizeX))) and (t.y <= ((sizeY/2) + posY1 + ((1/2) * sizeY))))) then
t.x, t.y = posX1[i] + (sizeX/2), posY1 + (sizeY/2);
t2[i] = t.value
--t1 = t1..t.value
-- elseif (((t.x >= ((sizeX/2) + posX2 - ((2/3) * sizeX))) and (t.y >= ((sizeY/2) + posY2 - ((1/3) * sizeY))) and (t.x <= ((sizeX/2) + posX2 + ((2/3) * sizeX))) and (t.y <= ((sizeY/2) + posY2 + ((1/3) * sizeY)))) or ((t.x >= ((sizeX/2) + posX2 - ((1/3) * sizeX))) and (t.y >= ((sizeY/2) + posY2 - ((2/3) * sizeY))) and (t.x <= ((sizeX/2) + posX2 + ((1/3) * sizeX))) and (t.y <= ((sizeY/2) + posY2 + ((2/3) * sizeY)))) or ((t.x >= ((sizeX/2) + posX2 - ((1/2) * sizeX))) and (t.y >= ((sizeY/2) + posY2 - ((1/2) * sizeY))) and (t.x <= ((sizeX/2) + posX2 + ((1/2) * sizeX))) and (t.y <= ((sizeY/2) + posY2 + ((1/2) * sizeY))))) then
-- t.x, t.y = posX2 + (sizeX/2), posY2 + (sizeY/2);
end
end
t1 = t1..t.value
t.xScale = 1
t.yScale = 1
--print(tile.label)
display.getCurrentStage():setFocus( nil )
t.isFocus = false
--n = n + 1
--value = " "
end
end
Here is my code as it stands, Also Im I have a value attached to each tile e.g tile a = "A", currently I'm just appending a string each time a tile is moved, but I want the string or maybe use a table to only be appended when the tile has been put in a container. For example if one letter is in the wrong place and I move it to the right place I have touched it twice so my final string will be wrong
Why not set a value on your container:
myContainer.isOccupied = false
Then in your event handler, when every you get to your ended phase you can simply do a check:
if not myContainer.isOccupied then
-- code to drop
myContainer.isOccupied = true
end
Related
In my player script. I called a function to check is if a tile is collided with the player.
The basic tile collisions has been made, but I got some problems.
When the player jump while colliding with the wall, the player stop.
When the player is falling while colliding with the wall, the player stop.
I think the problem is in the Y collision checking part, but I can't figure out how to solve it.
--------------------- Collision Check ----------------------
local box = Vt2:new(self.pos.x + self.vel.x * dt * self.dirXTemp,
self.pos.y + self.vel.y * dt)
for i = 1, #collTile do
local tile = collTile[i]
local isColl = BdBoxCollision(box, self.size, tile.pos, tile.size)
if
isColl[1] == true and
isColl[2] == true and
isColl[3] == true and
isColl[4] == true then
local isOnWall = false
if self.pos.y + self.size.y > tile.pos.y then -- X Collision
if box.x + self.size.x / 2 < tile.pos.x + tile.size.x / 2 then
if box.x + self.size.x > tile.pos.x then
if self.dirX == 1 then
self.vel.x = 0
isOnWall = true
end
end
end
if box.x + self.size.x / 2 > tile.pos.x + tile.size.x / 2 then
if box.x < tile.pos.x + tile.size.x then
if self.dirX == -1 then
self.vel.x = 0
isOnWall = true
end
end
end
end
if box.y + self.size.y / 2 < tile.pos.y + tile.size.y / 2 then -- Y Collision
if box.y + self.size.y > tile.pos.y and
self.pos.y + self.size.y < tile.pos.y + 8 then
if isOnWall == false then
self.pos.y = tile.pos.y - self.size.y
self.vel.y = 0
end
end
elseif box.y + self.size.y / 2 > tile.pos.y + tile.size.y / 2 then
if box.y < tile.pos.y + tile.size.y then
self.vel.y = self.gravity
end
end
end
end
::skip::
self.pos.x = self.pos.x + self.vel.x * dt
self.pos.y = self.pos.y + self.vel.y * dt
Is there some built in function to print expressions in a readable way? For example, this expression;
<Expression "( - (x(2) * (2 * (x(0) * P(0,0)) + (x(1) * P(1,0)) + (x(1) * P(0,1)) + (x(2) * P(2,0)) + (x(2) * P(0,2)) + (x(3) * P(3,0)) + (x(3) * P(0,3)) + 2 * (lambda(0) * Q(0,0)) + 2 * (lambda(1) * Q(0,1)))) - (x(3) * ((x(0) * P(1,0)) + (x(0) * P(0,1)) + 2 * (x(1) * P(1,1)) + (x(2) * P(2,1)) + (x(2) * P(1,2)) + (x(3) * P(3,1)) + (x(3) * P(1,3)) + 2 * (lambda(0) * Q(1,0)) + 2 * (lambda(1) * Q(1,1)))) - ((9.8000000000000007 * x(1) + (x(0) * K(0)) + (x(1) * K(1)) + (x(2) * K(2)) + (x(3) * K(3))) * ((x(0) * P(2,0)) + (x(0) * P(0,2)) + (x(1) * P(2,1)) + (x(1) * P(1,2)) + 2 * (x(2) * P(2,2)) + (x(3) * P(3,2)) + (x(3) * P(2,3)) + 2 * (lambda(0) * Q(2,0)) + 2 * (lambda(1) * Q(2,1)))) - ((19.600000000000001 * x(1) + lambda(0) - lambda(1) + (x(0) * K(0)) + (x(1) * K(1)) + (x(2) * K(2)) + (x(3) * K(3))) * ((x(0) * P(3,0)) + (x(0) * P(0,3)) + (x(1) * P(3,1)) + (x(1) * P(1,3)) + (x(2) * P(3,2)) + (x(2) * P(2,3)) + 2 * (x(3) * P(3,3)) + 2 * (lambda(0) * Q(3,0)) + 2 * (lambda(1) * Q(3,1)))))">
Would be a lot easier to see if it was organized by the matrix and variable dot products.
If you're working in a notebook workflow, then we have a ToLatex method.
My favorite way to use it is, e.g.:
from IPython.display import Markdown, display
from pydrake.all import ToLatex, MultibodyPlant, Parser
from underactuated import FindResource, ManipulatorEquations
plant = MultibodyPlant(time_step=0)
parser = Parser(plant)
parser.AddModelFromFile(FindResource("models/double_pendulum.urdf"))
plant.Finalize()
# Evaluate the dynamics numerically
q = [0.1, 0.1]
v = [1, 1]
(M, Cv, tauG, B, tauExt) = ManipulatorDynamics(plant, q, v)
display(Markdown("$M = " + ToLatex(M, precision=2) + "$"))
display(Markdown("$Cv = " + ToLatex(Cv, precision=2) + "$"))
display(Markdown("$\\tau_G = " + ToLatex(tauG, precision=2) + "$"))
display(Markdown("$B = " + ToLatex(B, precision=2) + "$"))
display(Markdown("$\\tau_{ext} = " + ToLatex(tauExt, precision=2) + "$"))
as seen in the intro chapter notebook from my underactuated course notes: https://deepnote.com/project/52e7e101-429f-4aef-a373-e4cca7980cfe/%2Fintro.ipynb
I am solving a convex problem using cvxpy. The constraints are rather simple, there are 3 variables, but we could eliminate one. The objective is convex and involves the entropy and logarithm. The solution is correct, in the sense that the variables have the expected values. Howver the objective value should be around -1.06, but it is infinite. Is there a bug with evaluating involved expressions ?
#!/usr/bin/env python3
import cvxpy as cx
import numpy as np
from math import log
def entr(x):
return -x * log(x)
def check_obj(a, b, c):
return -entr(2.0) + -2.0 * log(2.0) + -entr(1.0 + a) + -1.0 + a * log(2.0) + -entr(2.0 + a) -2.0 + a * log(1.0) -entr(1.0 + a + b + c) + -1.0 + a + b + c * log(2.0) + -entr(2.0) + -2.0 * log(2.0) + -entr(1.0 + b) -1.0 + b * log(2.0) + -entr(2.0 + b) + -2.0 + b * log(1.0) -entr(1.0 + b + a + c) -1.0 + b + a + c * log(2.0)
a = cx.Variable(name='a')
b = cx.Variable(name='b')
c = cx.Variable(name='c')
obj = -cx.entr(2.0) + -2.0 * cx.log(2.0) + -cx.entr(1.0 + a) + -1.0 + a * cx.log(2.0) + -cx.entr(2.0 + a) -2.0 + a * cx.log(1.0) -cx.entr(1.0 + a + b + c) + -1.0 + a + b + c * cx.log(2.0) + -cx.entr(2.0) + -2.0 * cx.log(2.0) + -cx.entr(1.0 + b) -1.0 + b * cx.log(2.0) + -cx.entr(2.0 + b) + -2.0 + b * cx.log(1.0) -cx.entr(1.0 + b + a + c) -1.0 + b + a + c * cx.log(2.0)
p = cx.Problem(cx.Minimize(obj), [0 <= a, 0<= b, 0 <= c, a + b + c == 1])
p.solve()
# should be 'optimal' and indeed it is
print(p.status)
# the following two values should be the same, but p.value is infinite and should be around -1.06
print(p.value)
print(check_obj(a.value, b.value, c.value))
It looks like a bug in the entropy atom. I fixed it and made a pull request here. It is merged now. If you run your code with the latest cvxpy from the master branch it should give correct results.
function GetDamage(spell, unit)
if spell == _Q and (isReady(_Q) or qActive) and not HasItem(3025) and not HasItem(3100) and not HasItem(3057) and not HasItem(3078) then
return myHero:CalcDamage(unit, ((((myHero:GetSpellData(_Q).level * 20) + 10) + myHero.totalDamage) + qStacks))
elseif spell == _Q and (isReady(_Q) or qActive) and (HasItem(3057) or sheenActive) then
return myHero:CalcDamage(unit, myHero.damage) + myHero:CalcDamage(unit, ((((myHero:GetSpellData(_Q).level * 20) + 10) + myHero.totalDamage) + qStacks))
elseif spell == _Q and (isReady(_Q) or qActive) and (HasItem(3025) or fActive) then
return myHero:CalcDamage(unit, myHero.damage) + myHero:CalcDamage(unit, ((((myHero:GetSpellData(_Q).level * 20) + 10) + myHero.totalDamage) + qStacks))
elseif spell == _Q and (isReady(_Q) or qActive) and (HasItem(3100) or lActive) then
return myHero:CalcMagicDamage(unit, ((myHero.damage * 0.75) + (myHero.ap * 0.5))) + myHero:CalcDamage(unit, ((((myHero:GetSpellData(_Q).level * 20) + 10) + myHero.totalDamage) + qStacks))
elseif spell == _Q and (isReady(_Q) or qActive) and (HasItem(3078) or tActive) then
return myHero:CalcDamage(unit, (myHero.damage * 2)) + myHero:CalcDamage(unit, ((((myHero:GetSpellData(_Q).level * 20) + 10) + myHero.totalDamage) + qStacks))
elseif spell == _E and isReady(_E) then
return myHero:CalcMagicDamage(unit, (((myHero:GetSpellData(_E).level * 40) + 15) + (myHero.ap * 0.6)))
else
return 0
end
end
this is my code to return x spell damage to x enemy, however while it works as intended it seems inefficient and ugly also I would like it to work a little differently and I'm not sure how to code it as such.
What I would like it to do is if I do for example GetDamage(all) or something similiar I would like it to return total damage I can do given isReady(spell) returns true ie if spell q and e are ready it would return the total of q and e only or if all were ready then would return all, additionally if I only need to know r's damage I could still just do GetDamage(_R).
Is there a cleaner way to do this using a table or some more efficient manner to get the results I need? Because currently using GetDamage(spell) + GetDamage(spell2) + GetDamage(spell3) etc looks like very bad coding.
Introduce new spell "ALL"
function GetDamage(spell, unit)
local result = 0
if (spell == "ALL" or spell == _Q) and (isReady(_Q) or qActive) then
local bonus = 0
if (HasItem(3057) or sheenActive) then
bonus = myHero:CalcDamage(unit, myHero.damage)
elseif (HasItem(3025) or fActive) then
bonus = myHero:CalcDamage(unit, myHero.damage)
elseif (HasItem(3100) or lActive) then
bonus = myHero:CalcMagicDamage(unit, ((myHero.damage * 0.75) + (myHero.ap * 0.5)))
elseif (HasItem(3078) or tActive) then
bonus = myHero:CalcDamage(unit, (myHero.damage * 2))
end
result = result + bonus + myHero:CalcDamage(unit, ((((myHero:GetSpellData(_Q).level * 20) + 10) + myHero.totalDamage) + qStacks))
end
if (spell == "ALL" or spell == _E) and isReady(_E) then
result = result + myHero:CalcMagicDamage(unit, (((myHero:GetSpellData(_E).level * 40) + 15) + (myHero.ap * 0.6)))
end
return result
end
Usage examples:
dmg = GetDamage("ALL", unit)
dmg = GetDamage(_Q, unit)
dmg = GetDamage(_E, unit)
EDIT:
There is another way of implementing the same by using a table with spells as keys and functions as values:
spell_dmg_func = {
[_Q] =
function(unit)
if (isReady(_Q) or qActive) then
local bonus = 0
if (HasItem(3057) or sheenActive) then
bonus = myHero:CalcDamage(unit, myHero.damage)
elseif (HasItem(3025) or fActive) then
bonus = myHero:CalcDamage(unit, myHero.damage)
elseif (HasItem(3100) or lActive) then
bonus = myHero:CalcMagicDamage(unit, ((myHero.damage * 0.75) + (myHero.ap * 0.5)))
elseif (HasItem(3078) or tActive) then
bonus = myHero:CalcDamage(unit, (myHero.damage * 2))
end
return bonus + myHero:CalcDamage(unit, ((((myHero:GetSpellData(_Q).level * 20) + 10) + myHero.totalDamage) + qStacks))
end
end,
[_E] =
function(unit)
if isReady(_E) then
return myHero:CalcMagicDamage(unit, (((myHero:GetSpellData(_E).level * 40) + 15) + (myHero.ap * 0.6)))
end
end,
}
function GetDamage(spell, unit)
if spells == "ALL" then
local sum = 0
for spell, func in pairs(spell_dmg_func) do
sum = sum + (func(unit) or 0)
end
return sum
else
return spell_dmg_func[spell](unit) or 0
end
end
I am working on this for homework, and I have most of this done my problem is there is something wrong in one of the for loops as the display I get is only one line when it should be a line for every value in the range 50,000 - 60,000 with a step value of 50.
def computeTax(status, taxableIncome):
print("Taxable Income/" + "\t" + "Single/" + "\t" + "Married Joint/" + \
"\t" + "Married Separate/" + " " + "Head of Household")
for taxableIncome in range(50000, 60001, 50):
for status in range(1, 5, 1):
if (status == 1): #tax calculation for single person
tax1 = 8350 * .10 + (33950 - 8350) * 0.15 + \
(taxableIncome - 33950) * 0.25
tax1 = round(tax1,0)
if (status == 2): #tax calculation for married file jointly
tax2 = 16700 * .10 + (67900 - 16700) * 0.15 + \
(taxableIncome - 67900) * 0.15
tax2 = round(tax2,0)
if (status == 3): #tax calculation for married file separately
tax3 = 8350 * .10 + (33950 - 8350) * 0.15 + \
(taxableIncome - 33950) * 0.25
tax3 = round(tax3,0)
if (status == 4): #tax calculation for head of household
tax4 = 11950 * .10 + (45500 - 11950) * 0.15 + \
(taxableIncome - 45500) * 0.25
tax4 = round(tax4,0)
print(str(tax1) + "\t" + str(tax2) + "\t" + str(tax3) + "\t" + str(tax4))
return (status, taxableIncome)
computeTax(range(1, 5, 1),range(50000, 60001, 50))
I'm not familiar with python, but do you have to define code blocks like you would with PHP?
EG
if (status == 1): #tax calculation for single person
{
tax1 = 8350 * .10 + (33950 - 8350) * 0.15 + \
(taxableIncome - 33950) * 0.25
tax1 = round(tax1,0)
}
Like I said; not familiar with Python, but I thought I might have an answer for you; if I'm completely off then sorry and good luck.