hi im just wondering how to make use of the mouse joint
ie would it be possible to have a summary of whats required, ie retrieve the mouse co-ords, and if left button is down
and some syntax to go along with it, as the only examples ive seen is for AS3, also i was unable to find it in the testbed, or to retrieve a good account of whats occuring
thanks
btw, just incase heres my soruce, needs cleaned and theres no real system yet, after i get the mouse joint working, im going to work on the gameplay aspect (shouldnt take long, as its simple), and then go on making a more dynamic system, so its easier to create scenes
EDIT : Damn, sorry only realised i had two windows opened, and posted this thread in the wrong category sorry, could a mod please move it, thank you
ie would it be possible to have a summary of whats required, ie retrieve the mouse co-ords, and if left button is down
and some syntax to go along with it, as the only examples ive seen is for AS3, also i was unable to find it in the testbed, or to retrieve a good account of whats occuring
thanks
btw, just incase heres my soruce, needs cleaned and theres no real system yet, after i get the mouse joint working, im going to work on the gameplay aspect (shouldnt take long, as its simple), and then go on making a more dynamic system, so its easier to create scenes
EDIT : Damn, sorry only realised i had two windows opened, and posted this thread in the wrong category sorry, could a mod please move it, thank you
Code:
/// Includes //////
#include <stdio.h>
#include <stdlib.h>
#include <cmath>
#include "Box2D.h"
#include "SDL/SDL.h"
#include "SDL/SDL_gfxPrimitives.h"
//////////////////////////////////
/// Window Parameters //////
const int WINDOW_WIDTH = 800;
const int WINDOW_HEIGHT = 480;
const char* WINDOW_TITLE = "Life Of Slime";
///////////////////////////////////////////
/// Scale Meters to Pixels, for Drawing ///
float scale = 10;
////////////////
/// Set-up Object Properties ///
float Ball[3]={40,1, 1};// x-y-r
float Ground[4]={40,47, 40,1};// x-y-(1/2)w-(1/2)h
float Platform[4]={38,35, 1.5,0.5};// x-y-(1/2)w-(1/2)h
///////////////////////////////////////////////////////
/// Controls Table ///
int Mouse[4];
/////////////
int main(int argc, char **argv)
{
/// Initiate Window ///////
SDL_Init( SDL_INIT_VIDEO );
SDL_Surface* screen = SDL_SetVideoMode( WINDOW_WIDTH, WINDOW_HEIGHT, 0, SDL_HWSURFACE | SDL_DOUBLEBUF /* | SDL_FULLSCREEN */ );
SDL_WM_SetCaption( WINDOW_TITLE, 0 );
/////////////////////////////////////
/// Rename for Simipiler Reference ///
SDL_Event event;
////////////////
/// Initiate World ///
b2AABB worldAABB;
worldAABB.lowerBound.Set(-100, -100);
worldAABB.upperBound.Set(100, 100);
b2Vec2 gravity(0.0f, 9.8f);
bool doSleep = true;
b2World world(worldAABB, gravity, doSleep);
///////////////////////////////////////////
/// Simulation Parameters //////
float32 timeStep = 1.0f / 60.0f;
int32 iterations = 10;
bool pause=true;
////////////////
/// Create Ground Body [Static] ///
b2BodyDef groundBodyDef;
groundBodyDef.position.Set(Ground[0], Ground[1]);
b2Body* groundBody = world.CreateBody(&groundBodyDef);
b2PolygonDef groundShapeDef;
groundShapeDef.SetAsBox(Ground[2], Ground[3]);
groundBody->CreateShape(&groundShapeDef);
/////////////////////////////////////////
/// Create Platform Body [Static] ///
b2BodyDef platformBodyDef;
platformBodyDef.position.Set(Platform[0], Platform[1]);
b2Body* platformBody = world.CreateBody(&platformBodyDef);
b2PolygonDef platformShapeDef;
platformShapeDef.SetAsBox(Platform[2], Platform[3]);
platformBody->CreateShape(&platformShapeDef);
/////////////////////////////////////////////
/// Create Ball Body [Dynamic] ///
b2BodyDef ballBodyDef;
ballBodyDef.position.Set(Ball[0], Ball[1]);
b2Body* ballBody = world.CreateBody(&ballBodyDef);
b2CircleDef ballShapeDef;
ballShapeDef.radius = Ball[2];
ballShapeDef.density = 1.0f;
ballShapeDef.friction = 1.0f;
ballBodyDef.linearDamping = 10000;
ballBodyDef.angularDamping = 10000;
ballShapeDef.restitution = 0.2;
ballBody->CreateShape(&ballShapeDef);
ballBody->SetMassFromShapes();
//////////////////////////////
/*/// Create Mouse Joint ///////
b2MouseJointDef mouseJointDef;
mouseJointDef.body1 = groundBody;
mouseJointDef.body2 = ballBody;
//mouseJointDef.target();
mouseJointDef.maxForce = 3000;
//mouseJointDef.frequencyHz = 1;
mouseJointDef.dampingRatio = 0.1;
mouseJointDef.timeStep = 1.0f / 60.0f;
//////////////////////////////////////*/
bool gameRunning = true;
while (gameRunning)
{
/// Pause Simulation /////////////////////////////////////////////
if (pause==true){
if (SDL_PollEvent(&event)){
if (event.type == SDL_QUIT) {gameRunning = false;} // Quit Application
if ( event.type == SDL_KEYDOWN ){
if ( event.key.keysym.sym == SDLK_SPACE ) pause=false;
}
}
}else{
world.Step(timeStep, iterations);
if (SDL_PollEvent(&event)){
if (event.type == SDL_QUIT) {gameRunning = false;} // Quit Application
if ( event.type == SDL_KEYDOWN ){
if ( event.key.keysym.sym == SDLK_SPACE ) pause=true;
}
}
}
////////////////////////////////////////////////////////////////////
/// Mouse /////////////////////////////////////////////////////////////////////////////////
if( SDL_GetMouseState(0, 0) & SDL_BUTTON(SDL_BUTTON_LEFT)){ Mouse[0]=1; }else{ Mouse[0]=0;}
Mouse[2]=event.motion.x;
Mouse[3]=event.motion.y;
////////////////////////
/// Return Ball Position and Angle, To Draw ///
b2Vec2 position = ballBody->GetPosition();
float32 angle = ballBody->GetAngle();
/////////////////////////////////////
/// Clear Screen ///
SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 0, 0, 0));
////
/// Draw Ground ///
boxRGBA(screen, (Ground[0]-Ground[2])*scale, (Ground[1]-Ground[3])*scale, (Ground[0]+Ground[2])*scale, (Ground[1]+Ground[3])*scale, 0, 255, 0, 255);
////
/// Draw Platform ///
boxRGBA(screen, (Platform[0]-Platform[2])*scale, (Platform[1]-Platform[3])*scale, (Platform[0]+Platform[2])*scale, (Platform[1]+Platform[3])*scale, 0, 255, 0, 255);
////
/// Draw Ball ///
filledEllipseRGBA(screen, position.x*scale, position.y*scale, Ball[2]*scale, Ball[2]*scale, 255, 255, 255, 255);
////
/// Draw Circle at Moue once Left button is clicked ///
if (Mouse[0]==1){
filledEllipseRGBA(screen, event.motion.x, event.motion.y, 10, 10, 255, 0, 0, 255);
}
SDL_Flip(screen);
}
SDL_Quit();
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <cmath>
#include "Box2D.h"
#include "SDL/SDL.h"
#include "SDL/SDL_gfxPrimitives.h"
//////////////////////////////////
/// Window Parameters //////
const int WINDOW_WIDTH = 800;
const int WINDOW_HEIGHT = 480;
const char* WINDOW_TITLE = "Life Of Slime";
///////////////////////////////////////////
/// Scale Meters to Pixels, for Drawing ///
float scale = 10;
////////////////
/// Set-up Object Properties ///
float Ball[3]={40,1, 1};// x-y-r
float Ground[4]={40,47, 40,1};// x-y-(1/2)w-(1/2)h
float Platform[4]={38,35, 1.5,0.5};// x-y-(1/2)w-(1/2)h
///////////////////////////////////////////////////////
/// Controls Table ///
int Mouse[4];
/////////////
int main(int argc, char **argv)
{
/// Initiate Window ///////
SDL_Init( SDL_INIT_VIDEO );
SDL_Surface* screen = SDL_SetVideoMode( WINDOW_WIDTH, WINDOW_HEIGHT, 0, SDL_HWSURFACE | SDL_DOUBLEBUF /* | SDL_FULLSCREEN */ );
SDL_WM_SetCaption( WINDOW_TITLE, 0 );
/////////////////////////////////////
/// Rename for Simipiler Reference ///
SDL_Event event;
////////////////
/// Initiate World ///
b2AABB worldAABB;
worldAABB.lowerBound.Set(-100, -100);
worldAABB.upperBound.Set(100, 100);
b2Vec2 gravity(0.0f, 9.8f);
bool doSleep = true;
b2World world(worldAABB, gravity, doSleep);
///////////////////////////////////////////
/// Simulation Parameters //////
float32 timeStep = 1.0f / 60.0f;
int32 iterations = 10;
bool pause=true;
////////////////
/// Create Ground Body [Static] ///
b2BodyDef groundBodyDef;
groundBodyDef.position.Set(Ground[0], Ground[1]);
b2Body* groundBody = world.CreateBody(&groundBodyDef);
b2PolygonDef groundShapeDef;
groundShapeDef.SetAsBox(Ground[2], Ground[3]);
groundBody->CreateShape(&groundShapeDef);
/////////////////////////////////////////
/// Create Platform Body [Static] ///
b2BodyDef platformBodyDef;
platformBodyDef.position.Set(Platform[0], Platform[1]);
b2Body* platformBody = world.CreateBody(&platformBodyDef);
b2PolygonDef platformShapeDef;
platformShapeDef.SetAsBox(Platform[2], Platform[3]);
platformBody->CreateShape(&platformShapeDef);
/////////////////////////////////////////////
/// Create Ball Body [Dynamic] ///
b2BodyDef ballBodyDef;
ballBodyDef.position.Set(Ball[0], Ball[1]);
b2Body* ballBody = world.CreateBody(&ballBodyDef);
b2CircleDef ballShapeDef;
ballShapeDef.radius = Ball[2];
ballShapeDef.density = 1.0f;
ballShapeDef.friction = 1.0f;
ballBodyDef.linearDamping = 10000;
ballBodyDef.angularDamping = 10000;
ballShapeDef.restitution = 0.2;
ballBody->CreateShape(&ballShapeDef);
ballBody->SetMassFromShapes();
//////////////////////////////
/*/// Create Mouse Joint ///////
b2MouseJointDef mouseJointDef;
mouseJointDef.body1 = groundBody;
mouseJointDef.body2 = ballBody;
//mouseJointDef.target();
mouseJointDef.maxForce = 3000;
//mouseJointDef.frequencyHz = 1;
mouseJointDef.dampingRatio = 0.1;
mouseJointDef.timeStep = 1.0f / 60.0f;
//////////////////////////////////////*/
bool gameRunning = true;
while (gameRunning)
{
/// Pause Simulation /////////////////////////////////////////////
if (pause==true){
if (SDL_PollEvent(&event)){
if (event.type == SDL_QUIT) {gameRunning = false;} // Quit Application
if ( event.type == SDL_KEYDOWN ){
if ( event.key.keysym.sym == SDLK_SPACE ) pause=false;
}
}
}else{
world.Step(timeStep, iterations);
if (SDL_PollEvent(&event)){
if (event.type == SDL_QUIT) {gameRunning = false;} // Quit Application
if ( event.type == SDL_KEYDOWN ){
if ( event.key.keysym.sym == SDLK_SPACE ) pause=true;
}
}
}
////////////////////////////////////////////////////////////////////
/// Mouse /////////////////////////////////////////////////////////////////////////////////
if( SDL_GetMouseState(0, 0) & SDL_BUTTON(SDL_BUTTON_LEFT)){ Mouse[0]=1; }else{ Mouse[0]=0;}
Mouse[2]=event.motion.x;
Mouse[3]=event.motion.y;
////////////////////////
/// Return Ball Position and Angle, To Draw ///
b2Vec2 position = ballBody->GetPosition();
float32 angle = ballBody->GetAngle();
/////////////////////////////////////
/// Clear Screen ///
SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 0, 0, 0));
////
/// Draw Ground ///
boxRGBA(screen, (Ground[0]-Ground[2])*scale, (Ground[1]-Ground[3])*scale, (Ground[0]+Ground[2])*scale, (Ground[1]+Ground[3])*scale, 0, 255, 0, 255);
////
/// Draw Platform ///
boxRGBA(screen, (Platform[0]-Platform[2])*scale, (Platform[1]-Platform[3])*scale, (Platform[0]+Platform[2])*scale, (Platform[1]+Platform[3])*scale, 0, 255, 0, 255);
////
/// Draw Ball ///
filledEllipseRGBA(screen, position.x*scale, position.y*scale, Ball[2]*scale, Ball[2]*scale, 255, 255, 255, 255);
////
/// Draw Circle at Moue once Left button is clicked ///
if (Mouse[0]==1){
filledEllipseRGBA(screen, event.motion.x, event.motion.y, 10, 10, 255, 0, 0, 255);
}
SDL_Flip(screen);
}
SDL_Quit();
return 0;
}