(URGENT)Creating a sequence for setDynamic(true) to setDynamic(false)

(URGENT)Creating a sequence for setDynamic(true) to setDynamic(false)
0

#1

Experimenting with several “setXYZ” code, I have been trying to get my reloading slingshot, main code here:,


	if ((1 == a->getCollisionBitmask() && 2 == b->getCollisionBitmask()) || (2 == a->getCollisionBitmask() && 1 == b->getCollisionBitmask()))
	{
		turn += 1;
		AMMO -= 1;
		
		CocosDenshion::SimpleAudioEngine::getInstance()->playEffect("Ball collision.mp3");



		listener->setEnabled(true);

		auto tion = MoveTo::create(NULL, Point(194, 450));
		DEL1BALL->runAction(tion);
		physicsBBody->setVelocityLimit(70);
		DEL1SLINGDOT = Sprite::create("Background.png");
		SpringBody = PhysicsBody::createBox(Size(40, 60)),
			PhysicsMaterial(8.9f, 0.2f, 9.4f);


		SpringBody->setCollisionBitmask(8);
		SpringBody->setContactTestBitmask(true);
		DEL1SLINGDOT->setScale(0.1);
		DEL1SLINGDOT->setVisible(false);
		auto tio = MoveTo::create(NULL, Point(194, 450));
		DEL1SLINGDOT->runAction(tio);
		SpringBody->setDynamic(false);
		
		DEL1SLINGDOT->addComponent(SpringBody);

		PhysicsJointSpring* joint = PhysicsJointSpring::construct(SpringBody, physicsBBody, Point::ZERO, Point::ZERO, 2700.6f, 0.8f);
		getPhysicsWorld()->addJoint(joint);

		this->addChild(DEL1SLINGDOT);

to stop conserving momentum after every reload. By this I mean, If the ball charges into the body which causes reload, the momentum it has when I charged into the wall is conserved and so reloads bouncing about the joint at high speed. I want it to reload with a set momentum. I have tried the following:

setVelocity
setVelocityLimit
setDynamic //(causes errors)
//plus about 5 more

So I ask, how do I get the ball to reload with a set velocity, and how do I achieve this within the collision method? Think of it like a breakwater


#2

If you had a class for each object you wouldn’t be asking most of these questions (probably) as it would make sense how and where to put code to accomplish what that piece needs.

You need:

  1. A slingshot class

  2. A ball class

  3. A class that tracks score, lives left, etc stats etc

  4. A basket class

I’d subclass nothing and make your sprites etc members variables


#3

by class do you mean a cpp and a header(this is how they are referred to in the default files)? Or a class that functions like a “function”. My old console example:

void AreaCalcFunc()
{
	string Jole;
	double x;
	double h;
	double u;
	double a;
	double b;
	double H;
	
	cout<<"Welcome to the area finder, what shape would you like to find the area of?"<<endl;
	
	cin>> Jole;
	if (Jole == "a")
	{cout<<"Please enter the value of one side."<< endl;
	
	cin>> x;
	
	cout<< "The area is "<< x * x;}
	
	if (Jole == "Triangle")
	{cout<<"Please enter the value of the height."<<endl;
	
	cin>> h;
	
	cout<<"and Now the base length"<<endl;
	
	cin>> u;
	
	cout<< "The area is "<< 0.5*(h * u);}
	
	if (Jole == "Trapezium")
	{cout<<"Please enter the length of the top line."<<cta;
	
	cin>> a;
	
	cout<<cta<<"Now please enter the length of the bottom line"<<cta;
	
	cin>> b;
	
	cout<<cta<<"Now enter the height (vertical distance from the top to bottom)"<<cta;
	
	cin>> H;
	
	cout<<cta<<"The area of the trapezium is "<< H*((a + b) * 0.5);}
}
	

because I have attempted the latter, And despite their existence in the header, it doesn’t recognise variables


#4

Yes I mean a class as in a .cpp with a .h.


#5

http://www.cplusplus.com/doc/tutorial/classes/

C++ class might be in any C++ file (.cpp, .h, .hpp).
If you have .h and .cpp this does not mean it’s class but usually yes.


#6

This is a good point. I was trying to keep simple.

class xzy
{


}

#7

don’t forget about ; at end :wink:


#8

No, that was a quiz for @Rascake to see if he would notice :slight_smile:


#9

ah :smile:


#10

But it may be too late to make several classes, I have 30 days left and a functioning slingshot, the only issue is the momentum conservation which is is the reason I posted this topic. In future I’ll be less minimal with classes but can we please sort out the issue? With it solved I can move on the make the game even more playable

I came here AFTER dimon :slight_smile:


#11

Well, to be honest, the terms you use make it hard to understand what you actually want/need. I read your post a few times and we have to take the time to imagine what you are saying.

Example:

What does this even mean to anyone besides you? We have to try and stop and imagine your scene and imagine your sprites and imagine the ball moving and, and, and. Then look at your code, then say ok, he has code and it does this…imagining how the code applies to the imagined scene we just imagined…

just use shapes and numbers.

ball has a velocity of 10.

ball collides with wall.

The wall takes away some of the velocity of the ball, say 8, 2 remaining.

I want to take the 2 remaining and add that to the previous velocity from the last attempt. Or if this was the first attempt add the remaining to a default value.

You are asking us to help develop your game. You don’t really want concepts you want specific code to solve your specific problem.

You don’t post pictures all the time

You don’t post all the relevant code. You post what you think we want.

You argue when we give you advice or say the advice isn’t for you. Or just to help you with what you have.

We are here to teach, not write your game for you.


#12

The reason I
"

"
d earlier was because what you said had nothing to do with velocity


#13

reset forces and accelerations on the body, velocity is not the only property in dynamics


#14

I only asked how to stop mass conservation? A simple line. I would send an image to make it simpler but I fear that all I’d get in return is criticism for not using classes. But simply put, I want to use a code like this:

physicsBody-> **xyz**

in a collision method to give the body a neutral mass. Your suggested method didn’t work I am sorry to say

You make it seem my entire game is just code from the forum glued together. This is no-where near the case. I see the forum as a last resort after at least a day of unsuccessful in-depth research into an issue, this happened to be one of those circumstances


#15

But what did you try?

You didn’t show us where you tried any of that code. You just said you tried.

The thing about what you are doing is you are trying to spawn new objects inside a contact event right?


#16

Partly :), I reposition the ball and spawn the joint.

