Not having any luck in the Corona forums, so I thought I'd try here.
I'm simply trying to create an object with a pivot joint. Seems simple but it's just not working.
I just can't figure out how to add bodies to the physics system if those bodies are part of an object that is created in a separate file (class). Hope someone can help - been struggling for weeks on this.
Here is my code:
main.lua:
local JointedObj = require("JointedObj")
local physics = require("physics")
physics.start()
local o = JointedObj.new()
o.x = 50
o.y = 200
local wall = display.newRect( 350, 10, 50, 300 )
physics.addBody ( wall, "static", {density=1, friction=1, bounce=.5})
local floor = display.newRect( 0, 300, 400, 10 )
physics.addBody ( floor, "static", {density=1, friction=1, bounce=.5})
--toss the object against the wall
o:toss(120, -160, o.x, o.y)
JointedObj.lua:
module(..., package.seeall)
--constructor
function new()
local obj = display.newGroup()
local red = display.newImageRect( "images/red.png", 27, 18 )
local blue = display.newImageRect( "images/blue.png", 11, 9 )
blue.x = -16
obj:insert(red)
obj:insert(blue)
function obj:toss(xForce, yForce, xPos, yPos )
--THIS WORKS, BUT OBVIOUSLY THE OBJECT HAS NO JOINTS
--physics.addBody( obj, "dynamic", {density=1, friction=1, bounce=0.3} )
--obj:applyForce( xForce, yForce, xPos, yPos )
--THIS IS WHAT I WANT TO DO. AS-IS, THE OBJECT JUST FALLS THROUGH EVERYTHING
physics.addBody( red, {density=1, friction=1, bounce=0.3} )
physics.addBody( blue, {density=1, friction=1, bounce=0.3} )
myJoint = physics.newJoint( "pivot", red, blue, 0,0 )
myJoint.isLimitEnabled = true
myJoint:setRotationLimits( -30, 30 )
--obj:applyForce( xForce, yForce, xPos, yPos ) --THIS THROWS A NIL ERROR IF UNCOMMENTED
end
return obj;
end
Physics don't work between groups, only within a group. That's done on purpose to allow for moving cameras done using groups. Refer to the Egg Breaker demo to see what I mean. The entire scene is in a group that moves around, but objects within the group don't react to the group moving.
Incidentally, the reason that last line throws an error is because you can only use applyForce on a physics body and you haven't set a physics body on "obj", only on "red" and "blue".
Related
I am working on creating a trend indicator in tradingview to track which way the trend is going. Specifically, I want a variable that will stay the same over days, but when a certain condition is met it will change. It seems like it should be simple to do, but everytime I try I get thrown into a never ending loop and I can't seem to wrap my head around it. Variable "Trend"
///Condition
pos = close > open
neg = close < open
pos_cond = pos and pos[1]
neg_cond = neg and neg[1]
///Variables to keep track of trend
Trend = iff(***pos_cond or neg_cond not met***, Trend[1], Trend + real_trend)
trend_change_neg = iff(pos_cond, 1, 0)
trend_change_pos = iff(neg_cond, -1, 0)
real_trend = trend_change_neg + trend_change_pos
Trend = iff(Trend > 2, 2, iff(Trend < -2, -2, Trend))
/////////plots
plotshape(Trend > 0, color = color.green, location = location.top, style = shape.square, title="TrendLong")
plotshape( Trend == 0, color = color.yellow, location = location.top, style = shape.square, title = "TrendNeutral")
plotshape( Trend < 0, color = color.red, location = location.top, style = shape.square, title = "TrendShort")
So basically what I want to do is keep a running total for Trend where each time there are 2 consecutive candles against the trend it will switch to neutral, but as the trend continues to move in 1 direction it can build back up to +-2 (This was we are never more than 2 "pullbacks" away from neutral. I've racked my brain over this for days now, but if anyone has any ideas any help would be appreciated.
You need to use var.
Example:
var a = 0
a:=close>open?1:0
https://www.tradingview.com/pine-script-docs/en/v4/language/Expressions_declarations_and_statements.html
I'm trying to make a mesh of a piece of beam with stirrup and bars, but I'm having some trouble with stirrup, it is inside the main domain, and I do not know how to solve it. I'm attaching the .geo file, hoping someone could help. Maybe there are other way to mesh it, I do not know.
SetFactory("OpenCASCADE");
// Input
Rectangle(1) = {0, 0, 0, 300, 300, 0};
Disk(2) = {50, 50, 0, 10, 10};
Disk(3) = {50,250,0,10,10};
Disk(4) = {250,250,0,10,10};
Disk(5) = {250,50,0,10,10};
Rectangle(6) = {30,30,146,240,240,10};
Rectangle(7) = {40,40,146,220,220,10};
// Start Operations
s() = BooleanFragments{ Surface{1}; Delete; }{ Surface{2,3,4,5}; Delete;};
ext() = Extrude{0,0,300} {Surface{s()}; Layers{10}; Recombine;};
st() = BooleanFragments{ Surface{6}; Delete;}{Surface{7}; Delete;};
Recursive Delete {Surface{7}; }
Extrude{0,0,10} {Surface{22}; Layers{10}; Recombine;}
BooleanFragments{ Volume{5}; Delete;}{Volume{6}; Delete;}
// Mesh Options all elements needs to be Hexa
Mesh.RecombineAll = 2;
Not a complete answer; however, I think I identified the problem that probably causes the major troubles:
The circular extrusions (cylinders) touch the stirrup exactly at the vertices, thus creating complications to OpenCASCADE-based BooleanFragments operation.
The following code:
SetFactory("OpenCASCADE");
// Input
Rectangle(1) = {0, 0, 0, 300, 300, 0};
Disk(2) = {52, 52, 0, 10, 10};
Disk(3) = {52,248,0,10,10};
Disk(4) = {248,248,0,10,10};
Disk(5) = {248,52,0,10,10};
Rectangle(6) = {30,30,146,240,240,10};
Rectangle(7) = {40,40,146,220,220,10};
// Start Operations
s() = BooleanFragments{ Surface{1}; Delete; }{ Surface{2,3,4,5}; Delete;};
ext() = Extrude{0,0,300} {Surface{s()}; Layers{10}; Recombine;};
st() = BooleanFragments{ Surface{6}; Delete;}{Surface{7}; Delete;};
Recursive Delete {Surface{7}; }
Extrude{0,0,10} {Surface{22}; Layers{10}; Recombine;}
BooleanFragments{ Volume{5}; Delete;}{Volume{6}; Delete;}
// Mesh Options all elements needs to be Hexa
Mesh.RecombineAll = 2;
where I slightly shifted the cylinders to the inside (50->52 and 250 -> 248) should not have the meshing problem.
However, this disconnects the cylinders from the loop and modifies the problem drastically. Here is a zoom on the problematic part in the original, unmodified setup.
So, what you required from the CAD tool, is to handle the merging of those two surfaces (the loop and the cylinder) automatically using BooleanFragments, which might be problematic, especially if one has to take floating-point arithmetic aspects into account.
I have to create a simple simulation of the Earth revolving around the sun. I can't use spherical coordinates, I have to use gravitational energy and velocity equations So far I have this:
earth = sphere (color = color.green, radius = 10**6)
sun = sphere (color = color.yellow, radius = 10**7)
earth.mass = 5.972*10**24
sun.mass=1.989*10**30
d=1.496*10**8
v=(2*math.pi*d)/(3.154*10**7)
earth.velocity = vector(0.0, 0.0, -1*v)
earth.pos = vector(d, 0, 0)
sun.pos = vector(0,0,0)
dt = 50000
t = 0.0
P=0
while True:
rate(400)
f=(6.667*10**.11)*((earth.mass*sun.mass)/d**2)
P = P + f*dt
earth.velocity = P/earth.mass
earth.pos = earth.pos + earth.velocity*dt
t=t+dt
I don't know if I am missing something from the physics side or the coding but when I run it nothing shows up. Thanks in advance!
Two issues: Your rate statement must be indented, and with the momentum P initialized to 0 the Earth should fall into the Sun.
Today, I was messing around with trying to make a gui class for game I'd like to start making in LOVE2D. I decided to try and use OOP to make creating new menus easier in the future. The OOP works great until I try and put it into it's own module, where it gives me the error above. I've double and triple checked my code against similar code and I can't find the problem. I've also looked it up and there are similar threads, but nothing that helps my problem. Here is the relevant code...
From the main.lua
local gui = {
x = 0, y = 0,
width = 0, height = 0,
popupSpeed = 300,
active = false,
color = {red = 0, blue = 0, green = 0, alpha = 0},
menuType = "",
--buttons = require "Game/buttons"
}
And from the gui.lua...
local newGUI = require "Game/gui"
local menus = {
playerInv = newGUI.new()
}
function love.load()
menus.playerInv:createDropDown("right" , _, 30, 100, love.graphics.getHeight() - 60, 50, 128, 50, 255)
end
function gui.new()
newMenu = {}
for k, v in pairs(gui) do
newMenu[k] = v
end
return newMenu
end
function gui:createDropDown(direction, x, y, width, height, red, blue, green, alpha)
self.red = red
self.blue = blue
self.green = green
self.alpha = alpha
if direction == "right" then
self.x = love.graphics.getWidth() - width
self.y = y
self.width = width
self.height = height
self.menuType = "rightDropDown"
elseif direction == "left" then
self.x = 0
self.y = y
self.widthMax = width
self.height = height
self.menuType = "leftDropDown"
elseif direction == "down" then
self.x = x
self.y = y
self.width = width
self.heightMax = height
self.menuType = "regDropDown"
end
end
function gui:drawGui()
if self.active == true then
love.graphics.setColor(self.red, self.blue, self.green, self.alpha)
love.graphics.rectangle("fill", self.x, self.y, self.width, self.height, 10, 10, 6)
end
end
I'm assuming the first snippet is Game/gui and that the second part is main.lua, if this is so, You are attempting to call .new() function which is clearly absent on your Game/gui file. You need to move all of gui's functions to it's own file as well, this includes gui.new(), gui:createDropDown and gui:drawGui() last but not least, you have to return gui at the end of it's own file.
Your main file should end up somewhat like this:
local newGUI = require "Game/gui"
local menus = {
playerInv = newGUI.new()
}
function love.load()
menus.playerInv:createDropDown("right" , _, 30, 100, love.graphics.getHeight() - 60, 50, 128, 50, 255)
end
and Game/gui somewhat like this:
local gui = {} -- With all the stuff inside
function gui.new()
-- With all the stuff it had inside
end
function gui:createDropDown()
-- With all the stuff it had inside
end
function gui:drawGui()
-- With all the stuff it had inside
end
return gui
You see, you forgot to move its functions to its own file, and return gui itself. Don't forget to replace what i omitted on Game/gui!
I want to get some subarea of an image and make it a new image, and then execute further functions on it.
How can I use mouse to select a subarea of an image?
I know img[] could get the subarea of img, but I need some function by which I can interact with img. I mean, I want to get a WYSIWYG effect.
Is there any commands available, or is there any methods of ROI capable?
There different ways to do what you want in a script: You could ask the user to place an ROI and then use the [] to address this area. If you want to get a new image from this selection (CTRL + C and CTRL + SHIFT + V without scripting) you would write:
ShowImage( ImageClone( img[] ) )orimg[].ImageClone().ShowImage()
If you want to place a ROI for the user, you can used SetSelection() for a simple, rectangle, volatile ROI, or you use the full ROI commands like in this example:
image img := RealImage( "Test", 4, 256, 256 ) // create image
img.ShowImage() // show the image (so that it has a display)
imageDisplay disp = img.ImageGetImageDisplay(0) // Get the 'display' of an image
ROI myR = NewRoi() // create ROI
myR.ROISetRectangle( 110, 120, 130, 140 ) // Make it a rectangle of given area
myR.ROISetVolatile( 0 ) // make it non-volatile
myR.ROISetLabel( "Selection" ) // give it a label
myR.ROISetColor( 0, 1, 0 ) // make it green
disp.ImageDisplayAddROI( myR ) // add it to the display
A full list of ROI commands can be found in the following section of the F1 help documentation: