I want to create a CC3MeshNode with boundingvolume in cocos3d. I have created a CC3MeshNode with a pod file. I want to get notification on collision .
-(void) initializeScene
{
[self addContentFromPODFile: #"hello-world.pod" withName:#"pod1"];
CC3MeshNode *object1 = (CC3MeshNode*)[self getNodeNamed: #"pod1"];
CC3MeshNod *object2=[[object1 copyWithName:#"pod1"]autorelease];
[object1 populateAsSolidBox:CC3BoundingBoxMake(9.5, 5.0, 4.0, 0.0, 0.0, 0.0)]; object1.location=cc3v(-5.0, 0.0, 0.0); object2.location=cc3v(5.0, 0.0, 0.0);
[self addChild:object2];
CCActionInterval *move1=[CC3MoveTo actionWithDuration:3.0 moveTo:cc3v(-1.0, 0.0, 0.0)]; CCActionInterval *move2=[CC3MoveTo actionWithDuration:3.0 moveTo:cc3v(1.0, 0.0, 0.0)]; [object1 runAction:move1];
[object2 runAction:move2];
}
-(void) updateAfterTransform: (CC3NodeUpdatingVisitor*) visitor
{
if([object1 doesIntersectNode:object2])
NSLog(#"Collision !!!!!!!!!!!!!!!!!!!!!!");
}
I tried to assign boundingvolume but didnt work. Actually i am confused in implementing boundingvolume for cc3MeshNode
Your comment above says:
I get error in [object1 populateAsSolidBox:CC3BoundingBoxMake(9.5, 5.0, 4.0, 0.0, 0.0, 0.0)];
Here's why you have the error. From the CC3Foundation documentation it looks like you are setting the max values in CC3BoundingboxMake to a value lower than the mins (they are all zero), in the call
CC3BoundingBoxMake(9.5, 5.0, 4.0, 0.0, 0.0, 0.0)];
That is, from the link above, the signature of CC3BoundingBoxMake() is
CC3BoundingBoxMake ( GLfloat minX,
GLfloat minY,
GLfloat minZ,
GLfloat maxX,
GLfloat maxY,
GLfloat maxZ
)
where your minX is 9.5 and your maxX is 0.0, your minY is 5.0 and your maxY is 0.0, and your minZ is 4.0 and your maxZ is 0.0.
Related
So I am using a CAKeyframeAnimation to spin a layer 360*s. It works as intended on iPhone 6 and iOS Simulator:
http://www.goloskok.com/u/cap_2016-12-02_12-51-28_pm.mov
but looks different on iPad mini 1st Gen:
http://goloskok.com/u/ipad-C97WYOhVhj.mov
I suspect that this has something to do with iPad mini being 32bit?
The code I'm using is:
[NSValue valueWithCATransform3D:RTSpinKit3DRotationWithPerspective(1.0/120.0, 0.0, 0.0, 0.0, 0.0)],
[NSValue valueWithCATransform3D:RTSpinKit3DRotationWithPerspective(1.0/120.0, 0.5 * M_PI, 0.0, 1.0, 0.0)],
[NSValue valueWithCATransform3D:RTSpinKit3DRotationWithPerspective(1.0/120.0, M_PI, 0.0, 1.0, 0.0)],
[NSValue valueWithCATransform3D:RTSpinKit3DRotationWithPerspective(1.0/120.0, 2 * M_PI, 0.0, 1.0, 0.0)],
[NSValue valueWithCATransform3D:RTSpinKit3DRotationWithPerspective(1.0/120.0, 0.0, 0.0, 0.0, 0.0)]
With the used function being:
CATransform3D RTSpinKit3DRotationWithPerspective(CGFloat perspective, CGFloat angle, CGFloat x, CGFloat y, CGFloat z) {
CATransform3D transform = CATransform3DIdentity;
transform.m34 = perspective;
return CATransform3DRotate(transform, angle, x, y, z);
}
I create a custom slider with using subclass of UIControl. I draw my background and a button in drawRect() but when I redraw my button, there are remnants of previous draw left.
How can I clear this?
I try to track memory leaks with instruments but I haven't found any leaks. My code is:
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGFloat radius = 10;
// Make sure corner radius isn't larger than half the shorter side
if (radius > self.bounds.size.width/2.0) radius = self.bounds.size.width/2.0;
if (radius > self.bounds.size.height/2.0) radius = self.bounds.size.height/2.0;
CGFloat minx = CGRectGetMinX(self.bounds);
CGFloat midx = CGRectGetMidX(self.bounds);
CGFloat maxx = CGRectGetMaxX(self.bounds);
CGFloat miny = CGRectGetMinY(self.bounds);
CGFloat maxy = CGRectGetMaxY(self.bounds);
CGContextMoveToPoint(context, maxx, miny);
CGContextAddArcToPoint(context, minx, miny+ 30, minx, miny+150, radius);
CGContextAddArcToPoint(context, minx, maxy-30, minx+30, maxy, radius);
CGContextAddLineToPoint (context, maxx, maxy);
CGContextClosePath(context);
CGContextSetRGBFillColor(context,
0.0, 0.0, 0.0, 0.2 );
CGContextSetRGBStrokeColor( context,
0.7, 0.7, 0.7, 0.4 );
CGContextDrawPath(context, kCGPathFill);
CGContextSetLineWidth(context, 10);
CGContextSetLineCap(context, kCGLineCapRound);
CGContextMoveToPoint(context, midx, miny+ 80);
CGContextAddLineToPoint(context, midx, maxy-50);
CGContextStrokePath(context);
[self drawButton: _buttonRect inContext:context];
}
and the drawButton() function:
- (void)drawButton:(CGRect)rect inContext:(CGContextRef) context{
UIGraphicsPushContext(context);
CGFloat radius = 7;
CGFloat minx = CGRectGetMinX(rect);
CGFloat midx = CGRectGetMidX(rect);
CGFloat maxx = CGRectGetMaxX(rect);
CGFloat miny = CGRectGetMinY(rect);
CGFloat midy = CGRectGetMidY(rect);
CGFloat maxy = CGRectGetMaxY(rect);
CGContextMoveToPoint(context, minx, midy);
CGContextAddArcToPoint(context, minx, miny, midx, miny, radius);
CGContextAddArcToPoint(context, maxx, miny, maxx, midy, radius);
CGContextAddArcToPoint(context, maxx, maxy, midx, maxy, radius);
CGContextAddArcToPoint(context, minx, maxy, minx, midy, radius);
CGContextClosePath(context);
CGContextSetRGBFillColor(context,
1.0, 1.0, 1.0, 1.0 );
CGContextDrawPath(context, kCGPathFill);
CGContextSetRGBStrokeColor(context,
1.0, 0.0, 0.0, 1.0 );
//Gradient related variables
size_t locationCount = 3;
CGFloat locationList[] = {0.0,0.5,1.0};
CGFloat colorList[] = {
0.0, 0.0, 0.0, 1.0,
0.5, 0.5, 0.5, 1.0,
1.0, 1.0, 1.0, 1.0
};
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGGradientRef gradient = CGGradientCreateWithColorComponents(colorSpace, colorList,
locationList, locationCount);
//Paint a linear gradient
CGPoint startPoint, endPoint;
startPoint.x = midx;
startPoint.y = midy-2.5;
endPoint.x = midx;
endPoint.y = midy+2.5;
CGContextSaveGState(context);
CGContextAddEllipseInRect(context, CGRectMake(midx-2.5, midy-2.5, 5, 5));
CGContextClosePath(context);
CGContextClip(context);
CGContextDrawLinearGradient (context, gradient, startPoint, endPoint, kCGGradientDrawsAfterEndLocation);
CGContextRestoreGState(context);
UIGraphicsPopContext();
}
I tried a lot of things but now I have no idea anymore. Thanks.
I made of subclass of UIView called Bubble using which i want to draw a solid circle.Here is the drawRect: rect method
- (void)drawRect:(CGRect)rect
{
// Drawing code
CGContextRef ctx = UIGraphicsGetCurrentContext();
// Drawing with a white stroke color
CGContextSetRGBStrokeColor(ctx, 1.0, 1.0, 1.0, 1.0);
// And draw with a blue fill color
CGContextSetRGBFillColor(ctx, 0.0, 0.0, 1.0, 1.0);
// Draw them with a 2.0 stroke width so they are a bit more visible.
CGContextSetLineWidth(ctx, 2.0);
CGRect rrect = CGRectMake(self.bounds.origin.x, self.bounds.origin.y, self.bounds.size.width, self.bounds.size.height);
CGFloat radius = 30.0;
CGFloat minx = CGRectGetMinX(rrect), midx = CGRectGetMidX(rrect), maxx = CGRectGetMaxX(rrect);
CGFloat miny = CGRectGetMinY(rrect), midy = CGRectGetMidY(rrect), maxy = CGRectGetMaxY(rrect);
// Start at 1
CGContextMoveToPoint(ctx, minx, midy);
// Add an arc through 2 to 3
CGContextAddArcToPoint(ctx, minx, miny, midx, miny, radius);
// Add an arc through 4 to 5
CGContextAddArcToPoint(ctx, maxx, miny, maxx, midy, radius);
// Add an arc through 6 to 7
CGContextAddArcToPoint(ctx, maxx, maxy, midx, maxy, radius);
// Add an arc through 8 to 9
CGContextAddArcToPoint(ctx, minx, maxy, minx, midy, radius);
// Close the path
CGContextClosePath(ctx);
// Fill & stroke the path
CGContextDrawPath(ctx, kCGPathFillStroke);
}
I have taken the code from the apple sample Quartzdemo.
My problem is i am drawing circle inside a rectangle,but still i see the corners of the rectangle drawn.
How to clip them so that i see only circle? please help. i am new to quartz 2d
Here is how i call it in my viewDidLoad of my controller
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor colorWithRed:0.5 green:0.5 blue:0.5 alpha:0.5];
Bubble *bub = [[Bubble alloc]initWithFrame:CGRectMake(210.0, 90.0, 60.0, 60.0)];
[self.view addSubview:bub];
[bub release];
}
Put this is viewDidLoad
bub.backgroundColor = [UIColor clearColor];
I'm porting a C++-based engine from Android to iOS and I have some questions on how landscape mode on iOS devices work.
I can successfully start my device on landscape mode but my framebuffer stays on portrait mode no matter how I rotate it (or force its rotation). For instance, I would expect a 960x640 resolution instead of the default 320x480.
I've been researching about it and I saw some people doing this but explicitly rotating the scene using OpenGL, which means the actual axis were not in landscape mode, which for me is an ugly workaround.
Make sure your OpenGL UIView is attached to a UIViewController and not inserted directly into the UIWindow. To do that, create a custom view controller:
#interface MyViewController : UIViewController
#end
#implementation MyViewController
- (void)loadView {
// Create your EAGL view
MyEAGLView *eaglView = [[MyEAGLView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
self.view = eaglView;
[eaglView release];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}
#end
Then add it to your app's window via the AppDelegate:
- (void)applicationDidFinishLaunching:(UIApplication *)application {
MyViewController *viewController = [[MyViewController alloc] init];
[self.window setRootViewController:viewController];
[viewController release];
[self.window makeKeyAndVisible];
}
self.window is just the app's UIWindow, you may have to change that if you store the window somewhere else.
After that, the UIViewController takes care of rotations and resizing your view when the device is turned and you can override the layoutSubviews method in your MyEAGLView object to handle resizing the OpenGL context. You may also want to look at Apple's sample code for OpenGL apps since it correctly handles this stuff.
You can rotate the OpenGL output in the vertex shader as follows:
#version 300 es
in vec4 position;
in mediump vec4 texturecoordinate;
in vec4 color;
uniform float preferredRotation;
out mediump vec2 coordinate;
void main()
{
//const float pi = 4.0 * atan(1.0);
//float radians = (( -90.0 ) / 180.0 * pi );
// Preferred rotation of video acquired, for example, by:
// AVAssetTrack *videoTrack = [tracks objectAtIndex:0];
// CGAffineTransform preferredTransform = [videoTrack preferredTransform];
// self.glKitView.preferredRotation = -1 * atan2(preferredTransform.b, preferredTransform.a);
// Preferred rotation for both portrait and landscape
mat4 rotationMatrix = mat4( cos(preferredRotation), -sin(preferredRotation), 0.0, 0.0,
sin(preferredRotation), cos(preferredRotation), 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
0.0, 0.0, 0.0, 1.0);
// Mirror vertical (portrait only)
mat4 rotationMatrix = mat4( cos(preferredRotation), sin(preferredRotation), 0.0, 0.0,
-sin(preferredRotation), cos(preferredRotation), 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
0.0, 0.0, 0.0, 1.0);
// Mirror horizontal (landscape only)
mat4 rotationMatrix = mat4( 1.0, 0.0, 0.0, 0.0,
0.0, cos(preferredRotation), -sin(preferredRotation), 0.0,
0.0, sin(preferredRotation), cos(preferredRotation), 0.0,
0.0, 0.0, 0.0, 1.0);
// Mirror vertical (landscape only)
mat4 rotationMatrix = mat4( cos(preferredRotation), 0.0, sin(preferredRotation), 0.0,
0.0, 1.0, 0.0, 0.0,
-sin(preferredRotation), 0.0, cos(preferredRotation), 0.0,
0.0, 0.0, 0.0, 1.0);
gl_Position = position * rotationMatrix;
coordinate = texturecoordinate.xy;
}
Obviously, you choose only one matrix4, depending on the orientation
of the video, and then its rotation. Each matrix4 flips the video
window -- not rotates it. For rotation, you have to first pick the
matrix4 based on the orientation, and then substitute the
preferredRotation variable with the number of degrees (in radians, the
formula for which is also provided).
There are a lot of ways to rotate a view, layer, object, etc.; but, if
you're rendering an image via OpenGL, then you should choose this
method, and only this method.
I want to draw text use Quartz 2D. The "menu" direction is wrong. I want "Menu" still readable and have 45 degree with X-axis. Below is my code:
CGContextSelectFont(context, "Arial", 12, kCGEncodingMacRoman);
CGContextSetTextDrawingMode(context, kCGTextFill);
CGContextSetRGBFillColor(context, 0, 0, 0, 1); // 6
CGContextSetRGBStrokeColor(context, 0, 0, 0, 1);
CGContextSetTextMatrix(context, CGAffineTransformMake(1.0,0.0, 0.0, -1.0, 0.0, 0.0));
CGContextSetTextMatrix(context, CGAffineTransformMakeRotation(45));
CGContextShowTextAtPoint(context,10, 10, "Menu", 4);
CGAffineTransformMakeRotation expects angle in radians, not in degrees.
#define DEGREES_TO_RADIANS(x) (M_PI * (x) / 180.0)
...
CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(45));
[view setTransform:CGAffineTransformMakeRotation(M_PI / 2)];