Sorry to cause suspicion, you do not see the attempts as the moment I find out they don’t work I just erase it

but from my many attempts I tried to addMass setMass setDynamic(false) setVelocityLimit setVelocity

As the setposition way causes lag, I use a null speed moveto. This causes the ball to reload based on the velocity on collsion <-- something I have been frantically trying to fix


#17

I think I see. Post the entire collision that is occurring, let’s make sure we have an accurate picture of that.

So when the ball hits a wall you want to take left over velocity and apply it to the next shot?


#18

Ok, A picture tells a thousand words so I made a youtube video. Please skip to 0:08


and notice how, when the ball is launched lightly and collides with the floor, it re-loads very lightly. When I launch the ball into the ground at a high speed it respawns so fast you cannot control it. This is the code of the method, perhaps I can add something to it to make it have no dynamicy and then dynamicy, but if I try this I get an abort error:
bool DEL1::onContactBegin(cocos2d::PhysicsContact &contact)
{
	PhysicsBody *a = contact.getShapeA()->getBody();
	PhysicsBody *b = contact.getShapeB()->getBody();

	if ((1 == a->getCollisionBitmask() && 2 == b->getCollisionBitmask()) || (2 == a->getCollisionBitmask() && 1 == b->getCollisionBitmask()))
	{
		turn += 1;
		AMMO -= 1;
		
		CocosDenshion::SimpleAudioEngine::getInstance()->playEffect("Ball collision.mp3");

		
			
		 
		listener->setEnabled(true);
		
		auto tion = MoveTo::create(NULL, Point(194, 450));
		DEL1BALL->runAction(tion);
		
		DEL1SLINGDOT = Sprite::create("Background.png");
		SpringBody = PhysicsBody::createBox(Size(40, 60)),
			PhysicsMaterial(8.9f, 0.2f, 9.4f);


		SpringBody->setCollisionBitmask(8);
		SpringBody->setContactTestBitmask(true);
		DEL1SLINGDOT->setScale(0.1);
		DEL1SLINGDOT->setVisible(false);
		auto tio = MoveTo::create(NULL, Point(194, 450));
		DEL1SLINGDOT->runAction(tio);
		SpringBody->setDynamic(false);
		
		DEL1SLINGDOT->addComponent(SpringBody);

		PhysicsJointSpring* joint = PhysicsJointSpring::construct(SpringBody, physicsBBody, Point::ZERO, Point::ZERO, 1800.6f, 0.8f);
		getPhysicsWorld()->addJoint(joint);

		this->addChild(DEL1SLINGDOT);

// (other collision actions, like the prompt you saw)

#19

Why are you not allowing gravity to effect things?

If I think about how a slingshot would work you would pull back and let go. The more I pull back the faster and farther the ball should go. If the ball hits something going fast it will lose some speed but still be fast. If the ball hits something slowly it will lose some speed but still be slow or even just fall to the ground if it is to slow.

I’d think that you need to look at setVelocity,setMoment, setLinearDamping

This example:

and this one:

I think would have what you need.


#20

This is what the slingshot joint connects to. It is a non collidable invisible block which, on release, is removed. The ball is hooked with gravity and does normal physics

Here it is when it’s not invisible:
(very tiny green spec above the ball) this is why I called it the slingdot
luc

the ball is connected to that spec with a spring joint. On release this dot is removed, casing the ball to launch

Upon analysis of the video, what do you think is best. I have tried velocity and already use lineardamping, but they both failed to replicate the ball at the beginning, as damping made the ball slower than the last and velocity doesn’t actually change the hole conservation issue but I’ll have a look at the moment.

Edit: moment did not show any difference :frowning: