My dynamic body is not falling down after setting the world. It is not following the gravity. Even I apply force on the body it isn't work.
b2Vec2 gravity(0.0f, -9.8f);
b2World world(gravity);
//PineCone
class PineCone
{
private:
//Surface box
SDL_Rect box;
//Real position
float rX, rY;
//Velocity
float xVel, yVel;
//Physics world
b2World* world;
//Body
b2Body* pBodyPineCone;
public:
//Specify
void specify(int w, int h, SDL_Surface* source, SDL_Surface* dest, b2World* world);
//Input
void handle_input();
//Motion
void updatepos();
//void checkStanding(SDL_Rect env[], int N_SOLIDS);
//Render
void show();
};
void PineCone::specify(int w, int h, SDL_Surface* source, SDL_Surface* dest, b2World* world)
{
//Source
pPineCone = source;
//Destination
pScreen = dest;
// Define the dynamic pinecone. We set its position and call the body factory.
b2BodyDef bodyDef;
bodyDef.type = b2_dynamicBody;
// Define a ball shape for our dynamic body.
b2CircleShape dynamicPineCone;
dynamicPineCone.m_p.Set(0, 0); //position, relative to body position
dynamicPineCone.m_radius = 0.5; //radius
// Define the dynamic body fixture.
b2FixtureDef fixtureDef;
fixtureDef.shape = &dynamicPineCone;
fixtureDef.density = 1.0f;
fixtureDef.friction = 0.3f;
// Add the shape to the body.
bodyDef.position.Set(0, 20);
pBodyPineCone = world->CreateBody(&bodyDef);
pBodyPineCone->CreateFixture(&fixtureDef);
}
void PineCone::show()
{
int blit = SDL_BlitSurface( pPineCone, NULL, pScreen, &box );
if( blit < 0 )
{
exit(1);
}
}
I am not sure if I missed something in Main.
int main(int argc, char *argv[])
{
B2_NOT_USED(argc);
B2_NOT_USED(argv);
// Prepare for simulation. Typically we use a time step of 1/60 of a
// second (60Hz) and 10 iterations. This provides a high quality simulation
// in most game scenarios.
float32 timeStep = 1.0f / 60.0f;
int32 velocityIterations = 6;
int32 positionIterations = 2;
// Start the timer
g_dwStartTicks = SDL_GetTicks();
init();
load_files();
//PineCone
PineCone myPineCone;
myPineCone.specify(g_iPineConeSize, g_iPineConeSize, pPineCone, pScreen, &world);
//
// The Main Game Loop
//
do
{
// Record the current time
g_dwStartTime = SDL_GetTicks();
//
// Handle they keyboard events here
//
while (SDL_PollEvent(&event) > 0)
{
if (event.type == SDL_QUIT)
{
// Quit event! (Window close, kill signal, etc.)
g_iLoopDone = TRUE;
}
}
world.Step(timeStep, velocityIterations, positionIterations);
myPineCone.handle_input();
if (g_bPlaying || g_bEnd)
{
//Show pinecone
myPineCone.show();
}
// Update the display
SDL_Flip(pScreen);
g_dwEndTime = SDL_GetTicks();
if (g_dwEndTime < g_dwStartTime + (1000 / 60))
{
SDL_Delay(g_dwStartTime + (1000 / 60) - g_dwEndTime);
}
}while (!g_iLoopDone);
//Clean Up
clean_up();
return 0;
}
I can think for example that the physical self is updating're not visualizing
what the Sprite (Image) not update the corresponding value of its physical body.
Try to give it a Box2D body position if the image does not move you do not
have to be the update of physics.
Try this:
void setL1b2SpritePosition(Point position)
{
pBodyPineCone->SetAwake (true);
pBodyPineCone->setPosition(position);
//update box2d
pBodyPineCone->SetTransform(
b2Vec2(position.x / PTM_RATIO, position.y/ PTM_RATIO),
_body->GetAngle());
}
void updatePhysics()
{
_world->Step(.1, 10, 10);
for (b2Body *b = _world->GetBodyList(); b; b = b->GetNext())
{
if (b->GetUserData() != NULL)
{
Sprite *ballData = dynamic_cast<Sprite*> ((Sprite *) b->GetUserData());
ballData->setPosition(b->GetPosition().x * PTM_RATIO,
b->GetPosition().y * PTM_RATIO);
ballData->setRotation(-1 * CC_RADIANS_TO_DEGREES(b->GetAngle()));
}
}
}
Related
I am in a project where map and other information are displayed in a Gtk window. There are several map layers, that I draw into a Cairo surface and save in a .png (plotRect() function in code below). That .png is displayed in a Gtk image when there is a Gtk draw signal (draw()).
I now want to accomplish smooth grabbing and panning. When the mouse button is pressed, I want the whole image to be translated within the window area, follow the movements of the mouse. When the button is released the image should be Cairo remade and redrawn with new bounds. During the drag/pan procedure itself there is no need to draw areas that where previously out of the window borders -- it is OK to wait for that to be done when the mouse button is released.
Enclosed you find a simplified version of my code. The main has a loop going until the windows is closed, redrawing the image after each panning. The problem is in the pan() function. After the translation in line 21 I woould expect the draw statement in line 128 to successively draw panned images while moving the mouse, but the visible image is unaffected. Uncommenting line 23 shows that graph->image has really been modified, and I can see that the draw signal of line 25 is invoking the draw() callback function. After button release, the translated image is correctly displayed.
Can anyone please give me some advice?
I'm using gcc, Cairo, Gtk3 and Ubuntu 18.04 on a double-booted MacBook Pro 64 bit i5.
#include <cairo.h>
#include <chrono>
#include <cmath>
#include <gtk/gtk.h>
#include <iostream>
#include <mutex>
#include <thread>
using namespace std;
mutex mtx;
bool gtkMainLoopRunning = false;
#define SLEEP(d) this_thread::sleep_for(chrono::milliseconds(d))
template <class T>
inline T sqr(T x) {
return x * x;
}
//-----------------------------------------------------------------------------
class Graph {
double toplat, leftlon; // upper left corner
double dydlat, dxdlon; // pixels / degree lon/lat
public:
int size; // window x = y
GtkWidget *window;
GtkImage *image;
const char *png = "/tmp/image.png";
cairo_surface_t *surface{};
cairo_t *cr{};
bool closed = false;
bool leftbuttondown = false;
int mousex = 0, mousey = 0;
Graph(const double, const double, const double, const double);
~Graph();
void plotRect(const double, const double, const double, const double);
bool pan();
};
//-----------------------------------------------------------------------------
static gboolean draw(GtkWidget *widget, cairo_t *cr, gpointer data) {
Graph *graph = (Graph *)data;
if (!graph->leftbuttondown) {
mtx.lock();
gtk_image_set_from_file(graph->image, graph->png);
mtx.unlock();
}
return FALSE;
}
//-----------------------------------------------------------------------------
static gboolean clicked(GtkWidget *widget, GdkEventButton *button, gpointer data) {
Graph *graph = (Graph *)data;
if (button->button == 1) {
if (button->type == GDK_BUTTON_PRESS) {
graph->leftbuttondown = true;
} else if (button->type == GDK_BUTTON_RELEASE) {
graph->leftbuttondown = false;
}
}
graph->mousex = button->x;
graph->mousey = button->y;
return FALSE;
}
//-----------------------------------------------------------------------------
Graph::~Graph() {
do {
SLEEP(100);
} while (gtkMainLoopRunning); // wait until gtk main loop has stopped
cairo_destroy(cr);
cairo_surface_destroy(surface);
}
//-----------------------------------------------------------------------------
void destroyWindow(GtkWidget *widget, gpointer data) {
gtk_main_quit();
gtkMainLoopRunning = false;
Graph *graph = (Graph *)data;
graph->closed = true; // signal gtkThread to finish
gtk_widget_destroy((GtkWidget *)graph->image);
gtk_widget_destroy(graph->window);
}
//-----------------------------------------------------------------------------
Graph::Graph(const double minlat, const double minlon, const double maxlat, const double maxlon) {
gtk_init(NULL, NULL);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
size = 800;
gtk_widget_show(window);
image = (GtkImage *)gtk_image_new();
gtk_widget_set_size_request((GtkWidget *)image, size, size);
gtk_container_add(GTK_CONTAINER(window), (GtkWidget *)image);
gtk_widget_show((GtkWidget *)image);
g_signal_connect(image, "draw", G_CALLBACK(draw), this);
g_signal_connect(window, "destroy", G_CALLBACK(destroyWindow), this);
surface = cairo_image_surface_create(CAIRO_FORMAT_RGB24, size, size);
cr = cairo_create(surface);
gtk_widget_add_events(window, GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK | GDK_BUTTON1_MOTION_MASK);
g_signal_connect(window, "button-press-event", G_CALLBACK(clicked), this);
g_signal_connect(window, "button-release-event", G_CALLBACK(clicked), this);
g_signal_connect(window, "motion-notify-event", G_CALLBACK(clicked), this);
const double coslat = cos((minlat + maxlat) / 2 * M_PI / 180);
const double extension = max(maxlat - minlat, (maxlon - minlon) * coslat); // [lat degrees]
toplat = (minlat + maxlat + extension) / 2;
leftlon = (minlon + maxlon - extension / coslat) / 2;
dydlat = -size / extension; // [pixels/degree]
dxdlon = size / extension * coslat;
gtkMainLoopRunning = true;
thread(gtk_main).detach();
}
//-----------------------------------------------------------------------------
bool Graph::pan() {
const int sqrSignifPan = sqr(4);
while (!closed) {
if (leftbuttondown) {
int x0 = mousex;
int y0 = mousey;
int dx = 0, dy = 0;
GdkPixbuf *origPixbuf = gdk_pixbuf_new_from_file("/tmp/image.png", NULL);
char *origPixels = (char *)gdk_pixbuf_get_pixels(origPixbuf);
const int rowstride = gdk_pixbuf_get_rowstride(origPixbuf);
const int nChannels = gdk_pixbuf_get_n_channels(origPixbuf);
char *imagePixels = (char *)gdk_pixbuf_get_pixels(gtk_image_get_pixbuf(image));
while (leftbuttondown) {
const int dx0 = dx, dy0 = dy;
dx = mousex - x0, dy = mousey - y0;
if (sqr(dx - dx0) + sqr(dy - dy0) >= sqrSignifPan) {
const int minx = max(0, -dx);
const int nx = max(0, size - abs(dx));
if (nx > 0) {
for (int y = max(0, -dy); y < min(size, size - dy); ++y) {
memcpy(imagePixels + (y + dy) * rowstride + (minx + dx) * nChannels, origPixels + y * rowstride + minx * nChannels, nx * nChannels);
}
// gdk_pixbuf_save(gtk_image_get_pixbuf(image), "/tmp/imagePixbuf.png", "png", NULL, NULL);
gtk_widget_queue_draw((GtkWidget *)image);
SLEEP(10); // pause for drawing
}
}
SLEEP(100);
}
// rescale graph
toplat -= (mousey - y0) / dydlat;
leftlon -= (mousex - x0) / dxdlon;
dxdlon = -dydlat * cos((toplat + size / dydlat / 2) * M_PI / 180);
gtk_widget_queue_draw((GtkWidget *)image);
return true;
}
SLEEP(100);
}
return false;
}
//-----------------------------------------------------------------------------
void Graph::plotRect(const double minlat, const double minlon, const double maxlat, const double maxlon) {
cairo_set_source_rgb(cr, 1, 1, 1);
cairo_paint(cr);
cairo_rectangle(cr, (minlon - leftlon) * dxdlon, (minlat - toplat) * dydlat, (maxlon - minlon) * dxdlon, (maxlat - minlat) * dydlat);
cairo_set_source_rgb(cr, 0, 1, 0);
cairo_fill(cr);
mtx.lock();
remove(png);
cairo_surface_write_to_png(surface, png);
mtx.unlock();
}
//-----------------------------------------------------------------------------
int main() {
const double minlat = 59, minlon = 16, maxlat = 60, maxlon = 18;
Graph *graph = new Graph(minlat - 0.5, minlon - 1, maxlat + 0.5, maxlon + 1);
do {
graph->plotRect(minlat, minlon, maxlat, maxlon);
} while (graph->pan());
delete graph;
}
I searched on this topic and found that using sf::Clock to implement frame independent update(using deltaTime) can help to solve it. However even after adding it the paddle movement stutters a bit. On the other hand when I shift the entire Playing case to the event polling loop without using deltatime the game seems to run smoothly.
How do I go about using sf::Clock properly in my code and why does my game seems to run smoothly when I shift the Playing case in event pool loop without even using deltatime?
Game initialization:
void Game::start() {
if (_gameState != Uninitialized) {
return;
}
//Creating window
_mainWindow.create(sf::VideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, 32), "Pong");
_gameState = ShowingMenu;
//Adding game entities to the manager
VisibleGameObject *paddle1 = new PlayerPaddle("Player1", 1.0f, sf::Keyboard::Key::Up, sf::Keyboard::Key::Down);
VisibleGameObject *paddle2 = new PlayerPaddle("Player2", 1.0f, sf::Keyboard::Key::W, sf::Keyboard::Key::S);
VisibleGameObject *background = new Background("Background", 0.0f);
paddle1 -> setPosition(SCREEN_WIDTH - (paddle1 -> getWidth()), 0);
paddle2->setPosition(0, 0);
manager.addObject(paddle1);
manager.addObject(paddle2);
manager.addObject(background);
//Starting Clock
deltaTime = 0.0f;
frameClock.restart();
while (!isExiting()) {
gameLoop();
}
_mainWindow.close();
}
Game Loop :
void Game::gameLoop()
{
static bool firstPass = true;
sf::Event currentEvent;
//Event loop
while(_mainWindow.pollEvent(currentEvent) || firstPass)
{
if (firstPass) {
currentEvent = sf::Event();
currentEvent.type = sf::Event::GainedFocus;
firstPass = false;
}
if (currentEvent.type == sf::Event::Closed)
{
_gameState = Exiting;
}
switch (_gameState)
{
case ShowingMenu:
{
showingMenu();
break;
}
case Paused:
{
break;
}
default:
break;
}
}
//Extracting deltaTime to update game logic
deltaTime = frameClock.restart().asSeconds();
if(_gameState == Playing)
{
manager.updateAllLayers(deltaTime);
manager.drawAllLayers(_mainWindow);
_mainWindow.display();
}
}
Paddle Update Logic:
void PlayerPaddle::update(const float & elapsedTime)
{
sf::Vector2f currentPos = getPosition();
float displacement = 0.0f;
if (sf::Keyboard::isKeyPressed(controls.up))
{
displacement = -speed * elapsedTime;
}
else if (sf::Keyboard::isKeyPressed(controls.down))
{
displacement = speed * elapsedTime;
}
if (displacement + currentPos.y < 0.0f)
{
setPosition(currentPos.x, 0.0f);
return;
}
else if (displacement + currentPos.y + getHeight() > Game::SCREEN_HEIGHT)
{
setPosition(currentPos.x, Game::SCREEN_HEIGHT - getHeight());
return;
}
setPosition(currentPos.x, currentPos.y + displacement);
}
I don't have enough reputation to post a comment so I'll have to make this an answer...
You update (and draw) every frame. Your game loop doesn't make sense if you want to implement a fixed update time step.
This is how your game loop should look like. I use your variable names for your convenience:
// ...
sf::Clock frameClock;
const sf::Time timePerFrame = sf::seconds(1.0f / 60.0f);
sf::Time timeSinceLastUpdate = sf::Time::Zero;
while (_mainWindow.isOpen()) {
timeSinceLastUpdate += frameClock.restart();
// events
{
sf::Event evt;
while (_mainWindow.pollEvent(evt)) {
//...
}
}
// update
{
while (timeSinceLastUpdate > timePerFrame) {
timeSinceLastUpdate -= timePerFrame;
manager.updateAllLayers(timePerFrame);
}
}
// render
{
_mainWindow.clear();
// ...
_mainWindow.display();
}
}
// ...
This updates your game at a fixed rate (1/60s) and renders it whenever possible (you can use setFramerateLimit() to limit the FPS; this won't affect the fixed update interval, so no problem there).
You now pass sf::Time to your manager.updateAllLayers which is used like your elapsedTime (it has functions like .asSeconds() so essentially nothing changes but you'll obviously have to adjust the values for speed)
I am very new to SFML and C++. My problem is that I successfully fire projectiles towards the mouse. However as I move the mouse the projectiles also move.
EDIT:
Okay I solved my previous problem. However the projectile stops while it reaches the mouse position. How can the bullets continue to go further than its destination?
Code:
if (isFiring == true) {
sf::Vector2f startPos = sf::Vector2f(myPipe.getX(), myPipe.getY());
sf::Vector2i mousePos = sf::Mouse::getPosition(window);
sf::Vector2f endPos = sf::Vector2f(mousePos.x, mousePos.y);
double projSpeed{0.3};
//float projSpeed = projSpeed * (myPipe.getY() - mousePos.y) / 100;
Projectile newProjectile(20, startPos, endPos, projSpeed);
projVec.push_back(newProjectile);
isFiring = false;
}
for (int i = 0; i < projVec.size(); i++) {
projVec[i].fire(delta.asMilliseconds());
projVec[i].draw(window);
clock.restart();
}
fire function:
void fire( float delta) {
sf::Vector2f pos = projectile.getPosition();
float veloX = (_end.x - pos.x);
float veloY = (_end.y - pos.y);
float distance = sqrt((veloX*veloX + veloY * veloY));
float dx = veloX / distance;
float dy = veloY / distance;
projectile.move(dx * delta, dy * delta);
}
One more question, am I doing it correct with multiplying with delta? The bullets are really laggy and weird.
(Original reply:)
I don't have enough reputation to comment so I'll have to put it here...
So if I understand correctly you're saying you want to shoot the projectiles towards the mouse's position but don't want them to follow its position once fired? In this case:
I see you have the code to draw the projectiles inside that for-loop, which leads me to believe you are calling the loop continuously. But the same loop also contains the code used to shoot the projectiles, which means that you are also continuously calling this "fire" code. So you are steering the projectiles towards the current mouse position in every loop iteration which means they will always follow the current mouse position.
What you should be doing is move the code to shoot the projectiles and only call it once (you can only shoot a projectile once, right?) so a projectile's directional vector is not constantly altered, resulting in the problem you stated.
And make sure to always separate logic and rendering. Ideally you should be updating the physics (for example the projectile updating) at a fixed interval so that the game will run the same on all machines, and draw all your stuff separately.
If you do want the projectiles to always follow the mouse, you can use this (semi pseudo) code (I'm using this code in one of my games):
sf::Vector2f normalizedVector = normalize(mousePos - projectilePos);
normalizedVector.x *= speed * deltaTime;
normalizedVector.y *= speed * deltaTime;
projectile.move(normalizedVector);
(Edit:)
I made a quick example project that works. This doesn't use all of your code but hopefully gives you an idea how it can be done.
Here's a short video that shows what the code below does: https://webmshare.com/play/jQqvd
main.cpp
#include <SFML/Graphics.hpp>
#include "projectile.h"
int main() {
const sf::FloatRect viewRect(0.0f, 0.0f, 800.0f, 600.0f);
sf::RenderWindow window(sf::VideoMode(viewRect.width, viewRect.height), "Test");
window.setFramerateLimit(120);
window.setVerticalSyncEnabled(false);
window.setKeyRepeatEnabled(false);
sf::Clock deltaClock;
const sf::Time timePerFrame = sf::seconds(1.0f / 60.0f);
sf::Time timeSinceLastUpdate = sf::Time::Zero;
std::vector<Projectile> projectiles;
while (window.isOpen()) {
timeSinceLastUpdate += deltaClock.restart();
// process events
{
sf::Event evt;
while (window.pollEvent(evt)) {
switch (evt.type) {
case sf::Event::Closed: { window.close(); } break;
// shoot with left mouse button
case sf::Event::MouseButtonPressed: { switch (evt.mouseButton.button) { case sf::Mouse::Button::Left: {
const sf::Vector2f center(viewRect.left + viewRect.width / 2, viewRect.top + viewRect.height / 2);
const sf::Vector2f mousePos(window.mapPixelToCoords(sf::Mouse::getPosition(window)));
const float angle = atan2(mousePos.y - center.y, mousePos.x - center.x);
projectiles.push_back(Projectile(center, angle));
} break; default: {} break; } } break;
default: {} break;
}
}
}
// update
{
while (timeSinceLastUpdate > timePerFrame) {
timeSinceLastUpdate -= timePerFrame;
// update projectiles
{
for (std::size_t i = 0; i < projectiles.size(); ++i) {
Projectile &proj = projectiles[i];
proj.update(timePerFrame);
if (!viewRect.intersects(proj.getBoundingBox())) { proj.destroy(); }
}
projectiles.erase(std::remove_if(projectiles.begin(), projectiles.end(), [](Projectile const &p) { return p.getCanBeRemoved(); }), projectiles.end());
}
}
}
// render
{
window.clear();
for (std::size_t i = 0; i < projectiles.size(); ++i) {
window.draw(projectiles[i]);
}
window.display();
}
}
return EXIT_SUCCESS;
}
projectile.h:
#ifndef PROJECTILE_H_INCLUDED
#define PROJECTILE_H_INCLUDED
#include <SFML/Graphics.hpp>
class Projectile : public sf::Drawable {
public:
Projectile();
Projectile(const sf::Vector2f pos, const float angle);
virtual ~Projectile();
const bool &getCanBeRemoved() const;
const sf::FloatRect &getBoundingBox() const;
void destroy();
void update(const sf::Time dt);
private:
virtual void draw(sf::RenderTarget &renderTarget, sf::RenderStates renderStates) const;
bool canBeRemoved_;
sf::FloatRect boundingBox_;
float angle_;
float speed_;
sf::RectangleShape shape_;
};
#endif
projectile.cpp:
#include "projectile.h"
Projectile::Projectile() :
canBeRemoved_(true),
boundingBox_(sf::FloatRect()),
angle_(0.0f),
speed_(0.0f)
{
}
Projectile::Projectile(const sf::Vector2f pos, const float angle) {
canBeRemoved_ = false;
boundingBox_ = sf::FloatRect(pos, sf::Vector2f(10.0f, 10.0f));
angle_ = angle;
speed_ = 0.5f;
shape_.setPosition(sf::Vector2f(boundingBox_.left, boundingBox_.top));
shape_.setSize(sf::Vector2f(boundingBox_.width, boundingBox_.height));
shape_.setFillColor(sf::Color(255, 255, 255));
}
Projectile::~Projectile() {
}
const bool &Projectile::getCanBeRemoved() const {
return canBeRemoved_;
}
const sf::FloatRect &Projectile::getBoundingBox() const {
return boundingBox_;
}
void Projectile::destroy() {
canBeRemoved_ = true;
}
void Projectile::update(const sf::Time dt) {
boundingBox_.left += static_cast<float>(std::cos(angle_) * speed_ * dt.asMilliseconds());
boundingBox_.top += static_cast<float>(std::sin(angle_) * speed_ * dt.asMilliseconds());
shape_.setPosition(boundingBox_.left, boundingBox_.top);
}
void Projectile::draw(sf::RenderTarget &renderTarget, sf::RenderStates renderStates) const {
renderTarget.draw(shape_);
}
Ball is not getting bounced what am I doing wrong ?
here is the class
#include "GameLayer.h"
USING_NS_CC;
GameLayer::GameLayer(){
mWorld = NULL;
mBody=NULL;
mBall=NULL;
}
GameLayer::~GameLayer(){
delete mWorld;
mBody=NULL;
delete mBall;
mBall=NULL;
}
bool GameLayer::init()
{
if ( !CCLayer::init() )
{
return false;
}
CCSize winSize = CCDirector::sharedDirector()->getWinSize();
// Create sprite and add it to the layer
mBall = CCSprite::spriteWithFile("Ball.jpg",CCRectMake(0,0,52,52));
mBall->setPosition(ccp(100,100));
addChild(mBall);
// Create a world
b2Vec2 gravity;
gravity.Set(0.0f,-10.0f);
mWorld = new b2World(gravity);
// Do we want to let bodies sleep?
// mWorld->SetAllowSleeping(true);
// mWorld->SetContinuousPhysics(true);
//Definition of the body
// body definition to specify initial properties of the body such as position or velocity.
b2BodyDef groundBodyDef;
groundBodyDef.position.Set(0,0);
// use the world object to create a body object by specifying the body definition.
b2Body *groundBody = mWorld->CreateBody(&groundBodyDef);
//Create the shape as needed (for ground all the sides)
b2EdgeShape groundEdge;
groundEdge.Set(b2Vec2_zero,b2Vec2(winSize.width/PTM_RATIO,0));
groundBody->CreateFixture(&groundEdge,0);
groundEdge.Set(b2Vec2(0,0), b2Vec2(0, winSize.height/PTM_RATIO));
groundBody->CreateFixture(&groundEdge,0);
groundEdge.Set(b2Vec2(0, winSize.height/PTM_RATIO),
b2Vec2(winSize.width/PTM_RATIO, winSize.height/PTM_RATIO));
groundBody->CreateFixture(&groundEdge,0);
groundEdge.Set(b2Vec2(winSize.width/PTM_RATIO,
winSize.height/PTM_RATIO), b2Vec2(winSize.width/PTM_RATIO, 0));
groundBody->CreateFixture(&groundEdge,0);
// Create the another body
//1. body defn
//2. create body from game world
// 3. define shape and create fixtures
b2BodyDef ballBodyDef;
ballBodyDef.position.Set(100/PTM_RATIO,100/PTM_RATIO);
// applying sprite as userdata to the bodydef
ballBodyDef.userData = mBall;
mBody = mWorld->CreateBody(&ballBodyDef);
// created different shapes as you want
b2CircleShape circle;
circle.m_radius = 26.0/PTM_RATIO;
//create the fixturedef
b2FixtureDef ballShapeDef;
ballShapeDef.shape = &circle;
ballShapeDef.density=1.0f;
ballShapeDef.friction=0.2f;
ballShapeDef.restitution=0.8f;
mBody->CreateFixture(&ballShapeDef);
setAccelerometerEnabled(true);
scheduleUpdate();
return true;
}
void GameLayer::update(float dt){
mWorld->Step(dt,10,10);
for(b2Body *b = mWorld->GetBodyList(); b; b=b->GetNext()) {
if (b->GetUserData() != NULL) {
CCSprite *ballData = (CCSprite *)b->GetUserData();
ballData->setPosition(ccp(b->GetPosition().x * PTM_RATIO,
b->GetPosition().y * PTM_RATIO));
ballData->setRotation(-1 * CC_RADIANS_TO_DEGREES(b->GetAngle()));
}
}
}
void GameLayer::didAccelerate(CCAcceleration* pAccelerationValue)
{
b2Vec2 gravity(-pAccelerationValue->y * 15,pAccelerationValue->x *15);
mWorld->SetGravity(gravity);
}
b2BodyDef has b2_staticBody type by default. Try to set
ballBodyDef.type = b2_dynamicBody;
I'm a beginner that just followed cocos2d-x's native tutorials and I am faced with a huge wall!
This is my error:
>c:\cocos2d-2.0-x-2.0.3\cocos2dsimplegame\classes\helloworldscene.cpp(86): error C2440: 'type cast' : cannot convert from 'void (__thiscall HelloWorld::* )(cocos2d::CCTime)' to 'cocos2d::SEL_SCHEDULE'
>Pointers to members have different representations; cannot cast between them
My cpp file:
#include "HelloWorldScene.h"
using namespace cocos2d;
CCScene* HelloWorld::scene()
{
CCScene * scene = NULL;
do
{
// 'scene' is an autorelease object
scene = CCScene::create();
CC_BREAK_IF(! scene);
// 'layer' is an autorelease object
HelloWorld *layer = HelloWorld::create();
CC_BREAK_IF(! layer);
// add layer as a child to scene
scene->addChild(layer);
} while (0);
// return the scene
return scene;
}
// on "init" you need to initialize your instance
bool HelloWorld::init()
{
bool bRet = false;
_targets = CCArray::create();
_projectiles = CCArray::create();
do {
////////////////////
// super init first
////////////////////
if ( !CCLayerColor::initWithColor( ccc4(255, 255, 255, 255) ) )
{
return false;
}
////////////////////
// add your codes below..
////////////////////
// 1. Add a menu item with "X" image, which is clicked to quit the program.
// Create a "close" menu item with close icon, it's an auto release object.
CCMenuItemImage *pCloseItem = CCMenuItemImage::itemWithNormalImage(
"CloseNormal.png",
"CloseSelected.png",
this,
menu_selector(HelloWorld::menuCloseCallback));
CC_BREAK_IF(! pCloseItem);
// Place the menu item bottom-right conner.
pCloseItem->setPosition(ccp(CCDirector::sharedDirector()->getWinSize().width - 20, 20));
//Create a menu with the "close" menu item, it's an auto release object.
CCMenu* pMenu = CCMenu::menuWithItems(pCloseItem, NULL);
pMenu->setPosition(CCPointZero);
CC_BREAK_IF(! pMenu);
//Add the menu to HelloWorld layer as a child layer.
this->addChild(pMenu, 1);
//////////////////////
// 2. add your codes below...
CCSize winSize = CCDirector::sharedDirector()->getWinSize();
CCSprite *player = CCSprite::spriteWithFile("Player.png", CCRectMake(0, 0, 27, 40));
player->setPosition(ccp(player->getContentSize().width/2, winSize.height/2));
this->addChild(player);
bRet = true;
} while (0);
// Call game logic about every second
this->schedule( schedule_selector(HelloWorld::gameLogic), 1.0);
// You can shoot the bullet
this->setTouchEnabled(true);
this->schedule( schedule_selector(HelloWorld::update) );
return bRet;
}
void HelloWorld::menuCloseCallback(CCObject* pSender)
{
// "close" menu item clicked
CCDirector::sharedDirector()->end();
}
void HelloWorld::addTarget()
{
CCSprite *target = CCSprite::spriteWithFile("Target.png", CCRectMake(0, 0, 27, 40));
// Determine where to spawn the target along the Y axis
CCSize winSize = CCDirector::sharedDirector()->getWinSize();
int minY = target->getContentSize().height/2;
int maxY = winSize.height - target->getContentSize().height/2;
int rangeY = maxY - minY;
// srand( TimGetTicks() );
int actualY = ( rand() % rangeY ) + minY;
// Create the target slightly off-screen along the right edge,
// and along a random position along the Y axis as calculated
target->setPosition( ccp(winSize.width + (target->getContentSize().width/2), actualY) );
this->addChild(target);
// Determine speed of the target
int minDuration = (int)2.0;
int maxDuration = (int)4.0;
int rangeDuration = maxDuration - minDuration;
// srand( TimGetTicks() );
int actualDuration = ( rand() % rangeDuration ) + minDuration;
// Create the actions
CCFiniteTimeAction* actionMove = CCMoveTo::actionWithDuration( (float)actualDuration, ccp(0 - target->getContentSize().width/2, actualY) );
CCFiniteTimeAction* actionMoveDone = CCCallFuncN::actionWithTarget( this, callfuncN_selector(HelloWorld::spriteMoveFinished) );
target->runAction( CCSequence::actions( actionMove, actionMoveDone, NULL) );
// Add to targets array
target->setTag(1);
_targets->addObject(target);
}
void HelloWorld::spriteMoveFinished(CCNode* sender)
{
CCSprite *sprite = (CCSprite *)sender;
this->removeChild(sprite, true);
if (sprite->getTag() == 1) // target
{
_targets->removeObject(sprite);
}
else if (sprite->getTag() == 2) // projectile
{
_projectiles->removeObject(sprite);
}
}
void HelloWorld::gameLogic(float dt)
{
this->addTarget();
}
void HelloWorld::ccTouchesEnded(CCSet* touches, CCEvent* event)
{
// Choose one of the touches to work with
CCTouch* touch = (CCTouch*)( touches->anyObject() );
CCPoint location = touch->locationInView();
location = CCDirector::sharedDirector()->convertToGL(location);
// Set up initial location of projectile
CCSize winSize = CCDirector::sharedDirector()->getWinSize();
CCSprite *projectile = CCSprite::spriteWithFile("Projectile.png", CCRectMake(0, 0, 20, 20));
projectile->setPosition( ccp(20, winSize.height/2) );
// Determine offset of location to projectile
int offX = location.x - projectile->getPosition().x;
int offY = location.y - projectile->getPosition().y;
// Bail out if we are shooting down or backwards
if (offX <= 0) return;
// OK to add now - we've double checked position
this->addChild(projectile);
// Determine where we wish to shoot the projectile to
int realX = winSize.width + (projectile->getContentSize().width/2);
float ratio = (float)offY / (float)offX;
int realY = (realX * ratio) + projectile->getPosition().y;
CCPoint realDest = ccp(realX, realY);
// Determine the length of how far we're shooting
int offRealX = realX - projectile->getPosition().x;
int offRealY = realY - projectile->getPosition().y;
float length = sqrtf((offRealX * offRealX) + (offRealY * offRealY));
float velocity = 480/1; // 480pixels/1sec
float realMoveDuration = length/velocity;
// Move projectile to actual endpoint
projectile->runAction( CCSequence::actions( CCMoveTo::actionWithDuration(realMoveDuration, realDest), CCCallFuncN::actionWithTarget(this, callfuncN_selector(HelloWorld::spriteMoveFinished)), NULL) );
// Add to projectiles array
projectile->setTag(2);
_projectiles->addObject(projectile);
}
void HelloWorld::update(CCTime dt)
{
CCArray *projectilesToDelete = CCArray::create();
CCObject* arrayItem1;
CCARRAY_FOREACH(_projectiles, arrayItem1)
{
CCSprite* projectile = (CCSprite*)arrayItem1;
CCRect projectileRect = CCRectMake(
projectile->getPosition().x - (projectile->getContentSize().width/2),
projectile->getPosition().y - (projectile->getContentSize().height/2),
projectile->getContentSize().width,
projectile->getContentSize().height);
CCArray* targetsToDelete = CCArray::create();
CCObject* arrayItem2;
CCARRAY_FOREACH(_targets, arrayItem2)
{
CCSprite* target = (CCSprite*) arrayItem2;
CCRect targetRect = CCRectMake(
target->getPosition().x - (target->getContentSize().width/2),
target->getPosition().y - (target->getContentSize().height/2),
target->getContentSize().width,
target->getContentSize().height);
if (CCRect::CCRectIntersectsRect(projectileRect, targetRect))
{
targetsToDelete->addObject(target);
}
}
CCARRAY_FOREACH(targetsToDelete, arrayItem2)
{
CCSprite* target = (CCSprite*) arrayItem2;
_targets->removeObject(target);
this->removeChild(target, true);
}
if (targetsToDelete->count() > 0)
{
projectilesToDelete->addObject(projectile);
}
targetsToDelete->release();
}
CCARRAY_FOREACH(projectilesToDelete, arrayItem1)
{
CCSprite* projectile = (CCSprite*) arrayItem1;
_projectiles->removeObject(projectile);
this->removeChild(projectile, true);
}
projectilesToDelete->release();
}
Maybe this part is a problem:
bool HelloWorld::init()
{
....
this->schedule( schedule_selector(HelloWorld::update) );
....
}
But I don't understand why this part is a problem.
Please, help me!
change CCTime to float.
in old cocos2d-x version, they have ccTime instead of CCTime
but in 2.0 they remove it. since it is duplicated with float.